Skip to content

Commit b4c3cfb

Browse files
committed
run rubocop -a
1 parent ba79dec commit b4c3cfb

16 files changed

Lines changed: 315 additions & 296 deletions

.rubocop.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AllCops:
2+
TargetRubyVersion: 2.7
3+
4+
Layout/IndentationWidth:
5+
Width: 4
6+
7+
Layout/LineLength:
8+
Enabled: true
9+
Max: 80

Gemfile

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development do
6-
gem "rack", ">= 1.5"
7-
gem "bundler"
8-
gem "rake"
9-
gem "minitest"
10-
gem 'minitest-vcr'
11-
gem 'minitest-reporters'
12-
gem 'webmock'
13-
gem 'rubocop'
6+
gem 'bundler'
7+
gem 'minitest'
8+
gem 'minitest-reporters'
9+
gem 'minitest-vcr'
10+
gem 'rack', '>= 1.5'
11+
gem 'rake'
12+
gem 'rubocop'
13+
gem 'webmock'
1414

15-
platforms :mri do
16-
# to avoid problems, bring Byebug in on just versions of Ruby under which
17-
# it's known to work well
18-
if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new("2.0.0")
19-
gem "byebug"
20-
gem "pry"
21-
gem "pry-byebug"
15+
platforms :mri do
16+
# to avoid problems, bring Byebug in on just versions of Ruby under which
17+
# it's known to work well
18+
if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.0.0')
19+
gem 'byebug'
20+
gem 'pry'
21+
gem 'pry-byebug'
22+
end
2223
end
23-
end
2424
end

Rakefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
require "bundler/gem_tasks"
2-
require "rake/testtask"
1+
require 'bundler/gem_tasks'
2+
require 'rake/testtask'
33

44
Rake::TestTask.new(:test) do |t|
5-
t.libs << "test"
6-
t.libs << "lib"
7-
t.test_files = FileList['test/**/*_test.rb']
5+
t.libs << 'test'
6+
t.libs << 'lib'
7+
t.test_files = FileList['test/**/*_test.rb']
88
end
99

10-
task :default => :test
10+
task default: :test

bin/console

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
require "ipinfo"
3+
require 'bundler/setup'
4+
require 'ipinfo'
55

6-
require "irb"
6+
require 'irb'
77
IRB.start

ipinfo.gemspec

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
lib = File.expand_path('../lib', __FILE__)
1+
lib = File.expand_path('lib', __dir__)
22
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
33

44
require 'ipinfo/version'
55

66
Gem::Specification.new do |spec|
7-
spec.name = "IPinfo"
8-
spec.version = IPinfo::VERSION
9-
spec.required_ruby_version = ">= 2.5.0"
10-
spec.authors = ["Stanislav K, James Timmins", "Uman Shahzad"]
11-
spec.email = ["jameshtimmins@gmail.com", "uman@mslm.io"]
7+
spec.name = 'IPinfo'
8+
spec.version = IPinfo::VERSION
9+
spec.required_ruby_version = '>= 2.5.0'
10+
spec.authors = ['Stanislav K, James Timmins', 'Uman Shahzad']
11+
spec.email = ['jameshtimmins@gmail.com', 'uman@mslm.io']
1212

13-
spec.summary = %q{ This is a ruby wrapper for http://ipinfo.io. }
14-
spec.description = %q{ This is a ruby wrapper for http://ipinfo.io. }
15-
spec.homepage = "https://ipinfo.io"
13+
spec.summary = ' This is a ruby wrapper for http://ipinfo.io. '
14+
spec.description = ' This is a ruby wrapper for http://ipinfo.io. '
15+
spec.homepage = 'https://ipinfo.io'
1616

17-
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the
18-
# 'allowed_push_host' to allow pushing to a single host or delete this
19-
# section to allow pushing to any host.
20-
if spec.respond_to?(:metadata)
21-
#spec.metadata['allowed_push_host'] = "http://mygemserver.com'"
22-
else
23-
raise "RubyGems 2.0 or newer is required to protect against " \
24-
"public gem pushes."
25-
end
17+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the
18+
# 'allowed_push_host' to allow pushing to a single host or delete this
19+
# section to allow pushing to any host.
20+
if spec.respond_to?(:metadata)
21+
# spec.metadata['allowed_push_host'] = "http://mygemserver.com'"
22+
else
23+
raise 'RubyGems 2.0 or newer is required to protect against ' \
24+
'public gem pushes.'
25+
end
2626

27-
spec.add_runtime_dependency 'faraday', '~> 1.0'
28-
spec.add_runtime_dependency 'json', '~> 2.1'
29-
spec.add_runtime_dependency 'lru_redux', '~> 1.1'
27+
spec.add_runtime_dependency 'faraday', '~> 1.0'
28+
spec.add_runtime_dependency 'json', '~> 2.1'
29+
spec.add_runtime_dependency 'lru_redux', '~> 1.1'
3030

31-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
32-
f.match(%r{^(test|spec|features)/})
33-
end
34-
spec.bindir = "exe"
35-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36-
spec.require_paths = ["lib"]
31+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
32+
f.match(%r{^(test|spec|features)/})
33+
end
34+
spec.bindir = 'exe'
35+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36+
spec.require_paths = ['lib']
3737
end

lib/ipinfo.rb

Lines changed: 81 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,88 @@
1010
require 'json'
1111

1212
module IPinfo
13-
DEFAULT_CACHE_MAXSIZE = 4096
14-
DEFAULT_CACHE_TTL = 60 * 60 * 24
15-
DEFAULT_COUNTRY_FILE = File.join(File.dirname(__FILE__), 'ipinfo/countries.json')
16-
RATE_LIMIT_MESSAGE = "To increase your limits, please review our paid plans at https://ipinfo.io/pricing"
17-
18-
class << self
19-
def create(access_token=nil, settings={})
20-
IPinfo.new(access_token, settings)
13+
DEFAULT_CACHE_MAXSIZE = 4096
14+
DEFAULT_CACHE_TTL = 60 * 60 * 24
15+
DEFAULT_COUNTRY_FILE = File.join(File.dirname(__FILE__),
16+
'ipinfo/countries.json')
17+
RATE_LIMIT_MESSAGE = 'To increase your limits, please review our paid plans at https://ipinfo.io/pricing'
18+
19+
class << self
20+
def create(access_token = nil, settings = {})
21+
IPinfo.new(access_token, settings)
22+
end
2123
end
22-
end
2324

24-
class IPinfo
25-
attr_accessor :access_token
26-
attr_accessor :countries
27-
attr_accessor :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))
32-
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', DEFAULT_COUNTRY_FILE))
37-
end
38-
39-
def details(ip_address=nil)
40-
details = request_details(ip_address)
41-
if details.has_key? :country
42-
details[:country_name] = @countries.fetch(details.fetch(:country), nil)
43-
end
44-
45-
if details.has_key? :ip
46-
details[:ip_address] = IPAddr.new(details.fetch(:ip))
47-
end
48-
49-
if details.has_key? :loc
50-
loc = details.fetch(:loc).split(",")
51-
details[:latitude] = loc[0]
52-
details[:longitude] = loc[1]
53-
end
54-
55-
Response.new(details)
56-
end
57-
58-
protected
59-
def request_details(ip_address=nil)
60-
if !@cache.contains?(ip_address)
61-
response = @http_client.get(escape_path(ip_address))
62-
63-
raise RateLimitError.new(RATE_LIMIT_MESSAGE) if response.status.eql?(429)
64-
65-
details = JSON.parse(response.body, symbolize_names: true)
66-
@cache.set(ip_address, details)
67-
end
68-
@cache.get(ip_address)
69-
end
70-
71-
def http_client(http_client=nil)
72-
if http_client
73-
@http_client = Adapter.new(access_token, http_client)
74-
else
75-
@http_client = Adapter.new(access_token)
76-
end
77-
end
78-
79-
def countries(filename)
80-
file = File.read(filename)
81-
JSON.parse(file)
82-
end
83-
84-
private
85-
def escape_path(ip)
86-
ip ? "/#{CGI::escape(ip)}" : '/'
25+
class IPinfo
26+
attr_accessor :access_token, :countries, :http_client
27+
28+
def initialize(access_token = nil, settings = {})
29+
@access_token = access_token
30+
@http_client = http_client(settings.fetch('http_client', nil))
31+
32+
maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
33+
ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
34+
@cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
35+
@countries = countries(settings.fetch('countries',
36+
DEFAULT_COUNTRY_FILE))
37+
end
38+
39+
def details(ip_address = nil)
40+
details = request_details(ip_address)
41+
if details.has_key? :country
42+
details[:country_name] =
43+
@countries.fetch(details.fetch(:country),
44+
nil)
45+
end
46+
47+
if details.has_key? :ip
48+
details[:ip_address] =
49+
IPAddr.new(details.fetch(:ip))
50+
end
51+
52+
if details.has_key? :loc
53+
loc = details.fetch(:loc).split(',')
54+
details[:latitude] = loc[0]
55+
details[:longitude] = loc[1]
56+
end
57+
58+
Response.new(details)
59+
end
60+
61+
protected
62+
63+
def request_details(ip_address = nil)
64+
unless @cache.contains?(ip_address)
65+
response = @http_client.get(escape_path(ip_address))
66+
67+
if response.status.eql?(429)
68+
raise RateLimitError,
69+
RATE_LIMIT_MESSAGE
70+
end
71+
72+
details = JSON.parse(response.body, symbolize_names: true)
73+
@cache.set(ip_address, details)
74+
end
75+
@cache.get(ip_address)
76+
end
77+
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
84+
end
85+
86+
def countries(filename)
87+
file = File.read(filename)
88+
JSON.parse(file)
89+
end
90+
91+
private
92+
93+
def escape_path(ip)
94+
ip ? "/#{CGI.escape(ip)}" : '/'
95+
end
8796
end
88-
end
8997
end

lib/ipinfo/adapter.rb

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
# frozen_string_literal: true
2+
23
require 'faraday'
34
require 'cgi'
45

56
module IPinfo
6-
class Adapter
7-
HOST = 'ipinfo.io'
7+
class Adapter
8+
HOST = 'ipinfo.io'
89

9-
attr_reader :conn
10+
attr_reader :conn
1011

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

16-
def get(uri)
17-
@conn.get(uri) do |req|
18-
default_headers.each_pair do |key, value|
19-
req.headers[key] = value
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
23+
end
2024
end
21-
req.params['token'] = CGI::escape(token) if token
22-
end
23-
end
2425

25-
private
26+
private
2627

27-
attr_reader :token
28+
attr_reader :token
2829

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

35-
def default_headers
36-
headers = {
37-
'User-Agent' => "IPinfoClient/Ruby/1.0",
38-
'Accept' => 'application/json'
39-
}
40-
if token
41-
headers['Authorization'] = "Bearer #{CGI::escape(token)}"
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
4243
end
43-
headers
4444
end
45-
end
4645
end

0 commit comments

Comments
 (0)