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



108
109
110
111
112
# File 'lib/keen/client/publishing_methods.rb', line 108

def beacon_url(event_collection, properties)
  json = MultiJson.encode(properties)
  data = [json].pack("m0").tr("+/", "-_").gsub("\n", "")
  "#{self.api_url}#{api_event_collection_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
# 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)
  publish_body(
    api_event_collection_resource_path(event_collection),
    MultiJson.encode(properties),
    "publish")
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



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
89
90
91
92
93
94
95
96
97
98
# File 'lib/keen/client/publishing_methods.rb', line 59

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)
  http = http_client.post(
    :path => api_event_collection_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

#publish_batch(events) ⇒ Object

Publishes a batch of events See detailed documentation here keen.io/docs/api/reference/#post-request-body-example-of-batch-event-posting

and the values are arrays of hashes (event properties)

Parameters:

  • events
    • a hash where the keys are event collection names

Returns:

  • the JSON response from the API



42
43
44
45
46
47
48
49
# File 'lib/keen/client/publishing_methods.rb', line 42

def publish_batch(events)
  ensure_project_id!
  ensure_write_key!
  publish_body(
    api_events_resource_path,
    MultiJson.encode(events),
    "publish")
end