lib/oehclient/interaction/interaction.rb in oeh-client-0.1.4 vs lib/oehclient/interaction/interaction.rb in oeh-client-0.2.0

- old
+ new

@@ -1,8 +1,12 @@ class OEHClient::Interaction::Interaction + # + # -------[ CONSTANTS ] + # + # constants used to construct the restful API Request URL API_REALTIME = "/interaction" API_OFFLINE = "/offline" # ONE attributes that are either in the request and/or returned in the response ONE_PARAM_URI = "uri" @@ -19,10 +23,14 @@ ONE_RESPONSE_OPTIMIZATIONS = "optimizations" ONE_RESPONSE_TRACKERS = "trackers" ONE_RESPONSE_CAPTURES = "captures" + # + # -------[ 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 :timestamp, # A millisecond value representing a Time value of the interaction :customer_key, # The customer to which this interaction is related @@ -31,18 +39,20 @@ :optimizations, # The collection of optimizations that are relevent for the interaction :trackers, # The collection of tracking point data that are relevant for the interaction :captures, # The collection of capture point data that are relevant for the interactions - :errors + :cookies # The JSON object of cookies returned within a ONE Request - ### - ### ------------- Class Methods - ### + # + # -------[ CLASS METHODS ] + # + class << self - # post + # 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 post(site_key, uri, timestamp=nil, tid=nil, customer_key=nil, properties={}) # setup the baseline attributes hash with the site_key and interaction URI, which are the # minimal values needed for an interaction attributes = { @@ -85,14 +95,15 @@ end end - ### - ### ------------- Instance Methods - ### + # + # -------[ 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)) @@ -113,11 +124,11 @@ def send(parameters={}) # raise the MissingParameterException when one (or more) of the miminal parameters are missing raise OEHClient::Exception::MissingParameterException.new(missing_minimal_parameters) unless (minimal_parameters?) # call the appropriate method based on the existance of the timestamp value - ((!@timestamp.nil?) ? send_offline(@space.token, parameters) : send_realtime(@space.token, parameters)) + ((!@timestamp.nil?) ? send_offline(parameters) : send_realtime(parameters)) # return the current instance interacton self end @@ -155,84 +166,87 @@ # force the properties object to be an empty Hash if, for any reason, it is NIL properties ||= Hash.new # Call the PUT method to update the - #OEHClient::Helper::Response.handle(OEHClient.put(@space.token(), - # OEHClient::Helper::Request.format_url(realtime_url, {:sk => @space.site_key}), - # nil, - # request_data(properties))) unless properties.empty? + send_request(OEHClient::Helper::Request::PUT_METHOD, realtime_url, properties) unless properties.empty? - send_request(OEHClient::Helper::Request::PUT_METHOD, @space.token(), realtime_url, properties) unless properties.empty? - # return the current object self end + # + # -------[ PRIVATE METHODS ] + # + private # send_realtime posts a new interaction occuring at the moment (in realtime). The response attributes # are mapped to the current instance attributes for all valid requests - def send_realtime(token, properties={}) + def send_realtime(properties={}) # POST the realtime interaction method - response = send_request(OEHClient::Helper::Request::POST_METHOD, token, realtime_url, properties) + response = send_request(OEHClient::Helper::Request::POST_METHOD, realtime_url, properties) # map the response message to the current instance attributes map_response(response) end # send_offline posts a historic interaction, using a specified timestamp - def send_offline(token, properties={}) + def send_offline(properties={}) # PUT the offline interaction method - response = send_request(OEHClient::Helper::Request::PUT_METHOD, token, offline_url, properties) + response = send_request(OEHClient::Helper::Request::PUT_METHOD, offline_url, properties) # map the response message to the current instance attributes map_response(response) end # send_request acts as the wrapper to send all client requests to the ONE server in a unified manner - def send_request(method, token, url, properties={}) + def send_request(method, url, properties={}) # set the URL parameters for the site_key and the tid, of the value exists url_parameters = {:sk => @space.site_key} url_parameters.merge!({:tid => @tid}) unless (@tid.blank?) # send the POST or PUT methond along with the arguments to the OEHClient class - OEHClient::Helper::Response.handle(OEHClient.send(method.downcase.to_sym, - token, - OEHClient::Helper::Request.format_url(url, url_parameters), - nil, - request_data(properties) - )) + OEHClient.send(method.downcase.to_sym, + url, + @space.oauth_consumer, + {:params => url_parameters, :payload => ActiveSupport::JSON.encode(request_data(properties))}) 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 = response[ONE_PARAM_TID] if (@tid.nil? || (!@tid.blank? && @tid != response[ONE_PARAM_TID])) - @session = response[ONE_PARAM_SESSION] if @session.nil? + @tid = body[ONE_PARAM_TID] if (@tid.nil? || (!@tid.blank? && @tid != body[ONE_PARAM_TID])) + @session = body[ONE_PARAM_SESSION] if @session.nil? # capture the optimizations returned from the request and map it to the OEHClient::Interaction::Optimization # class # initialize the optimizations collection if it is null @optimizations ||= Array.new # map each of the optimizations to the OEHClient::Interaction::Optmization class - response[ONE_RESPONSE_OPTIMIZATIONS].each do | response_optimization | + body[ONE_RESPONSE_OPTIMIZATIONS].each do | response_optimization | @optimizations << OEHClient::Interaction::Optimization.create(response_optimization) end + # store the cookies passed back by the system + @cookies = response[:cookies] + # capture the trackers returned from the request and mpt it to the OEHClient::Interaction::Tracker # class # TODO: Create OEHClient::Interaction::Tracker class - @trackers = response[ONE_RESPONSE_TRACKERS] + @trackers = body[ONE_RESPONSE_TRACKERS] # capture the capture points returned from the request and map it to the OEHClient::Interaction::Capture # class # TODO: Create OEHClient::Interaction::Capture class - @captures = response[ONE_RESPONSE_CAPTURES] + @captures = body[ONE_RESPONSE_CAPTURES] + + end # minimal_parameters? determines if the minimal number of request parameters (uri & site_key) are # present in the current instance of the interaction class. This is a helper method that is used \ No newline at end of file