Skip to content

Commit f3bd7a8

Browse files
committed
fix rubocop and test errors/warnings
1 parent 5c05794 commit f3bd7a8

File tree

11 files changed

+210
-203
lines changed

11 files changed

+210
-203
lines changed

.rubocop.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Layout/LineLength:
99
Enabled: true
1010
Max: 80
1111

12+
Lint/MissingSuper:
13+
Enabled: false
14+
1215
Metrics/MethodLength:
1316
Enabled: false
1417

@@ -18,7 +21,19 @@ Metrics/AbcSize:
1821
Lint/DuplicateMethods:
1922
Enabled: false
2023

24+
Style/Documentation:
25+
Enabled: false
26+
27+
Style/ClassAndModuleChildren:
28+
Enabled: false
29+
2130
Style/MethodCallWithArgsParentheses:
31+
EnforcedStyle: require_parentheses
32+
IgnoreMacros: false
33+
IgnoredPatterns: []
2234
AllowParenthesesInMultilineCall: true
2335
AllowParenthesesInChaining: true
2436
AllowParenthesesInCamelCaseMethod: true
37+
38+
Style/MethodCallWithoutArgsParentheses:
39+
Enabled: false

lib/ipinfo.rb

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,76 +22,77 @@ def create(access_token = nil, settings = {})
2222
IPinfo.new(access_token, settings)
2323
end
2424
end
25+
end
2526

26-
class IPinfo
27-
attr_accessor :access_token, :countries, :http_client
28-
29-
def initialize(access_token = nil, settings = {})
30-
@access_token = access_token
31-
@http_client = http_client(settings.fetch('http_client', nil))
27+
class IPinfo::IPinfo
28+
include IPinfo
29+
attr_accessor :access_token, :countries, :httpc
3230

33-
maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
34-
ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
35-
@cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
36-
@countries = countries(settings.fetch('countries',
37-
DEFAULT_COUNTRY_FILE))
38-
end
31+
def initialize(access_token = nil, settings = {})
32+
@access_token = access_token
33+
@httpc = prepare_http_client(settings.fetch('http_client', nil))
3934

40-
def details(ip_address = nil)
41-
details = request_details(ip_address)
42-
if details.key? :country
43-
details[:country_name] =
44-
@countries.fetch(details.fetch(:country), nil)
45-
end
35+
maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
36+
ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
37+
@cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
38+
@countries = prepare_countries(settings.fetch('countries',
39+
DEFAULT_COUNTRY_FILE))
40+
end
4641

47-
if details.key? :ip
48-
details[:ip_address] =
49-
IPAddr.new(details.fetch(:ip))
50-
end
42+
def details(ip_address = nil)
43+
details = request_details(ip_address)
44+
if details.key? :country
45+
details[:country_name] =
46+
@countries.fetch(details.fetch(:country), nil)
47+
end
5148

52-
if details.key? :loc
53-
loc = details.fetch(:loc).split(',')
54-
details[:latitude] = loc[0]
55-
details[:longitude] = loc[1]
56-
end
49+
if details.key? :ip
50+
details[:ip_address] =
51+
IPAddr.new(details.fetch(:ip))
52+
end
5753

58-
Response.new(details)
54+
if details.key? :loc
55+
loc = details.fetch(:loc).split(',')
56+
details[:latitude] = loc[0]
57+
details[:longitude] = loc[1]
5958
end
6059

61-
protected
60+
Response.new(details)
61+
end
6262

63-
def request_details(ip_address = nil)
64-
unless @cache.contains?(ip_address)
65-
response = @http_client.get(escape_path(ip_address))
63+
protected
6664

67-
if response.status.eql?(429)
68-
raise RateLimitError,
69-
RATE_LIMIT_MESSAGE
70-
end
65+
def request_details(ip_address = nil)
66+
unless @cache.contains?(ip_address)
67+
response = @httpc.get(escape_path(ip_address))
7168

72-
details = JSON.parse(response.body, symbolize_names: true)
73-
@cache.set(ip_address, details)
69+
if response.status.eql?(429)
70+
raise RateLimitError,
71+
RATE_LIMIT_MESSAGE
7472
end
75-
@cache.get(ip_address)
76-
end
7773

78-
def http_client(http_client = nil)
79-
@http_client = if http_client
80-
Adapter.new(access_token, http_client)
81-
else
82-
Adapter.new(access_token)
83-
end
74+
details = JSON.parse(response.body, symbolize_names: true)
75+
@cache.set(ip_address, details)
8476
end
77+
@cache.get(ip_address)
78+
end
8579

86-
def countries(filename)
87-
file = File.read(filename)
88-
JSON.parse(file)
89-
end
80+
def prepare_http_client(httpc = nil)
81+
@httpc = if httpc
82+
Adapter.new(access_token, httpc)
83+
else
84+
Adapter.new(access_token)
85+
end
86+
end
87+
88+
def prepare_countries(filename)
89+
file = File.read(filename)
90+
JSON.parse(file)
91+
end
9092

91-
private
93+
private
9294

93-
def escape_path(ip)
94-
ip ? "/#{CGI.escape(ip)}" : '/'
95-
end
95+
def escape_path(ip)
96+
ip ? "/#{CGI.escape(ip)}" : '/'
9697
end
9798
end

lib/ipinfo/adapter.rb

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,43 @@
22

33
require 'faraday'
44
require 'cgi'
5+
require 'ipinfo/mod'
56

6-
module IPinfo
7-
class Adapter
8-
HOST = 'ipinfo.io'
7+
class IPinfo::Adapter
8+
HOST = 'ipinfo.io'
99

10-
attr_reader :conn
10+
attr_reader :conn
1111

12-
def initialize(token = nil, adapter = :net_http)
13-
@token = token
14-
@conn = connection(adapter)
15-
end
12+
def initialize(token = nil, adapter = :net_http)
13+
@token = token
14+
@conn = connection(adapter)
15+
end
1616

17-
def get(uri)
18-
@conn.get(uri) do |req|
19-
default_headers.each_pair do |key, value|
20-
req.headers[key] = value
21-
end
22-
req.params['token'] = CGI.escape(token) if token
17+
def get(uri)
18+
@conn.get(uri) do |req|
19+
default_headers.each_pair do |key, value|
20+
req.headers[key] = value
2321
end
22+
req.params['token'] = CGI.escape(token) if token
2423
end
24+
end
2525

26-
private
26+
private
2727

28-
attr_reader :token
28+
attr_reader :token
2929

30-
def connection(adapter)
31-
Faraday.new(url: "https://#{HOST}") do |faraday|
32-
faraday.adapter adapter
33-
end
30+
def connection(adapter)
31+
Faraday.new(url: "https://#{HOST}") do |conn|
32+
conn.adapter(adapter)
3433
end
34+
end
3535

36-
def default_headers
37-
headers = {
38-
'User-Agent' => 'IPinfoClient/Ruby/1.0',
39-
'Accept' => 'application/json'
40-
}
41-
headers['Authorization'] = "Bearer #{CGI.escape(token)}" if token
42-
headers
43-
end
36+
def default_headers
37+
headers = {
38+
'User-Agent' => 'IPinfoClient/Ruby/1.0',
39+
'Accept' => 'application/json'
40+
}
41+
headers['Authorization'] = "Bearer #{CGI.escape(token)}" if token
42+
headers
4443
end
4544
end
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
# frozen_string_literal: true
22

3-
module IPinfo
4-
class CacheInterface
5-
class InterfaceNotImplemented < StandardError; end
3+
class IPinfo::CacheInterface
4+
class InterfaceNotImplemented < StandardError; end
65

7-
def get(_key)
8-
raise InterfaceNotImplemented
9-
end
6+
def get(_key)
7+
raise InterfaceNotImplemented
8+
end
109

11-
def set(_key, _value)
12-
raise InterfaceNotImplemented
13-
end
10+
def set(_key, _value)
11+
raise InterfaceNotImplemented
12+
end
1413

15-
def contains?(_key)
16-
raise InterfaceNotImplemented
17-
end
14+
def contains?(_key)
15+
raise InterfaceNotImplemented
1816
end
1917
end

lib/ipinfo/cache/default_cache.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33
require 'ipinfo/cache/cache_interface'
44
require 'lru_redux'
55

6-
module IPinfo
7-
class DefaultCache < CacheInterface
8-
def initialize(ttl, max_size)
9-
super
10-
@cache = LruRedux::TTL::Cache.new(max_size, ttl)
11-
end
6+
class IPinfo::DefaultCache < IPinfo::CacheInterface
7+
def initialize(ttl, max_size)
8+
@cache = LruRedux::TTL::Cache.new(max_size, ttl)
9+
end
1210

13-
def get(key)
14-
@cache[key]
15-
end
11+
def get(key)
12+
@cache[key]
13+
end
1614

17-
def set(key, value)
18-
@cache[key] = value
19-
end
15+
def set(key, value)
16+
@cache[key] = value
17+
end
2018

21-
def contains?(key)
22-
!@cache[key].nil?
23-
end
19+
def contains?(key)
20+
!@cache[key].nil?
2421
end
2522
end

lib/ipinfo/errors.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
# frozen_string_literal: true
22

3-
module IPinfo
4-
class RateLimitError < StandardError; end
5-
end
3+
class IPinfo::RateLimitError < StandardError; end

lib/ipinfo/mod.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
module IPinfo
4+
end

lib/ipinfo/response.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
require 'ipaddr'
44
require 'json'
55

6-
module IPinfo
7-
class Response
8-
attr_reader :all
6+
class IPinfo::Response
7+
attr_reader :all
98

10-
def initialize(response)
11-
@all = response
9+
def initialize(response)
10+
@all = response
1211

13-
@all.each do |name, value|
14-
instance_variable_set("@#{name}", value)
15-
self.class.send(:attr_accessor, name)
16-
end
12+
@all.each do |name, value|
13+
instance_variable_set("@#{name}", value)
14+
self.class.send(:attr_accessor, name)
1715
end
1816
end
1917
end

0 commit comments

Comments
 (0)