module IpApi::Fields

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

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

Constants

FIELDS

Public Instance Methods

field_to_numeric(field) click to toggle source

Gets the numeric value for a given field.

# 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) click to toggle source

Calculates an unique numeric value for a collection of fields.

# 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