Class: IPinfo::IPinfoCore

Inherits:
Object
  • Object
show all
Includes:
IPinfoCore
Defined in:
lib/ipinfo_core.rb

Constant Summary

Constants included from IPinfoCore

IPinfoCore::COUNTRY_FLAGS_URL, IPinfoCore::DEFAULT_CACHE_MAXSIZE, IPinfoCore::DEFAULT_CACHE_TTL, IPinfoCore::RATE_LIMIT_MESSAGE

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 IPinfoCore

create

Constructor Details

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

Returns a new instance of IPinfoCore.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ipinfo_core.rb', line 33

def initialize(access_token = nil, settings = {})
    @access_token = access_token
    @httpc = IPinfo::AdapterCore.new(access_token, httpc || :net_http)

    maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
    ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
    @cache = settings.fetch('cache', IPinfo::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.



31
32
33
# File 'lib/ipinfo_core.rb', line 31

def access_token
  @access_token
end

#countriesObject

Returns the value of attribute countries.



31
32
33
# File 'lib/ipinfo_core.rb', line 31

def countries
  @countries
end

#httpcObject

Returns the value of attribute httpc.



31
32
33
# File 'lib/ipinfo_core.rb', line 31

def httpc
  @httpc
end

Instance Method Details

#cache_key(ip) ⇒ Object



121
122
123
# File 'lib/ipinfo_core.rb', line 121

def cache_key(ip)
    "1:#{ip}"
end

#details(ip_address = nil) ⇒ Object



47
48
49
# File 'lib/ipinfo_core.rb', line 47

def details(ip_address = nil)
    details_base(ip_address)
end

#details_base(ip_address) ⇒ Object



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
# File 'lib/ipinfo_core.rb', line 75

def details_base(ip_address)
    details = request_details(ip_address)

    # Core response has nested geo object
    if details.key?(:geo) && details[:geo].is_a?(Hash) && details[:geo].key?(:country_code)
        country_code = details[:geo][:country_code]
        details[:geo][:country_name] = @countries.fetch(country_code, nil)
        details[:geo][:is_eu] = @eu_countries.include?(country_code)
        details[:geo][:country_flag] = @countries_flags.fetch(country_code, nil)
        details[:geo][:country_currency] = @countries_currencies.fetch(country_code, nil)
        details[:geo][:continent] = @continents.fetch(country_code, nil)
        details[:geo][:country_flag_url] = "#{COUNTRY_FLAGS_URL}#{country_code}.svg"
    end

    # Handle top-level country_code if present (for certain edge cases)
    if details.key?(:country_code)
        country_code = details[:country_code]
        details[:country_name] = @countries.fetch(country_code, nil)
        details[:is_eu] = @eu_countries.include?(country_code)
        details[:country_flag] = @countries_flags.fetch(country_code, nil)
        details[:country_currency] = @countries_currencies.fetch(country_code, nil)
        details[:continent] = @continents.fetch(country_code, nil)
        details[:country_flag_url] = "#{COUNTRY_FLAGS_URL}#{country_code}.svg"
    end

    if details.key? :ip
        details[:ip_address] =
            IPAddr.new(details.fetch(:ip))
    end

    IPinfo::Response.new(details)
end

#escape_path(ip) ⇒ Object



117
118
119
# File 'lib/ipinfo_core.rb', line 117

def escape_path(ip)
    ip ? "/#{CGI.escape(ip)}" : '/'
end

#isBogon(ip) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/ipinfo_core.rb', line 108

def isBogon(ip)
    if ip.nil?
      return false
    end

    matcher_object = IPinfo::IpAddressMatcher.new(ip)
    matcher_object.matches
end

#request_details(ip_address = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ipinfo_core.rb', line 51

def request_details(ip_address = nil)
    if ip_address && isBogon(ip_address)
        details = {}
        details[:ip] = ip_address
        details[:bogon] = true
        details[:ip_address] = IPAddr.new(ip_address)
        return details
    end

    res = @cache.get(cache_key(ip_address))
    return res unless res.nil?

    response = @httpc.get(escape_path(ip_address))

    if response.status.eql?(429)
        raise RateLimitError,
              RATE_LIMIT_MESSAGE
    end

    details = JSON.parse(response.body, symbolize_names: true)
    @cache.set(cache_key(ip_address), details)
    details
end