|
10 | 10 | require 'json' |
11 | 11 |
|
12 | 12 | 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 |
21 | 23 | end |
22 | | - end |
23 | 24 |
|
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 |
87 | 96 | end |
88 | | - end |
89 | 97 | end |
0 commit comments