lib/oehclient/interaction/interaction.rb in oeh-client-0.1.3 vs lib/oehclient/interaction/interaction.rb in oeh-client-0.1.4
- old
+ new
@@ -49,13 +49,13 @@
:sk => site_key,
:uri => uri
}
# conditionally merge the rest of the attributes if they are passed
- attributes.merge!(:timestamp => timestamp) unless (timestamp.nil?)
- attributes.merge!(:tid => tid) if (!tid.nil? && !tid.empty?)
- attributes.merge!(:ck => customer_key) if (!customer_key.nil? && !customer_key.empty?)
+ attributes.merge!(:timestamp => OEHClient::Helper::Timestamp.to_one_timestamp(timestamp)) unless(timestamp.blank?)
+ attributes.merge!(:tid => tid) unless(timestamp.blank?)
+ attributes.merge!(:ck => customer_key) unless(customer_key.blank?)
# create a new interaction using all attributes pass
new_interaction = OEHClient::Interaction::Interaction.new(attributes)
# Send the interaction for processing and return the instance of the interaction class
@@ -94,17 +94,17 @@
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))
- @customer_key = attributes[:ck] if (attributes.has_key?(:ck))
- @tid = attributes[:tid] if (attributes.has_key?(:tid))
- @session = attributes[:session] if (attributes.has_key?(:session))
- @timestamp = attributes[:timestamp] if (attributes.has_key?(:timestamp))
+ @uri = attributes[:uri] if (attributes.has_key?(:uri))
+ @customer_key = attributes[:ck] if (attributes.has_key?(:ck))
+ @tid = attributes[:tid] if (attributes.has_key?(:tid))
+ @session = attributes[:session] if (attributes.has_key?(:session))
+ @timestamp = OEHClient::Helper::Timestamp.to_one_timestamp(attributes[:timestamp]) if (attributes.has_key?(:timestamp))
- @space = OEHClient::Config::SpaceManager.instance.get(attributes[:sk]) if (attributes.has_key?(:sk))
+ @space = OEHClient::Config::SpaceManager.instance.get(attributes[:sk]) if (attributes.has_key?(:sk))
end
end
@@ -113,11 +113,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) : send_realtime(@space.token, parameters))
+ ((!@timestamp.nil?) ? send_offline(@space.token, parameters) : send_realtime(@space.token, parameters))
# return the current instance interacton
self
end
@@ -138,11 +138,11 @@
:uri => uri,
:ck => @customer_key,
:tid => @tid,
:session => @session,
:sk => @space.site_key,
- :timestamp => timestamp
+ :timestamp => OEHClient::Helper::Timestamp.to_one_timestamp(timestamp)
})
# Send the interaction for processing and return the current instance
new_interaction.send(parameters)
end
@@ -155,46 +155,54 @@
# 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?
+ #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, @space.token(), realtime_url, properties) unless properties.empty?
+
# return the current object
self
end
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, parameters={})
-
- response = OEHClient::Helper::Response.handle(OEHClient.post(token,
- OEHClient::Helper::Request.format_url(realtime_url, {:sk => @space.site_key}),
- nil,
- request_data(parameters))
- )
-
+ def send_realtime(token, properties={})
+ # POST the realtime interaction method
+ response = send_request(OEHClient::Helper::Request::POST_METHOD, token, 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)
-
- response = OEHClient::Helper::Response.handle(OEHClient.put(token,
- OEHClient::Helper::Request.format_url(offline_url, {:sk => @space.site_key}),
- nil,
- request_data))
+ def send_offline(token, properties={})
+ # PUT the offline interaction method
+ response = send_request(OEHClient::Helper::Request::PUT_METHOD, token, 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={})
+ # 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)
+ ))
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
@@ -276,10 +284,9 @@
# 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)
parameters.merge!({ONE_PARAM_CK => @customer_key}) if (!@customer_key.nil? && @customer_key.length > 0)
- parameters.merge!({ONE_PARAM_TID => @tid}) if (!@tid.nil? && @tid.length > 0)
parameters.merge!({ONE_PARAM_SESSION => @session}) if (!@session.nil? && @session.length > 0)
parameters.merge!({ONE_PARAM_TS => @timestamp}) if (!@timestamp.nil?)
# for each of the properties hash entry, build a name/value pair in the properties collection
\ No newline at end of file