Skip to content

Commit

Permalink
Add ostruct for Rake incompatibility
Browse files Browse the repository at this point in the history
Also upgrade RuboCop and some formatting to fix errors.

Fixes #108
  • Loading branch information
panthomakos committed Apr 30, 2024
1 parent 7375300 commit ad8c14b
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2']
ruby-version: ['2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3']

steps:
- uses: actions/checkout@v2
Expand Down
29 changes: 24 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
require: rubocop-performance

AllCops:
TargetRubyVersion: 2.2
TargetRubyVersion: 2.4
NewCops: enable
SuggestExtensions: false

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

Layout/EmptyLineBetweenDefs:
Enabled: true

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Layout/ParameterAlignment:
EnforcedStyle: with_fixed_indentation

Layout/SpaceAfterComma:
Enabled: true

Expand All @@ -23,6 +30,9 @@ Layout/SpaceBeforeSemicolon:
Enabled: true


Lint/MissingSuper:
Enabled: false

Lint/DuplicateMethods:
Enabled: true

Expand All @@ -39,7 +49,7 @@ Metrics/ClassLength:
# Offense count: 13
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
# URISchemes: http, https
Metrics/LineLength:
Layout/LineLength:
Max: 80

Metrics/MethodLength:
Expand Down Expand Up @@ -85,12 +95,21 @@ Style/NumericLiterals:
Style/PreferredHashMethods:
Enabled: true

Style/RedundantArgument:
Enabled: false

Style/RedundantFetchBlock:
Enabled: false

Style/RedundantReturn:
Enabled: true

Style/RedundantSelf:
Enabled: true

Style/SafeNavigation:
Enabled: false

Style/SingleLineMethods:
Enabled: false

Expand All @@ -103,7 +122,7 @@ Style/StringLiterals:
Style/TrivialAccessors:
Enabled: true

Style/UnneededPercentQ:
Style/RedundantPercentQ:
Enabled: true

Style/DateTime:
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# master (unreleased)

* Fix Rake 13/OpenStruct incompatibility ([@panthomakos][])

# 1.3.25

* Updated with `tzdata-2024a` ([@panthomakos][])
Expand Down
6 changes: 6 additions & 0 deletions lib/timezone/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ module Timezone
module Error
# Top-level error. All other timezone errors subclass this one.
class Base < StandardError; end

# Indicates an invalid timezone name.
class InvalidZone < Base; end

# Indicates a lookup failure.
class Lookup < Base; end

# Indicates an error during lookup using the geonames API.
class GeoNames < Lookup; end

# Indicates an error during lookup using the google API.
class Google < Lookup; end

# Indicates a missing stub during a test lookup.
class Test < Lookup; end

# Indicates an invalid configuration.
class InvalidConfig < Base; end
end
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
5 changes: 3 additions & 2 deletions lib/timezone/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
require 'timezone/lookup/google'
require 'timezone/lookup/test'
require 'timezone/net_http_client'
require 'ostruct'

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 +46,7 @@ class OptionSetter
test: ::Timezone::Lookup::Test
}.freeze

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

attr_reader :config

Expand Down
1 change: 1 addition & 0 deletions lib/timezone/lookup/geonames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions 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 @@ -34,7 +34,8 @@ def lookup(lat, long)
raise(Timezone::Error::Google, '403 Forbidden')
end

return unless response.code =~ /^2\d\d$/
return unless /^2\d\d$/.match?(response.code)

data = JSON.parse(response.body)

return if data['status'] == NO_TIMEZONE_INFORMATION
Expand Down
9 changes: 5 additions & 4 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 @@ -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,10 +77,10 @@ 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.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
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.25'.freeze
VERSION = '1.3.25'
end
1 change: 0 additions & 1 deletion 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
25 changes: 16 additions & 9 deletions test/timezone/lookup/test_geonames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'timezone/lookup/geonames'
require 'minitest/autorun'
require 'ostruct'
require_relative '../../http_test_client'

class TestGeonames < ::Minitest::Test
Expand All @@ -19,6 +20,10 @@ def etc_data
}
end

def lookup_file(file_name, &block)
lookup(File.open(File.join(mock_path, file_name)).read, &block)
end

def lookup(body = nil, &_block)
config = OpenStruct.new
config.username = 'timezone'
Expand All @@ -40,23 +45,25 @@ def test_missing_username
end

def test_lookup
mine = lookup(File.open(mock_path + '/lat_lon_coords.txt').read)
mine = lookup_file('/lat_lon_coords.txt')

assert_equal 'Australia/Adelaide', mine.lookup(*coordinates)
end

def test_lookup_with_etc
etc_data.each do |key, data|
body = File.open(mock_path + "/lat_lon_coords_#{key}.txt").read
mine = lookup(body) { |c| c.offset_etc_zones = true }
mine = lookup_file("/lat_lon_coords_#{key}.txt") do |c|
c.offset_etc_zones = true
end

assert_equal data[:name], mine.lookup(*data[:coordinates])
end
end

def test_wrong_offset
body = File.open(mock_path + '/lat_lon_coords_wrong_offset.txt').read
mine = lookup(body) { |c| c.offset_etc_zones = true }
mine = lookup_file('/lat_lon_coords_wrong_offset.txt') do |c|
c.offset_etc_zones = true
end

assert_nil mine.lookup(*coordinates)
end
Expand All @@ -80,7 +87,7 @@ def assert_exception(lookup, message)
end

def test_api_limit
mine = lookup(File.open(mock_path + '/api_limit_reached.json').read)
mine = lookup_file('/api_limit_reached.json')

assert_exception(
mine,
Expand All @@ -90,19 +97,19 @@ def test_api_limit
end

def test_invalid_latlong
mine = lookup(File.open(mock_path + '/invalid_latlong.json').read)
mine = lookup_file('/invalid_latlong.json')

assert_exception(mine, 'invalid lat/lng')
end

def test_no_result_found
mine = lookup(File.open(mock_path + '/no_result_found.json').read)
mine = lookup_file('/no_result_found.json')

assert_nil(mine.lookup(10, 10))
end

def test_invalid_parameter
mine = lookup(File.open(mock_path + '/invalid_parameter.json').read)
mine = lookup_file('/invalid_parameter.json')

assert_exception(mine, 'error parsing parameter')
end
Expand Down
9 changes: 7 additions & 2 deletions test/timezone/lookup/test_google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'timezone/lookup/google'
require 'minitest/autorun'
require 'timecop'
require 'ostruct'
require_relative '../../http_test_client'

class TestGoogle < ::Minitest::Test
Expand All @@ -12,6 +13,10 @@ def coordinates
[-34.92771808058, 138.477041423321]
end

def lookup_file(file_name, &block)
lookup(File.open(File.join(mock_path, file_name)).read, &block)
end

def lookup(body = nil, &_block)
config = OpenStruct.new
config.api_key = 'MTIzYWJj'
Expand All @@ -33,7 +38,7 @@ def test_missing_api_key
end

def test_google_using_lat_long_coordinates
mine = lookup(File.open(mock_path + '/google_lat_lon_coords.txt').read)
mine = lookup_file('/google_lat_lon_coords.txt')

assert_equal 'Australia/Adelaide', mine.lookup(*coordinates)
end
Expand Down Expand Up @@ -76,7 +81,7 @@ def test_url_enterprise
end

def test_no_result_found
mine = lookup(File.open(mock_path + '/google_no_result_found.json').read)
mine = lookup_file('/google_no_result_found.json')

assert_nil(mine.lookup(26.188703, -78.987053))
end
Expand Down
1 change: 1 addition & 0 deletions test/timezone/lookup/test_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'timezone/lookup/test'
require 'timezone'
require 'minitest/autorun'
require 'ostruct'

class TestTest < ::Minitest::Test
parallelize_me!
Expand Down
10 changes: 5 additions & 5 deletions test/timezone/test_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_test_config
Timezone::Lookup.config(:test)

assert_equal Timezone::Lookup::Test,
Timezone::Lookup.lookup.class
Timezone::Lookup.lookup.class
end

def test_geonames_config
Expand All @@ -17,10 +17,10 @@ def test_geonames_config
end

assert_equal Timezone::Lookup::Geonames,
Timezone::Lookup.lookup.class
Timezone::Lookup.lookup.class

assert_equal Timezone::NetHTTPClient,
Timezone::Lookup.lookup.config.request_handler
Timezone::Lookup.lookup.config.request_handler
end

def test_google_config
Expand All @@ -29,10 +29,10 @@ def test_google_config
end

assert_equal Timezone::Lookup::Google,
Timezone::Lookup.lookup.class
Timezone::Lookup.lookup.class

assert_equal Timezone::NetHTTPClient,
Timezone::Lookup.lookup.config.request_handler
Timezone::Lookup.lookup.config.request_handler
end

def test_custom_config
Expand Down
Loading

0 comments on commit ad8c14b

Please sign in to comment.