class Geoip2::Client

Public Class Methods

new(config) click to toggle source

Creates a new instance of Geoip2::Client

@param config [Hash] includes all of the config from the

# File lib/geoip2/client.rb, line 25
def initialize(config)
  @base_url = "https://#{config[:host]}"
  @base_path = config[:base_path]
  @parallel_requests = config[:parallel_requests]
  @user = config[:user_id]
  @password = config[:license_key]
end

Public Instance Methods

get(url, params = {}, faraday_options = {}) click to toggle source

Does a GET request to the url with the params

@param url [String] the relative path in the Geoip2 API @param params [Hash] the url params that should be passed in the request

# File lib/geoip2/client.rb, line 38
def get(url, params = {}, faraday_options = {})
  params = params.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
  preform(@base_path + url, :get, params: params) do
    return connection(faraday_options).get(@base_path + url, params).body
  end
end
in_parallel() { || ... } click to toggle source

Does a parallel request to the api for all of the requests in the block

@example block

Geoip2.in_parallel do
  Geoip2.create_review(review_params)
  Geoip2.update_account(account_params)
end
# File lib/geoip2/client.rb, line 53
def in_parallel
  connection.in_parallel do
    yield
  end
end

Private Instance Methods

connection(faraday_options = {}) click to toggle source

@return an instance of Faraday initialized with all that this gem needs

# File lib/geoip2/client.rb, line 75
def connection(faraday_options = {})
  if @faraday_options != faraday_options
    options = {url: @base_url, parallel_manager: Typhoeus::Hydra.new(max_concurrency: @parallel_requests)}.merge(faraday_options)
    @faraday_options = faraday_options
    @connection = Faraday.new(options) do |conn|

      conn.request :basic_auth, @user, @password

      # Set the response to be mashified
      conn.response :mashify

      # Setting request and response to use JSON/XML
      conn.request :json
      conn.response :json

      # Set to use instrumentals to get time logs
      conn.use :instrumentation

      conn.adapter :typhoeus
    end
  end
  @connection
end
preform(url, type, params = {}, &block) click to toggle source

Preforms an HTTP request and notifies the ActiveSupport::Notifications

@private @param url [String] the url to which preform the request @param type [String]

# File lib/geoip2/client.rb, line 67
def preform(url, type, params = {}, &block)
  ActiveSupport::Notifications.instrument 'Geoip2', request: type, base_url: url, params: params do
    block.call
  end
end