class Greenfinch::Consumer
A Consumer
receives messages from a Greenfinch::Tracker
, and sends them elsewhere- probably to Greenfinch's analytics services, but can also enqueue them for later processing, log them to a file, or do whatever else you might find useful.
You can provide your own consumer to your Greenfinch::Trackers, either by passing in an argument with a send!
method when you construct the tracker, or just passing a block to Greenfinch::Tracker.new
tracker = Greenfinch::Tracker.new(YOUR_GREENFINCH_TOKEN) do |type, message| # type will be one of :event, :profile_update or :import @kestrel.set(ANALYTICS_QUEUE, [type, message].to_json) end
You can also instantiate the library consumers yourself, and use them wherever you would like. For example, the working that consumes the above queue might work like this:
greenfinch = Greenfinch::Consumer while true message_json = @kestrel.get(ANALYTICS_QUEUE) greenfinch.send!(*JSON.load(message_json)) end
Greenfinch::Consumer
is the default consumer. It sends each message, as the message is recieved, directly to Greenfinch
.
Public Class Methods
Create a Greenfinch::Consumer
. If you provide endpoint arguments, they will be used instead of the default Greenfinch
endpoints. This can be useful for proxying, debugging, or if you prefer not to use SSL for your events.
# File lib/greenfinch-ruby/consumer.rb, line 61 def initialize(events_endpoint=nil, update_endpoint=nil, groups_endpoint=nil, import_endpoint=nil) @events_endpoint = events_endpoint || 'https://event.kcd.partners/api/publish' @update_endpoint = update_endpoint || 'https://api.greenfinch.com/engage' @groups_endpoint = groups_endpoint || 'https://api.greenfinch.com/groups' @import_endpoint = import_endpoint || 'https://api.greenfinch.com/import' end
Public Instance Methods
Request takes an endpoint HTTP or HTTPS url, and a Hash of data to post to that url. It should return a pair of
- response code, response body
-
as the result of the response. Response code should be nil if the request never receives a response for some reason.
# File lib/greenfinch-ruby/consumer.rb, line 126 def request(endpoint, form_data, jwt_token=nil) uri = URI(endpoint) headers = { "jwt" => jwt_token, "content-type" => "application/json", "label" => "ruby" } client = Net::HTTP.new(uri.host, uri.port) client.use_ssl = true client.open_timeout = 10 client.continue_timeout = 10 client.read_timeout = 10 client.ssl_timeout = 10 Greenfinch.with_http(client) response = client.request_post uri.request_uri, form_data.to_json, headers [response.code, response.body] end
This method was deprecated in release 2.0.0, please use send! instead
# File lib/greenfinch-ruby/consumer.rb, line 114 def send(type, message) warn '[DEPRECATION] send has been deprecated as of release 2.0.0, please use send! instead' send!(type, message) end
Send the given string message to Greenfinch
. Type should be one of :event, :profile_update or :import, which will determine the endpoint.
Greenfinch::Consumer#send!
sends messages to Greenfinch
immediately on each call. To reduce the overall bandwidth you use when communicating with Greenfinch
, you can also use Greenfinch::BufferedConsumer
# File lib/greenfinch-ruby/consumer.rb, line 78 def send!(type, message) type = type.to_sym endpoint = { :event => @events_endpoint, :profile_update => @update_endpoint, :group_update => @groups_endpoint, :import => @import_endpoint, }[type] decoded_message = JSON.load(message) jwt_token = decoded_message["jwt_token"] service_name = decoded_message["service_name"] endpoint = "#{endpoint}/#{service_name}" form_data = decoded_message["data"] begin response_code, response_body = request(endpoint, form_data, jwt_token) rescue => e raise ConnectionError.new("Could not connect to Greenfinch, with error \"#{e.message}\".") end result = {} if response_code.to_i == 200 begin result = JSON.parse(response_body.to_s) rescue JSON::JSONError raise ServerError.new("Could not interpret Greenfinch server response: '#{response_body}'") end else raise ServerError.new("Could not write to Greenfinch, server responded with #{response_code} returning: '#{response_body}'") end end