Module: IpApi::Fields

Included in:
Client
Defined in:
lib/ip_api/fields.rb

Overview

Methods for converting response fields to a numeric value (to save bandwidth).

Read more: ip-api.com/docs/api:json#fieldsTable

Constant Summary collapse

FIELDS =
%W[
  country countryCode region regionName city zip lat lon timezone isp org
  as reverse query status message mobile proxy :empty: district continent
  continentCode asname currency hosting offset
]

Instance Method Summary collapse

Instance Method Details

#field_to_numeric(field) ⇒ Object

Gets the numeric value for a given field.



14
15
16
17
# File 'lib/ip_api/fields.rb', line 14

def field_to_numeric(field)
  idx = FIELDS.index(field.to_s.downcase)
  idx.nil? ? 0 : 2**idx
end

#numeric_fields(*args) ⇒ Object

Calculates an unique numeric value for a collection of fields.



20
21
22
23
24
25
26
# File 'lib/ip_api/fields.rb', line 20

def numeric_fields(*args)
  fields = args.flatten

  return nil if fields.empty?

  fields.inject(0) {|memo, value| memo + field_to_numeric(value) }
end