@@ -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
9798end
0 commit comments