Class: IPinfo::IPinfo

Inherits:
Object
  • Object
show all
Includes:
IPinfo
Defined in:
lib/ipinfo.rb

Constant Summary

Constants included from IPinfo

COUNTRY_FLAGS_URL, DEFAULT_CACHE_MAXSIZE, DEFAULT_CACHE_TTL, RATE_LIMIT_MESSAGE, VERSION

Constants included from CountriesData

CountriesData::DEFAULT_CONTINENT_LIST, CountriesData::DEFAULT_COUNTRIES_CURRENCIES_LIST, CountriesData::DEFAULT_COUNTRIES_FLAG_LIST, CountriesData::DEFAULT_COUNTRY_LIST, CountriesData::DEFAULT_EU_COUNTRIES_LIST

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IPinfo

create

Constructor Details

#initialize(access_token = nil, settings = {}) ⇒ IPinfo

Returns a new instance of IPinfo.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ipinfo.rb', line 35

def initialize(access_token = nil, settings = {})
    @access_token = access_token
    prepare_http_client(settings.fetch('http_client', nil))

    maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
    ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
    @cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
    @countries = settings.fetch('countries', DEFAULT_COUNTRY_LIST)
    @eu_countries = settings.fetch('eu_countries', DEFAULT_EU_COUNTRIES_LIST)
    @countries_flags = settings.fetch('countries_flags', DEFAULT_COUNTRIES_FLAG_LIST)
    @countries_currencies = settings.fetch('countries_currencies', DEFAULT_COUNTRIES_CURRENCIES_LIST)
    @continents = settings.fetch('continents', DEFAULT_CONTINENT_LIST)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



33
34
35
# File 'lib/ipinfo.rb', line 33

def access_token
  @access_token
end

#countriesObject

Returns the value of attribute countries.



33
34
35
# File 'lib/ipinfo.rb', line 33

def countries
  @countries
end

#httpcObject

Returns the value of attribute httpc.



33
34
35
# File 'lib/ipinfo.rb', line 33

def httpc
  @httpc
end

Instance Method Details

#batch_requests(url_array, api_token) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ipinfo.rb', line 72

def batch_requests(url_array, api_token)
    result = Hash.new
    lookup_ips = []

    url_array.each { |url|
        ip = @cache.get(cache_key(url))

        unless ip.nil?
            result.store(url, ip)
        else
            lookup_ips << url
        end
    }

    if lookup_ips.empty?
        return result
    end

    begin
        lookup_ips.each_slice(1000){ |ips|
            json_arr = JSON.generate(lookup_ips)
            res = @httpc.post("/batch?token=#{api_token}", json_arr, 5)

            raise StandardError, "Request Quota Exceeded" if res.status == 429

            data = JSON.parse(res.body)
            data.each { |key, val|
                @cache.set(cache_key(key), val)
            }

            result.merge!(data)
        }

    rescue StandardError => e
        return e.message
    end

    result
end

#details(ip_address = nil) ⇒ Object



49
50
51
# File 'lib/ipinfo.rb', line 49

def details(ip_address = nil)
    details_base(ip_address, :v4)
end

#details_v6(ip_address = nil) ⇒ Object



53
54
55
# File 'lib/ipinfo.rb', line 53

def details_v6(ip_address = nil)
    details_base(ip_address, :v6)
end

#get_map_url(ips) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ipinfo.rb', line 57

def get_map_url(ips)
    if !ips.kind_of?(Array)
        return JSON.generate({:error => 'Invalid input. Array required!'})
    end
    if ips.length > 500000
        return JSON.generate({:error => 'No more than 500,000 ips allowed!'})
    end

    json_ips = JSON.generate({:ips => ips})
    res = @httpc.post('/tools/map', json_ips)

    obj = JSON.parse(res.body)
    obj['reportUrl']
end