Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ruby 3.2 to CI and update RuboCop #107

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/rake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby-version: ['2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '3.0', '3.1']
ruby-version: ['2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand All @@ -30,7 +31,7 @@ jobs:
ruby-version: ['3.0']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
93 changes: 19 additions & 74 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,110 +1,55 @@
require:
- rubocop-performance

AllCops:
TargetRubyVersion: 2.2
NewCops: enable

Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation
Gemspec/DevelopmentDependencies:
Enabled: false

Layout/EmptyLineBetweenDefs:
Enabled: true
Layout/LineLength:
Max: 80

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Layout/SpaceAfterComma:
Enabled: true

Layout/SpaceAroundEqualsInParameterDefault:
Enabled: true

Layout/SpaceAroundOperators:
Enabled: true

Layout/SpaceBeforeSemicolon:
Enabled: true


Lint/DuplicateMethods:
Enabled: true

Lint/UnusedMethodArgument:
Enabled: true
Layout/ParameterAlignment:
EnforcedStyle: with_fixed_indentation

Lint/MissingSuper:
Exclude:
- lib/timezone/lookup/test.rb

Metrics/AbcSize:
Enabled: false

Metrics/ClassLength:
Enabled: false

# Offense count: 13
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
# URISchemes: http, https
Metrics/LineLength:
Max: 80

Metrics/MethodLength:
Enabled: false

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 8


Performance/RedundantBlockCall:
Enabled: true


Naming/MethodName:
Enabled: true

Naming/BinaryOperatorParameterName:
Enabled: true


Style/ClassVars:
Enabled: true

Style/Documentation:
Enabled: true

Style/DoubleNegation:
Enabled: true

Style/EmptyLiteral:
Enabled: true

Style/GuardClause:
Enabled: true

Style/HashSyntax:
Enabled: true

Style/NumericLiterals:
MinDigits: 5

Style/PreferredHashMethods:
Enabled: true

Style/RedundantReturn:
Enabled: true
Style/OpenStructUse:
Enabled: false

Style/RedundantSelf:
Enabled: true
Style/RedundantInitialize:
Exclude:
- test/http_test_client.rb
- test/timezone/test_lookup.rb

Style/SingleLineMethods:
Enabled: false

Style/SpecialGlobalVars:
EnforcedStyle: use_perl_names

Style/StringLiterals:
Enabled: true

Style/TrivialAccessors:
Enabled: true

Style/UnneededPercentQ:
Enabled: true

Style/DateTime:
Enabled: false
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ task(:utc) { ENV['TZ'] = 'UTC' }
task default: %i[utc test rubocop]

task parse: :utc do
path = ENV['TZPATH'] || File.join(ENV['HOME'], 'Downloads', 'tz')
path = ENV['TZPATH'] || File.join(Dir.home, 'Downloads', 'tz')

require 'timezone/parser'

Expand Down
4 changes: 2 additions & 2 deletions lib/timezone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def self.[](name)
#
# @raise [Timezone::Error::InvalidZone] if the timezone is not found
# and a default value and block have not been provided
def self.fetch(name, default = :__block, &block)
def self.fetch(name, default = :__block)
return ::Timezone::Zone.new(name) if Loader.valid?(name)

if block_given? && default != :__block
warn('warning: block supersedes default value argument')
end

return block.call(name) if block_given?
return yield(name) if block_given?
return default unless default == :__block

raise ::Timezone::Error::InvalidZone
Expand Down
4 changes: 2 additions & 2 deletions lib/timezone/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

require 'timezone/error'

module Timezone # rubocop:disable Style/Documentation
module Timezone
# Responsible for loading and parsing timezone data from files.
module Loader
ZONE_FILE_PATH = File.expand_path(File.dirname(__FILE__) + '/../../data')
ZONE_FILE_PATH = File.expand_path("#{File.dirname(__FILE__)}/../../data")
SOURCE_BIT = 0

@rules = {} # cache of loaded rules
Expand Down
4 changes: 2 additions & 2 deletions lib/timezone/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Timezone
# Configure timezone lookups.
module Lookup
class << self
MISSING_LOOKUP = 'No lookup configured'.freeze
MISSING_LOOKUP = 'No lookup configured'
private_constant :MISSING_LOOKUP

# Returns the lookup object
Expand Down Expand Up @@ -45,7 +45,7 @@ class OptionSetter
test: ::Timezone::Lookup::Test
}.freeze

INVALID_LOOKUP = 'Invalid lookup specified'.freeze
INVALID_LOOKUP = 'Invalid lookup specified'

attr_reader :config

Expand Down
3 changes: 2 additions & 1 deletion lib/timezone/lookup/geonames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def lookup(lat, long)

return unless data['status']

return if NO_TIMEZONE_INFORMATION == data['status']['value']
return if data['status']['value'] == NO_TIMEZONE_INFORMATION

raise(Timezone::Error::GeoNames, data['status']['message'])
rescue StandardError => e
Expand All @@ -55,6 +55,7 @@ def get_timezone_id(data)
return unless data['gmtOffset'].is_a? Numeric

return 'Etc/UTC' if data['gmtOffset'].zero?

"Etc/GMT#{format('%+d', -data['gmtOffset'])}"
end

Expand Down
3 changes: 2 additions & 1 deletion lib/timezone/lookup/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Lookup
class Google < ::Timezone::Lookup::Basic
# Indicates that no time zone data could be found for the specified
# <lat, lng>. This can occur if the query is incomplete or ambiguous.
NO_TIMEZONE_INFORMATION = 'ZERO_RESULTS'.freeze
NO_TIMEZONE_INFORMATION = 'ZERO_RESULTS'

def initialize(config)
if config.api_key.nil?
Expand All @@ -35,6 +35,7 @@ def lookup(lat, long)
end

return unless response.code =~ /^2\d\d$/

data = JSON.parse(response.body)

return if data['status'] == NO_TIMEZONE_INFORMATION
Expand Down
13 changes: 7 additions & 6 deletions lib/timezone/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Parser
MIN_YEAR = -500
MAX_YEAR = 2039

LINE = /\s*(.+)\s*=\s*(.+)\s*isdst=(\d+)\s*gmtoff=([\+\-]*\d+)/
LINE = /\s*(.+)\s*=\s*(.+)\s*isdst=(\d+)\s*gmtoff=([+-]*\d+)/.freeze

# Bookkeeping files that we do not want to parse.
IGNORE = ['leapseconds', 'posixrules', 'tzdata.zi'].freeze
Expand All @@ -25,6 +25,7 @@ def perform
next if File.directory?(file)
next if file.end_with?('.tab')
next if IGNORE.include?(File.basename(file))

parse(file)
end
end
Expand Down Expand Up @@ -52,7 +53,7 @@ def initialize(config, file)
.reject { |line| line.start_with?('TZ=') }
.first

_date, _time, raw_offset, @name = first.split(' ')
_date, _time, raw_offset, @name = first.split
@offset = parse_offset(raw_offset)
end

Expand All @@ -65,7 +66,7 @@ def to_s
def parse_offset(offset)
arity = offset.start_with?('-') ? -1 : 1

match = offset.match(/^[\-\+](\d{2})$/)
match = offset.match(/^[-+](\d{2})$/)
arity * match[1].to_i * 60 * 60
end
end
Expand All @@ -76,11 +77,11 @@ def parse_offset(offset)
class Line
attr_accessor :source, :name, :dst, :offset

SOURCE_FORMAT = '%a %b %e %H:%M:%S %Y %Z'.freeze
SOURCE_FORMAT = '%a %b %e %H:%M:%S %Y %Z'

def initialize(match)
self.source = Time.strptime(match[1] + 'C', SOURCE_FORMAT).to_i
self.name = match[2].split(' ').last
self.source = Time.strptime("#{match[1]}C", SOURCE_FORMAT).to_i
self.name = match[2].split.last
self.dst = match[3].to_i
self.offset = match[4].to_i
end
Expand Down
2 changes: 1 addition & 1 deletion lib/timezone/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Timezone
VERSION = '1.3.23'.freeze
VERSION = '1.3.23'
end
3 changes: 1 addition & 2 deletions lib/timezone/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

require 'timezone/loader'
require 'timezone/error'
require 'timezone/loader'

module Timezone
# This object represents a real-world timezone. Each instance provides
Expand Down Expand Up @@ -187,7 +186,7 @@ def sanitize(time)
#
# Each rule has a SOURCE bit which is the number of seconds, since the
# Epoch, up to which the rule is valid.
def match?(seconds, rule) #:nodoc:
def match?(seconds, rule) # :nodoc:
seconds <= rule[SOURCE_BIT]
end

Expand Down
7 changes: 6 additions & 1 deletion test/test_timezone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'timezone'
require 'minitest/autorun'

class TestTimezone < ::Minitest::Test
class TestTimezone < Minitest::Test
parallelize_me!

def test_names
Expand All @@ -20,7 +20,12 @@ def test_get

def test_fetch
assert Timezone.fetch('Australia/Sydney').valid?

# Explicitly testing block syntax, so disable Cop
# rubocop:disable Style/RedundantFetchBlock
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

assert_equal 'foo', Timezone.fetch('foo/bar') { 'foo' }
# rubocop:enable Style/RedundantFetchBlock

assert_raises Timezone::Error::InvalidZone do
Timezone.fetch('foo/bar')
end
Expand Down
2 changes: 1 addition & 1 deletion test/timezone/lookup/test_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'minitest/autorun'
require 'ostruct'

class BasicLookupTest < ::Minitest::Test
class BasicLookupTest < Minitest::Test
parallelize_me!

def config
Expand Down
Loading