class Algolia::Insights

Constants

MIN_RUBY_VERSION

Public Class Methods

new(app_id, api_key, region = 'us', params = {}) click to toggle source
# File lib/algolia/insights.rb, line 6
def initialize(app_id, api_key, region = 'us', params = {})
  headers = params[:headers] || {}
  @app_id   = app_id
  @api_key  = api_key
  @url = "https://insights.#{region}.algolia.io"
  @headers  = headers.merge({
    Protocol::HEADER_APP_ID  => app_id,
    Protocol::HEADER_API_KEY => api_key,
    'Content-Type'           => 'application/json; charset=utf-8',
    'User-Agent'             => ["Algolia for Ruby (#{::Algolia::VERSION})", "Ruby (#{RUBY_VERSION})"].join('; ')
                            })
end

Public Instance Methods

send_event(event) click to toggle source
# File lib/algolia/insights.rb, line 23
def send_event(event)
  send_events([event])
end
send_events(events) click to toggle source
# File lib/algolia/insights.rb, line 27
def send_events(events)
  perform_request(:POST, '/1/events', {}, { 'events' => events }.to_json)
end
user(user_token) click to toggle source
# File lib/algolia/insights.rb, line 19
def user(user_token)
  UserInsights.new(self, user_token)
end

Private Instance Methods

perform_request(method, path, params = {}, data = {}) click to toggle source
# File lib/algolia/insights.rb, line 33
def perform_request(method, path, params = {}, data = {})
  http = HTTPClient.new

  url = @url + path

  encoded_params = Hash[params.map { |k, v| [k.to_s, v.is_a?(Array) ? v.to_json : v] }]
  url << "?" + Protocol.to_query(encoded_params)

  response = case method
             when :POST
               http.post(url, { :body => data, :header => @headers })
             end

  if response.code / 100 != 2
    raise AlgoliaProtocolError.new(response.code, "Cannot #{method} to #{url}: #{response.content}")
  end

  JSON.parse(response.content)
end