lib/ipinfo.rb in IPinfo-1.1.0 vs lib/ipinfo.rb in IPinfo-2.2.1

- old
+ new

@@ -7,31 +7,22 @@ require 'ipinfo/errors' require 'ipinfo/response' require 'ipinfo/version' require 'json' require_relative 'ipinfo/ipAddressMatcher' +require_relative 'ipinfo/countriesData' module IPinfo + include CountriesData DEFAULT_CACHE_MAXSIZE = 4096 DEFAULT_CACHE_TTL = 60 * 60 * 24 - DEFAULT_COUNTRY_FILE = File.join(File.dirname(__FILE__), - 'ipinfo/countries.json') - DEFAULT_EU_COUNTRIES_FILE = File.join(File.dirname(__FILE__), - 'ipinfo/eu.json') - DEFAULT_COUNTRIES_FLAG_FILE = File.join(File.dirname(__FILE__), - 'ipinfo/flags.json') - DEFAULT_COUNTRIES_CURRENCIES_FILE = File.join(File.dirname(__FILE__), - 'ipinfo/currency.json') - DEFAULT_CONTINENT_FILE = File.join(File.dirname(__FILE__), - 'ipinfo/continent.json') RATE_LIMIT_MESSAGE = 'To increase your limits, please review our ' \ 'paid plans at https://ipinfo.io/pricing' # Base URL to get country flag image link. # "PK" -> "https://cdn.ipinfo.io/static/images/countries-flags/PK.svg" COUNTRY_FLAGS_URL = "https://cdn.ipinfo.io/static/images/countries-flags/" - class << self def create(access_token = nil, settings = {}) IPinfo.new(access_token, settings) end end @@ -41,55 +32,28 @@ include IPinfo attr_accessor :access_token, :countries, :httpc def initialize(access_token = nil, settings = {}) @access_token = access_token - @httpc = prepare_http_client(settings.fetch('http_client', nil)) + 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 = prepare_json(settings.fetch('countries', - DEFAULT_COUNTRY_FILE)) - @eu_countries = prepare_json(settings.fetch('eu_countries', - DEFAULT_EU_COUNTRIES_FILE)) - @countries_flags = prepare_json(settings.fetch('countries_flags', - DEFAULT_COUNTRIES_FLAG_FILE)) - @countries_currencies = prepare_json(settings.fetch('countries_currencies', - DEFAULT_COUNTRIES_CURRENCIES_FILE)) - @continents = prepare_json(settings.fetch('continents', - DEFAULT_CONTINENT_FILE)) + @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 def details(ip_address = nil) - details = request_details(ip_address) - if details.key? :country - details[:country_name] = - @countries.fetch(details.fetch(:country), nil) - details[:is_eu] = - @eu_countries.include?(details.fetch(:country)) - details[:country_flag] = - @countries_flags.fetch(details.fetch(:country), nil) - details[:country_currency] = - @countries_currencies.fetch(details.fetch(:country), nil) - details[:continent] = - @continents.fetch(details.fetch(:country), nil) - details[:country_flag_url] = COUNTRY_FLAGS_URL + details.fetch(:country) + ".svg" - end + details_base(ip_address, :v4) + end - if details.key? :ip - details[:ip_address] = - IPAddr.new(details.fetch(:ip)) - end - - if details.key? :loc - loc = details.fetch(:loc).split(',') - details[:latitude] = loc[0] - details[:longitude] = loc[1] - end - - Response.new(details) + def details_v6(ip_address = nil) + details_base(ip_address, :v6) end def get_map_url(ips) if !ips.kind_of?(Array) return JSON.generate({:error => 'Invalid input. Array required!'}) @@ -145,11 +109,17 @@ result end protected - def request_details(ip_address = nil) + def prepare_http_client(httpc = nil) + @httpc = Adapter.new(access_token, httpc || :net_http) + end + + private + + def request_details(ip_address = nil, host_type) if isBogon(ip_address) details[:ip] = ip_address details[:bogon] = true details[:ip_address] = IPAddr.new(ip_address) @@ -157,11 +127,11 @@ end res = @cache.get(cache_key(ip_address)) return res unless res.nil? - response = @httpc.get(escape_path(ip_address)) + response = @httpc.get(escape_path(ip_address), host_type) if response.status.eql?(429) raise RateLimitError, RATE_LIMIT_MESSAGE end @@ -169,23 +139,38 @@ details = JSON.parse(response.body, symbolize_names: true) @cache.set(cache_key(ip_address), details) details end - def prepare_http_client(httpc = nil) - @httpc = if httpc - Adapter.new(access_token, httpc) - else - Adapter.new(access_token) - end - end + def details_base(ip_address, host_type) + details = request_details(ip_address, host_type) + if details.key? :country + details[:country_name] = + @countries.fetch(details.fetch(:country), nil) + details[:is_eu] = + @eu_countries.include?(details.fetch(:country)) + details[:country_flag] = + @countries_flags.fetch(details.fetch(:country), nil) + details[:country_currency] = + @countries_currencies.fetch(details.fetch(:country), nil) + details[:continent] = + @continents.fetch(details.fetch(:country), nil) + details[:country_flag_url] = COUNTRY_FLAGS_URL + details.fetch(:country) + ".svg" + end - def prepare_json(filename) - file = File.read(filename) - JSON.parse(file) - end + if details.key? :ip + details[:ip_address] = + IPAddr.new(details.fetch(:ip)) + end - private + if details.key? :loc + loc = details.fetch(:loc).split(',') + details[:latitude] = loc[0] + details[:longitude] = loc[1] + end + + Response.new(details) + end def isBogon(ip) if ip.nil? return false end