module Mxmnd

Constants

BASE_PATH
BASE_URL
LICENSE_KEY_ENV_KEY
USER_ID_ENV_KEY
VERSION

Public Class Methods

city(ip, faraday_options = {}) click to toggle source
# File lib/mxmnd.rb, line 12
def city(ip, faraday_options = {})
  raise BadRequest.new('IP_ADDRESS_REQUIRED', 'You have not passed IP address.') unless ip
  path = "/city/#{ip}"
  get(connection(faraday_options), path)
end

Private Class Methods

connection(faraday_options = {}) click to toggle source
# File lib/mxmnd.rb, line 20
def connection(faraday_options = {})
  @connection ||= Faraday.new(BASE_URL, faraday_options)
end
get(connection, path) click to toggle source
# File lib/mxmnd.rb, line 24
def get(connection, path)
  connection.basic_auth(ENV[USER_ID_ENV_KEY], ENV[LICENSE_KEY_ENV_KEY])
  full_path = "#{BASE_PATH}#{path}"
  response = connection.get(full_path)
  raise_if_error!(response)
  JSON.parse(response.body)
end
raise_client_error(response, klass) click to toggle source
# File lib/mxmnd.rb, line 43
def raise_client_error(response, klass)
  response = JSON.parse(response.body)
  code, error = response.values_at(*%w(code error))
  raise klass.new(code, error)
end
raise_if_error!(response) click to toggle source
# File lib/mxmnd.rb, line 32
def raise_if_error!(response)
  case response.status
  when 400 then raise_client_error(response, BadRequest)
  when 401 then raise_client_error(response, Unauthorized)
  when 402 then raise_client_error(response, PaymentRequired)
  when 403 then raise_client_error(response, Forbidden)
  # MaxMind will only return 503, but just in case
  when 500..599 then raise ServerError
  end
end