require 'base64' class OEHClient::Realtime::Event URI_RT_TRACK_PART = "one/rt/track" URI_EVENTS = "events" # ONE attributes that are either in the request and/or returned in the response ONE_PARAM_URI = "uri" ONE_PARAM_TID = "tid" ONE_PARAM_PROPERTIES = "properties" # Property Hash Keys ONE_PROPERTIES_NAME = "name" ONE_PROPERTIES_VALUE = "value" # # -------[ CLASS ATTRIBUTES ] # attr_accessor :uri, # The full touchpoint/interaction URI used to post the interaction :space, # The configured OEHClient::Config::Space object that represents the OEH workspace :tid # The Thunderhead ID (TID) to which this interaction is related # class-level wrapper to post a new interaction to the OEH server using either the realtime or offline # API for an anonymous or known prospects/customer def self.post(site_key, uri, properties={}) # setup the baseline attributes hash with the site_key and interaction URI, which are the # minimal values needed for an interaction attributes = {:sk => site_key,:uri => uri} # create a new interaction using all attributes pass new_event = OEHClient::Realtime::Event.new(attributes) # Send the interaction for processing and return the instance of the interaction class new_event.send(properties) end # # -------[ INSTANCE METHODS ] # # constructor that allows the passed Ruby Hash to be mapped to the def initialize(attributes=nil) # set the instance attributes is the parameter hash is created if (!attributes.nil? && attributes.kind_of?(Hash)) @uri = attributes[:uri] if (attributes.has_key?(:uri)) @tid = attributes[:tid] if (attributes.has_key?(:tid)) @space = OEHClient::Config::SpaceManager.instance.get(attributes[:sk]) if (attributes.has_key?(:sk)) end end def send(properties={}) send_args = {:payload => ActiveSupport::JSON.encode(request_data(properties))} send_args[:header] = properties[:header] if (!properties.nil? && properties.has_key?(:header)) # send the POST or PUT methond along with the arguments to the OEHClient class map_response(OEHClient.send(OEHClient::Helper::Request::POST_METHOD.to_sym, request_url, nil, send_args)) self end private def request_url "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@space.host}/#{OEHClient::Realtime::Event::URI_RT_TRACK_PART}/#{@space.site_key}/#{OEHClient::Realtime::Event::URI_EVENTS}" end # request_data creates a properly formatted Hash object that represents the body of the request needed # for POST and PUT operations def request_data(passed_properties={}) # Initialize the parameters hash parameters ||= Hash.new # merge in the different parts of the request data if the values currently exist within # the instance of the class parameters.merge!({ONE_PARAM_URI => @uri}) if (!@uri.nil? && @uri.length > 0) # for each of the properties hash entry, build a name/value pair in the properties collection properties = Array.new # merge in the different parts of the request data if the values currently exist within # the instance of the class passed_properties.each do | key, value | properties << {ONE_PROPERTIES_NAME => key.to_s, ONE_PROPERTIES_VALUE => value} unless (key == :header) end # merge the properties (name / value) connections if there are additonal values to pass parameters.merge!({ONE_PARAM_PROPERTIES => properties}) if (properties.length > 0) # return the full parameter hash return(parameters) end # map_response takes the attributes returned in an interaction response and maps it to exiting # attributes in the current instance of the interaction object def map_response(response) body = response[:body] # Save the tid and session data if they where not previously used in the request @tid = body[ONE_PARAM_TID] if (@tid.nil? || (!@tid.blank? && @tid != body[ONE_PARAM_TID])) end end