Class: Keen::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/keen/client.rb

Constant Summary collapse

CONFIG =
{
  :api_host => "api.keen.io",
  :api_port => 443,
  :api_version => "3.0",
  :api_sync_http_options => {
    :use_ssl => true,
    :verify_mode => OpenSSL::SSL::VERIFY_PEER,
    :verify_depth => 5,
    :ca_file => File.expand_path("../../../config/cacert.pem", __FILE__) },
  :api_async_http_options => {},
  :api_headers => {
    "Content-Type" => "application/json",
    "User-Agent" => "keen-gem v#{Keen::VERSION}"
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/keen/client.rb', line 33

def initialize(*args)
  options = args[0]
  unless options.is_a?(Hash)
    # deprecated, pass a hash of options instead
    options = {
      :project_id => args[0],
      :api_key => args[1],
    }.merge(args[2] || {})
  end

  @project_id, @api_key = options.values_at(
    :project_id, :api_key)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(_method, *args, &block) ⇒ Object (private)



133
134
135
# File 'lib/keen/client.rb', line 133

def method_missing(_method, *args, &block)
  CONFIG[_method.to_sym] || super
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/keen/client.rb', line 9

def api_key
  @api_key
end

#project_idObject

Returns the value of attribute project_id.



9
10
11
# File 'lib/keen/client.rb', line 9

def project_id
  @project_id
end

Instance Method Details

#add_event(event_name, properties, options = {}) ⇒ Object

deprecated



98
99
100
# File 'lib/keen/client.rb', line 98

def add_event(event_name, properties, options={})
  self.publish(event_name, properties, options)
end

#beacon_url(event_name, properties) ⇒ Object



27
28
29
30
31
# File 'lib/keen/client.rb', line 27

def beacon_url(event_name, properties)
  json = MultiJson.encode(properties)
  data = [json].pack("m0").tr("+/", "-_").gsub("\n", "")
  "https://#{api_host}/#{api_version}/projects/#{@project_id}/events/#{event_name}?api_key=#{@api_key}&data=#{data}"
end

#publish(event_name, properties) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/keen/client.rb', line 47

def publish(event_name, properties)
  check_configuration!
  begin
    response = Keen::HTTP::Sync.new(
      api_host, api_port, api_sync_http_options).post(
        :path => api_path(event_name),
        :headers => api_headers_with_auth,
        :body => MultiJson.encode(properties))
  rescue Exception => http_error
    raise HttpError.new("Couldn't connect to Keen IO: #{http_error.message}", http_error)
  end
  process_response(response.code, response.body.chomp)
end

#publish_async(event_name, properties) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/keen/client.rb', line 61

def publish_async(event_name, properties)
  check_configuration!

  deferrable = EventMachine::DefaultDeferrable.new

  http_client = Keen::HTTP::Async.new(api_host, api_port, api_async_http_options)
  http = http_client.post({
    :path => api_path(event_name),
    :headers => api_headers_with_auth,
    :body => MultiJson.encode(properties)
  })

  if defined?(EM::Synchrony)
    if http.error
      Keen.logger.warn("Couldn't connect to Keen IO: #{http.error}")
      raise HttpError.new("Couldn't connect to Keen IO: #{http.error}")
    else
      process_response(http.response_header.status, http.response.chomp)
    end
  else
    http.callback {
      begin
        response = process_response(http.response_header.status, http.response.chomp)
        deferrable.succeed(response)
      rescue Exception => e
        deferrable.fail(e)
      end
    }
    http.errback {
      Keen.logger.warn("Couldn't connect to Keen IO: #{http.error}")
      deferrable.fail(Error.new("Couldn't connect to Keen IO: #{http.error}"))
    }
    deferrable
  end
end