Module: Keen::Client::PublishingMethods

Included in:
Keen::Client
Defined in:
lib/keen/client/publishing_methods.rb

Instance Method Summary collapse

Instance Method Details

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

Deprecated.

Publishes a synchronous event

Parameters:

  • event_collection
  • event (Hash)

    properties

Returns:

  • the JSON response from the API



12
13
14
# File 'lib/keen/client/publishing_methods.rb', line 12

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

#beacon_url(event_collection, properties) ⇒ Object

Returns an encoded URL that will record an event. Useful in email situations. See detailed documentation here keen.io/docs/api/reference/#event-collection-resource

Parameters:

  • event_collection
  • event (Hash)

    properties

Returns:

  • a URL that will track an event when hit



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

def beacon_url(event_collection, properties)
  json = MultiJson.encode(properties)
  data = [json].pack("m0").tr("+/", "-_").gsub("\n", "")
  "#{self.api_url}#{api_event_resource_path(event_collection)}?api_key=#{self.write_key}&data=#{data}"
end

#publish(event_collection, properties) ⇒ Object

Publishes a synchronous event See detailed documentation here keen.io/docs/api/reference/#event-collection-resource

Parameters:

  • event_collection
  • event (Hash)

    properties

Returns:

  • the JSON response from the API



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/keen/client/publishing_methods.rb', line 24

def publish(event_collection, properties)
  ensure_project_id!
  ensure_write_key!
  check_event_data!(event_collection, properties)

  begin
    response = Keen::HTTP::Sync.new(
      self.api_url, api_sync_http_options).post(
        :path => api_event_resource_path(event_collection),
        :headers => api_headers(self.write_key, "sync"),
        :body => MultiJson.encode(properties))
  rescue Exception => http_error
    raise HttpError.new("HTTP publish failure: #{http_error.message}", http_error)
  end
  process_response(response.code, response.body.chomp)
end

#publish_async(event_collection, properties) ⇒ Object

Publishes an asynchronous event See detailed documentation here keen.io/docs/api/reference/#event-collection-resource

Parameters:

  • event_collection
  • event (Hash)

    properties

Returns:

  • a deferrable to apply callbacks to



49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/keen/client/publishing_methods.rb', line 49

def publish_async(event_collection, properties)
  ensure_project_id!
  ensure_write_key!
  check_event_data!(event_collection, properties)

  deferrable = EventMachine::DefaultDeferrable.new

  http_client = Keen::HTTP::Async.new(self.api_url, api_async_http_options)
  http = http_client.post(
    :path => api_event_resource_path(event_collection),
    :headers => api_headers(self.write_key, "async"),
    :body => MultiJson.encode(properties)
  )

  if defined?(EM::Synchrony)
    if http.error
      error = HttpError.new("HTTP em-synchrony publish_async error: #{http.error}")
      Keen.logger.error(error)
      raise 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)
      rescue Exception => e
        Keen.logger.error(e)
        deferrable.fail(e)
      end
      deferrable.succeed(response) if response
    }
    http.errback {
      error = Error.new("HTTP publish_async failure: #{http.error}")
      Keen.logger.error(error)
      deferrable.fail(error)
    }
    deferrable
  end
end