oeh-client 2.2.2 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/oehclient/config/space.rb +114 -0
- data/lib/oehclient/config/space_manager.rb +55 -0
- data/lib/oehclient/config.rb +11 -0
- data/lib/oehclient/data/node.rb +66 -0
- data/lib/oehclient/data/structure.rb +82 -0
- data/lib/oehclient/data.rb +10 -0
- data/lib/oehclient/exception.rb +64 -0
- data/lib/oehclient/helper.rb +97 -0
- data/lib/oehclient/meta/entity.rb +97 -0
- data/lib/oehclient/meta/interaction.rb +14 -0
- data/lib/oehclient/meta/session.rb +36 -0
- data/lib/oehclient/meta/touchpoint.rb +36 -0
- data/lib/oehclient/meta/workspace.rb +58 -0
- data/lib/oehclient/meta/workspace_entity.rb +13 -0
- data/lib/oehclient/meta.rb +14 -0
- data/lib/oehclient/realtime/event.rb +114 -0
- data/lib/oehclient/realtime/interaction.rb +355 -0
- data/lib/oehclient/realtime/optimization.rb +55 -0
- data/lib/oehclient/realtime/response.rb +74 -0
- data/lib/oehclient/realtime.rb +11 -0
- data/lib/oehclient/version.rb +3 -0
- metadata +22 -1
@@ -0,0 +1,36 @@
|
|
1
|
+
class OEHClient::Meta::Touchpoint < OEHClient::Data::Node
|
2
|
+
include OEHClient::Meta::WorkspaceEntity
|
3
|
+
|
4
|
+
# tell the workspace entity module how to reference within ONE
|
5
|
+
self.entity_uri_stem = "touchpoints"
|
6
|
+
|
7
|
+
# create a new instance of the class using the data set and assig the workspace
|
8
|
+
def self.create(workspace, data)
|
9
|
+
# create from the past data structure
|
10
|
+
touchpoint_instance = OEHClient::Meta::Touchpoint.new(data)
|
11
|
+
# assign the local workspace object
|
12
|
+
touchpoint_instance.workspace = workspace
|
13
|
+
# return the instance
|
14
|
+
touchpoint_instance
|
15
|
+
end # self.create
|
16
|
+
|
17
|
+
# retrieve the full collection of touchpoints from the curent workspace
|
18
|
+
def interactions()
|
19
|
+
# initialize the collection array
|
20
|
+
interaction_collection = Array.new
|
21
|
+
# retrieve the collection from ONE using the workspace reference and current touchpoint
|
22
|
+
OEHClient::Meta::Interaction.get_collection(self.class.session, space: workspace.id, params: {:site => id}).each do | interaction_item |
|
23
|
+
# create the instance of the interaction and add it to the collection
|
24
|
+
interaction_collection << OEHClient::Meta::Interaction.create(workspace, interaction_item)
|
25
|
+
end
|
26
|
+
# return the collection to calling process
|
27
|
+
interaction_collection
|
28
|
+
end # end def interactions
|
29
|
+
|
30
|
+
# retrieve a single touchpoint from the current workspace based on the name
|
31
|
+
def interaction(interaction_name)
|
32
|
+
# create and return the instance of an interaction based on the name, current workspace, and current touchpoint
|
33
|
+
OEHClient::Meta::Interaction.create(workspace, OEHClient::Meta::Interaction.find_by_name(self.class.session, interaction_name, space: workspace.id, params: {:site => id}))
|
34
|
+
end # def interaction
|
35
|
+
|
36
|
+
end # class OEHClient::Meta::Touchpoint
|
@@ -0,0 +1,58 @@
|
|
1
|
+
class OEHClient::Meta::Workspace < OEHClient::Data::Node
|
2
|
+
extend OEHClient::Meta::Entity
|
3
|
+
|
4
|
+
# set the ONE entity URI Stem for workspace requests
|
5
|
+
self.entity_uri_stem = "workspaces"
|
6
|
+
|
7
|
+
###
|
8
|
+
### ------------- Constants
|
9
|
+
###
|
10
|
+
|
11
|
+
ONE_WORKSPACE_SITE_KEY = "siteKey"
|
12
|
+
|
13
|
+
|
14
|
+
###
|
15
|
+
### ------------- Attributes
|
16
|
+
###
|
17
|
+
|
18
|
+
###
|
19
|
+
### ------------- Class Methods
|
20
|
+
###
|
21
|
+
|
22
|
+
def self.find_by_key(active_session, site_key, **args)
|
23
|
+
# default the workspace instance NIL
|
24
|
+
workspace_instance = nil
|
25
|
+
# get the list of workspaces from the thinstance
|
26
|
+
workspace_data = get(active_session, {:name => ONE_WORKSPACE_SITE_KEY, :value => site_key}, **args)
|
27
|
+
unless (workspace_data.blank?)
|
28
|
+
# convert the the workspace instance into a NEW instance of the OEHClient::Meta::Workspace instance
|
29
|
+
workspace_instance = OEHClient::Meta::Workspace.new(workspace_data)
|
30
|
+
# set the session object
|
31
|
+
# workspace_instance.session = session
|
32
|
+
# return the workspace instance
|
33
|
+
workspace_instance
|
34
|
+
end # unless workspace_data.blank?
|
35
|
+
end # def self.find_by_key
|
36
|
+
|
37
|
+
|
38
|
+
###
|
39
|
+
### ------------- Instance Methods
|
40
|
+
###
|
41
|
+
|
42
|
+
# retrieve the full collection of touchpoints from the curent workspace
|
43
|
+
def touchpoints()
|
44
|
+
touchpoint_collection = Array.new
|
45
|
+
OEHClient::Meta::Touchpoint.get_collection(self.class.session, space: id).each do | touchpoint_item |
|
46
|
+
touchpoint_collection << OEHClient::Meta::Touchpoint.create(self, touchpoint_item)
|
47
|
+
end
|
48
|
+
touchpoint_collection
|
49
|
+
end
|
50
|
+
|
51
|
+
# retrieve a single touchpoint from the current workspace based on the name
|
52
|
+
def touchpoint(touchpoint_name)
|
53
|
+
OEHClient::Meta::Touchpoint.create(self, OEHClient::Meta::Touchpoint.find_by_name(self.class.session, touchpoint_name, space: id))
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
module OEHClient
|
3
|
+
|
4
|
+
module Meta
|
5
|
+
end
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
require File.dirname(__FILE__) + '/meta/entity'
|
10
|
+
require File.dirname(__FILE__) + '/meta/workspace_entity'
|
11
|
+
require File.dirname(__FILE__) + '/meta/session'
|
12
|
+
require File.dirname(__FILE__) + '/meta/workspace'
|
13
|
+
require File.dirname(__FILE__) + '/meta/touchpoint'
|
14
|
+
require File.dirname(__FILE__) + '/meta/interaction'
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
class OEHClient::Realtime::Event
|
4
|
+
|
5
|
+
URI_RT_TRACK_PART = "one/rt/track"
|
6
|
+
URI_EVENTS = "events"
|
7
|
+
# ONE attributes that are either in the request and/or returned in the response
|
8
|
+
ONE_PARAM_URI = "uri"
|
9
|
+
ONE_PARAM_TID = "tid"
|
10
|
+
ONE_PARAM_PROPERTIES = "properties"
|
11
|
+
# Property Hash Keys
|
12
|
+
ONE_PROPERTIES_NAME = "name"
|
13
|
+
ONE_PROPERTIES_VALUE = "value"
|
14
|
+
|
15
|
+
#
|
16
|
+
# -------[ CLASS ATTRIBUTES ]
|
17
|
+
#
|
18
|
+
|
19
|
+
attr_accessor :uri, # The full touchpoint/interaction URI used to post the interaction
|
20
|
+
:space, # The configured OEHClient::Config::Space object that represents the OEH workspace
|
21
|
+
:tid # The Thunderhead ID (TID) to which this interaction is related
|
22
|
+
|
23
|
+
|
24
|
+
# class-level wrapper to post a new interaction to the OEH server using either the realtime or offline
|
25
|
+
# API for an anonymous or known prospects/customer
|
26
|
+
def self.post(site_key, uri, properties={})
|
27
|
+
|
28
|
+
# setup the baseline attributes hash with the site_key and interaction URI, which are the
|
29
|
+
# minimal values needed for an interaction
|
30
|
+
attributes = {:sk => site_key,:uri => uri}
|
31
|
+
|
32
|
+
# create a new interaction using all attributes pass
|
33
|
+
new_event = OEHClient::Realtime::Event.new(attributes)
|
34
|
+
# Send the interaction for processing and return the instance of the interaction class
|
35
|
+
new_event.send(properties)
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# -------[ INSTANCE METHODS ]
|
41
|
+
#
|
42
|
+
|
43
|
+
# constructor that allows the passed Ruby Hash to be mapped to the
|
44
|
+
def initialize(attributes=nil)
|
45
|
+
|
46
|
+
# set the instance attributes is the parameter hash is created
|
47
|
+
if (!attributes.nil? && attributes.kind_of?(Hash))
|
48
|
+
|
49
|
+
@uri = attributes[:uri] if (attributes.has_key?(:uri))
|
50
|
+
@tid = attributes[:tid] if (attributes.has_key?(:tid))
|
51
|
+
|
52
|
+
@space = OEHClient::Config::SpaceManager.instance.get(attributes[:sk]) if (attributes.has_key?(:sk))
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def send(properties={})
|
59
|
+
|
60
|
+
send_args = {:payload => ActiveSupport::JSON.encode(request_data(properties))}
|
61
|
+
send_args[:header] = properties[:header] if (!properties.nil? && properties.has_key?(:header))
|
62
|
+
|
63
|
+
# send the POST or PUT methond along with the arguments to the OEHClient class
|
64
|
+
map_response(OEHClient.send(OEHClient::Helper::Request::POST_METHOD.to_sym,
|
65
|
+
request_url,
|
66
|
+
nil,
|
67
|
+
send_args))
|
68
|
+
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def request_url
|
75
|
+
"#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@space.host}/#{OEHClient::Realtime::Event::URI_RT_TRACK_PART}/#{@space.site_key}/#{OEHClient::Realtime::Event::URI_EVENTS}"
|
76
|
+
end
|
77
|
+
|
78
|
+
# request_data creates a properly formatted Hash object that represents the body of the request needed
|
79
|
+
# for POST and PUT operations
|
80
|
+
def request_data(passed_properties={})
|
81
|
+
|
82
|
+
# Initialize the parameters hash
|
83
|
+
parameters ||= Hash.new
|
84
|
+
|
85
|
+
# merge in the different parts of the request data if the values currently exist within
|
86
|
+
# the instance of the class
|
87
|
+
parameters.merge!({ONE_PARAM_URI => @uri}) if (!@uri.nil? && @uri.length > 0)
|
88
|
+
|
89
|
+
# for each of the properties hash entry, build a name/value pair in the properties collection
|
90
|
+
properties = Array.new
|
91
|
+
# merge in the different parts of the request data if the values currently exist within
|
92
|
+
# the instance of the class
|
93
|
+
passed_properties.each do | key, value |
|
94
|
+
properties << {ONE_PROPERTIES_NAME => key.to_s, ONE_PROPERTIES_VALUE => value} unless (key == :header)
|
95
|
+
end
|
96
|
+
# merge the properties (name / value) connections if there are additonal values to pass
|
97
|
+
parameters.merge!({ONE_PARAM_PROPERTIES => properties}) if (properties.length > 0)
|
98
|
+
|
99
|
+
# return the full parameter hash
|
100
|
+
return(parameters)
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
# map_response takes the attributes returned in an interaction response and maps it to exiting
|
105
|
+
# attributes in the current instance of the interaction object
|
106
|
+
def map_response(response)
|
107
|
+
|
108
|
+
body = response[:body]
|
109
|
+
|
110
|
+
# Save the tid and session data if they where not previously used in the request
|
111
|
+
@tid = body[ONE_PARAM_TID] if (@tid.nil? || (!@tid.blank? && @tid != body[ONE_PARAM_TID]))
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,355 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
class OEHClient::Realtime::Interaction
|
4
|
+
|
5
|
+
#
|
6
|
+
# -------[ CONSTANTS ]
|
7
|
+
#
|
8
|
+
|
9
|
+
# constants used to construct the restful API Request URL
|
10
|
+
API_REALTIME = "/interaction"
|
11
|
+
API_OFFLINE = "/offline"
|
12
|
+
# ONE attributes that are either in the request and/or returned in the response
|
13
|
+
ONE_PARAM_URI = "uri"
|
14
|
+
ONE_PARAM_CK = "customerKey"
|
15
|
+
ONE_PARAM_TID = "tid"
|
16
|
+
ONE_PARAM_SESSION = "session"
|
17
|
+
ONE_PARAM_SK = "sk"
|
18
|
+
ONE_PARAM_TS = "timestamp"
|
19
|
+
ONE_PARAM_PROPERTIES = "properties"
|
20
|
+
# Property Hash Keys
|
21
|
+
ONE_PROPERTIES_NAME = "name"
|
22
|
+
ONE_PROPERTIES_VALUE = "value"
|
23
|
+
# Collection objects returned in the response
|
24
|
+
ONE_RESPONSE_OPTIMIZATIONS = "optimizations"
|
25
|
+
ONE_RESPONSE_OPTIMIZATION_DATA = "data"
|
26
|
+
ONE_RESPONSE_ACTIONS = "actions"
|
27
|
+
ONE_RESPONSE_TRACKERS = "trackers"
|
28
|
+
ONE_RESPONSE_CAPTURES = "captures"
|
29
|
+
|
30
|
+
|
31
|
+
#
|
32
|
+
# -------[ CLASS ATTRIBUTES ]
|
33
|
+
#
|
34
|
+
|
35
|
+
attr_accessor :uri, # The full touchpoint/interaction URI used to post the interaction
|
36
|
+
:space, # The configured OEHClient::Config::Space object that represents the OEH workspace
|
37
|
+
|
38
|
+
:timestamp, # A millisecond value representing a Time value of the interaction
|
39
|
+
:keyname, # The customer keyname to use based on multi-key configuration. Default is customerKey
|
40
|
+
:customer_key, # The customer to which this interaction is related
|
41
|
+
:tid, # The Thunderhead ID (TID) to which this interaction is related
|
42
|
+
:session, # The session id to which this interaction is related
|
43
|
+
|
44
|
+
:optimizations, # The collection of optimizations that are relevent for the interaction
|
45
|
+
:trackers, # The collection of tracking point data that are relevant for the interaction
|
46
|
+
:captures, # The collection of capture point data that are relevant for the interactions
|
47
|
+
|
48
|
+
:cookies # The JSON object of cookies returned within a ONE Request
|
49
|
+
|
50
|
+
#
|
51
|
+
# -------[ CLASS METHODS ]
|
52
|
+
#
|
53
|
+
|
54
|
+
class << self
|
55
|
+
|
56
|
+
# class-level wrapper to post a new interaction to the OEH server using either the realtime or offline
|
57
|
+
# API for an anonymous or known prospects/customer
|
58
|
+
def post(site_key, uri, timestamp=nil, tid=nil, customer_key=nil, properties={})
|
59
|
+
|
60
|
+
# setup the baseline attributes hash with the site_key and interaction URI, which are the
|
61
|
+
# minimal values needed for an interaction
|
62
|
+
attributes = {
|
63
|
+
:sk => site_key,
|
64
|
+
:uri => uri
|
65
|
+
}
|
66
|
+
|
67
|
+
# conditionally merge the rest of the attributes if they are passed
|
68
|
+
attributes.merge!(:timestamp => OEHClient::Helper::Timestamp.to_one_timestamp(timestamp)) unless(timestamp.blank?)
|
69
|
+
attributes.merge!(:tid => tid) unless(timestamp.blank?)
|
70
|
+
|
71
|
+
if (customer_key.is_a?(Hash))
|
72
|
+
attributes.merge!(:ck => customer_key[:value]) if (customer_key.has_key?(:value))
|
73
|
+
attributes.merge!(:keyname => customer_key[:keyname]) if (customer_key.has_key?(:keyname))
|
74
|
+
else
|
75
|
+
attributes.merge!(:ck => customer_key) unless(customer_key.blank?)
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
# create a new interaction using all attributes pass
|
81
|
+
new_interaction = OEHClient::Realtime::Interaction.new(attributes)
|
82
|
+
# Send the interaction for processing and return the instance of the interaction class
|
83
|
+
new_interaction.send(properties)
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
# class-level wrapper to create a new instance of the OEHClient::Realtime::Interaction class, call the
|
88
|
+
# send_update method, and return the resulting instance of the same class
|
89
|
+
def update(site_key, uri, properties={}, tid=nil, customer_key=nil)
|
90
|
+
|
91
|
+
# setup the baseline attributes hash with the site_key and interaction URI, which are the
|
92
|
+
# minimal values needed for an interaction
|
93
|
+
attributes = {
|
94
|
+
:sk => site_key,
|
95
|
+
:uri => uri
|
96
|
+
}
|
97
|
+
|
98
|
+
# conditionally merge the rest of the attributes if they are passed
|
99
|
+
attributes.merge!(:tid => tid) if (!tid.nil? && !tid.empty?)
|
100
|
+
|
101
|
+
if (customer_key.is_a?(Hash))
|
102
|
+
attributes.merge!(:ck => customer_key[:value]) if (customer_key.has_key?(:value))
|
103
|
+
attributes.merge!(:keyname => customer_key[:keyname]) if (customer_key.has_key?(:keyname))
|
104
|
+
else
|
105
|
+
attributes.merge!(:ck => customer_key) unless(customer_key.blank?)
|
106
|
+
end
|
107
|
+
|
108
|
+
# create a new interaction using all parameters pass
|
109
|
+
new_interaction = OEHClient::Realtime::Interaction.new(attributes)
|
110
|
+
# send the update and return the current object
|
111
|
+
new_interaction.send_update(properties)
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# -------[ INSTANCE METHODS ]
|
119
|
+
#
|
120
|
+
|
121
|
+
# constructor that allows the passed Ruby Hash to be mapped to the
|
122
|
+
def initialize(attributes=nil)
|
123
|
+
|
124
|
+
# set the instance attributes is the parameter hash is created
|
125
|
+
if (!attributes.nil? && attributes.kind_of?(Hash))
|
126
|
+
|
127
|
+
@uri = attributes[:uri] if (attributes.has_key?(:uri))
|
128
|
+
@keyname = attributes[:keyname] if (attributes.has_key?(:keyname))
|
129
|
+
@customer_key = attributes[:ck] if (attributes.has_key?(:ck))
|
130
|
+
@tid = attributes[:tid] if (attributes.has_key?(:tid))
|
131
|
+
@session = attributes[:session] if (attributes.has_key?(:session))
|
132
|
+
@timestamp = OEHClient::Helper::Timestamp.to_one_timestamp(attributes[:timestamp]) if (attributes.has_key?(:timestamp))
|
133
|
+
|
134
|
+
@space = OEHClient::Config::SpaceManager.instance.get(attributes[:sk]) if (attributes.has_key?(:sk))
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
# send() will post a new interaction using either the realtime (current timestamp) or the offline (historic)
|
141
|
+
# API interface based on the existence of a timestamp value
|
142
|
+
def send(parameters={})
|
143
|
+
# raise the MissingParameterException when one (or more) of the miminal parameters are missing
|
144
|
+
raise OEHClient::Exception::MissingParameterException.new(missing_minimal_parameters) unless (minimal_parameters?)
|
145
|
+
|
146
|
+
# call the appropriate method based on the existance of the timestamp value
|
147
|
+
#((!@timestamp.nil?) ? send_offline(parameters) : send_realtime(parameters))
|
148
|
+
send_realtime(parameters)
|
149
|
+
# return the current instance interacton
|
150
|
+
self
|
151
|
+
end
|
152
|
+
|
153
|
+
# send_new posts a new interaction using the existing instance data, but for a different touchpoint
|
154
|
+
# URI. The method returns a new instance of the OEHClient::Realtime::Interaction class
|
155
|
+
def send_new(uri, timestamp=nil, parameters={})
|
156
|
+
# raise the MissingParameterException when one (or more) of the miminal parameters are missing
|
157
|
+
raise OEHClient::Exception::MissingParameterException.new(missing_minimal_parameters) unless (minimal_parameters?)
|
158
|
+
|
159
|
+
# protect against NIL by creating a new Hash object if the parameters, for any reason is
|
160
|
+
# NIL
|
161
|
+
parameters ||= Hash.new
|
162
|
+
|
163
|
+
# create a new interaction using all parameters from the existing other than the new touchpoint
|
164
|
+
# URI and timestamp of the current Interaction instance. The method can be used to submit new
|
165
|
+
# requests for the same customer, tid, & session
|
166
|
+
new_interaction = OEHClient::Realtime::Interaction.new({
|
167
|
+
:uri => uri,
|
168
|
+
:ck => @customer_key,
|
169
|
+
:tid => @tid,
|
170
|
+
:session => @session,
|
171
|
+
:sk => @space.site_key,
|
172
|
+
:timestamp => OEHClient::Helper::Timestamp.to_one_timestamp(timestamp)
|
173
|
+
})
|
174
|
+
|
175
|
+
# Send the interaction for processing and return the current instance
|
176
|
+
new_interaction.send(parameters)
|
177
|
+
end
|
178
|
+
|
179
|
+
# send_update allows the system to update the capture and tracking properties that are defined as
|
180
|
+
# part of the existing interaction
|
181
|
+
def send_update(properties={})
|
182
|
+
# raise the MissingParameterException when one (or more) of the miminal parameters are missing
|
183
|
+
raise OEHClient::Exception::MissingParameterException.new(missing_minimal_parameters) unless (minimal_parameters?)
|
184
|
+
|
185
|
+
# force the properties object to be an empty Hash if, for any reason, it is NIL
|
186
|
+
properties ||= Hash.new
|
187
|
+
|
188
|
+
# Call the PUT method to update the
|
189
|
+
send_request(OEHClient::Helper::Request::PUT_METHOD, realtime_url, properties) unless properties.empty?
|
190
|
+
|
191
|
+
# return the current object
|
192
|
+
self
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
#
|
197
|
+
# -------[ PRIVATE METHODS ]
|
198
|
+
#
|
199
|
+
|
200
|
+
private
|
201
|
+
|
202
|
+
|
203
|
+
# send_realtime posts a new interaction occuring at the moment (in realtime). The response attributes
|
204
|
+
# are mapped to the current instance attributes for all valid requests
|
205
|
+
def send_realtime(properties={})
|
206
|
+
# Put timestamp in the header if it has been provides
|
207
|
+
properties[:header] = {'X-ONE-Timestamp' => @timestamp}.merge!(OEHClient::Helper::Request.default_JSON_header()) unless (@timestamp.nil?)
|
208
|
+
# POST the realtime interaction method
|
209
|
+
response = send_request(OEHClient::Helper::Request::POST_METHOD, realtime_url, properties)
|
210
|
+
# map the response message to the current instance attributes
|
211
|
+
map_response(response)
|
212
|
+
end
|
213
|
+
|
214
|
+
# send_offline posts a historic interaction, using a specified timestamp
|
215
|
+
def send_offline(properties={})
|
216
|
+
# PUT the offline interaction method
|
217
|
+
response = send_request(OEHClient::Helper::Request::PUT_METHOD, offline_url, properties)
|
218
|
+
# map the response message to the current instance attributes
|
219
|
+
map_response(response)
|
220
|
+
end
|
221
|
+
|
222
|
+
# send_request acts as the wrapper to send all client requests to the ONE server in a unified manner
|
223
|
+
def send_request(method, url, properties={})
|
224
|
+
# set the URL parameters for the site_key and the tid, of the value exists
|
225
|
+
url_parameters = {:sk => @space.site_key}
|
226
|
+
url_parameters.merge!({:tid => @tid}) unless (@tid.blank?)
|
227
|
+
#url_parameters.merge!({:timestamp => @timestamp}) unless (@timestamp.nil?)
|
228
|
+
|
229
|
+
send_args = {:params => url_parameters, :payload => ActiveSupport::JSON.encode(request_data(properties))}
|
230
|
+
send_args[:header] = properties[:header] if (!properties.nil? && properties.has_key?(:header))
|
231
|
+
|
232
|
+
# send the POST or PUT methond along with the arguments to the OEHClient class
|
233
|
+
OEHClient.send(method.downcase.to_sym,
|
234
|
+
url,
|
235
|
+
@space.oauth_consumer,
|
236
|
+
send_args)
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
# map_response takes the attributes returned in an interaction response and maps it to exiting
|
241
|
+
# attributes in the current instance of the interaction object
|
242
|
+
def map_response(response)
|
243
|
+
|
244
|
+
body = response[:body]
|
245
|
+
|
246
|
+
# Save the tid and session data if they where not previously used in the request
|
247
|
+
@tid = body[ONE_PARAM_TID] if (@tid.nil? || (!@tid.blank? && @tid != body[ONE_PARAM_TID]))
|
248
|
+
@session = body[ONE_PARAM_SESSION] if @session.nil?
|
249
|
+
|
250
|
+
# capture the optimizations returned from the request and map it to the OEHClient::Realtime::Optimization
|
251
|
+
# class
|
252
|
+
|
253
|
+
# initialize the optimizations collection if it is null
|
254
|
+
@optimizations ||= Array.new
|
255
|
+
# map each of the optimizations to the OEHClient::Realtime::Optmization class
|
256
|
+
body[ONE_RESPONSE_OPTIMIZATIONS].each do | response_optimization |
|
257
|
+
# decode the data of the optimization
|
258
|
+
optimization_data = ActiveSupport::JSON.decode(Base64.decode64(response_optimization[ONE_RESPONSE_OPTIMIZATION_DATA]))
|
259
|
+
# get the actions for each optimization
|
260
|
+
optimization_data[ONE_RESPONSE_ACTIONS].each do | one_action |
|
261
|
+
@optimizations << OEHClient::Realtime::Optimization.create(self, one_action)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
# store the cookies passed back by the system
|
266
|
+
@cookies = response[:cookies]
|
267
|
+
|
268
|
+
# capture the trackers returned from the request and mpt it to the OEHClient::Realtime::Tracker
|
269
|
+
# class
|
270
|
+
# TODO: Create OEHClient::Realtime::Tracker class
|
271
|
+
@trackers = body[ONE_RESPONSE_TRACKERS]
|
272
|
+
|
273
|
+
# capture the capture points returned from the request and map it to the OEHClient::Realtime::Capture
|
274
|
+
# class
|
275
|
+
# TODO: Create OEHClient::Realtime::Capture class
|
276
|
+
@captures = body[ONE_RESPONSE_CAPTURES]
|
277
|
+
|
278
|
+
|
279
|
+
|
280
|
+
end
|
281
|
+
|
282
|
+
# minimal_parameters? determines if the minimal number of request parameters (uri & site_key) are
|
283
|
+
# present in the current instance of the interaction class. This is a helper method that is used
|
284
|
+
# before making any request
|
285
|
+
def minimal_parameters?()
|
286
|
+
((!@uri.nil? && !@uri.empty?) && !@space.nil?)
|
287
|
+
end
|
288
|
+
|
289
|
+
# missing_minimal_parameters returns an array of the minimal attributes that are missing from the current
|
290
|
+
# instance of OEHClient::Realtime::Interaction class
|
291
|
+
def missing_minimal_parameters
|
292
|
+
|
293
|
+
missing_parameters = []
|
294
|
+
|
295
|
+
missing_parameters << "site_key" if (!minimal_parameters? && @site_key.nil?)
|
296
|
+
missing_parameters << "uri" if (!minimal_parameters? && @uri.nil?)
|
297
|
+
|
298
|
+
missing_parameters
|
299
|
+
|
300
|
+
end
|
301
|
+
|
302
|
+
# request_url returns the base of the request URL used to make either a realtime or offline request
|
303
|
+
# through published API
|
304
|
+
def request_url()
|
305
|
+
"#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@space.host}#{OEHClient::Helper::Request::ONE_URI_PART}#{OEHClient::Helper::Request::API_URI_PART}#{OEHClient::Helper::Request::API_VERSION}"
|
306
|
+
end
|
307
|
+
|
308
|
+
# realtime_url is the interaction part of the API URI
|
309
|
+
def realtime_url()
|
310
|
+
"#{request_url}#{API_REALTIME}"
|
311
|
+
end
|
312
|
+
|
313
|
+
# offline_url appends the /offiline URI part to add support for historic data loading of interactions
|
314
|
+
def offline_url()
|
315
|
+
"#{realtime_url}#{API_OFFLINE}"
|
316
|
+
end
|
317
|
+
|
318
|
+
# request_data creates a properly formatted Hash object that represents the body of the request needed
|
319
|
+
# for POST and PUT operations
|
320
|
+
def request_data(passed_properties={})
|
321
|
+
|
322
|
+
# protect agains a NIL value in the passed_properties Hash
|
323
|
+
passed_properties ||= Hash.new
|
324
|
+
|
325
|
+
# Initialize the parameters hash
|
326
|
+
parameters ||= Hash.new
|
327
|
+
|
328
|
+
# merge in the different parts of the request data if the values currently exist within
|
329
|
+
# the instance of the class
|
330
|
+
parameters.merge!({ONE_PARAM_URI => @uri}) if (!@uri.nil? && @uri.length > 0)
|
331
|
+
|
332
|
+
parameters.merge!({"customerKeyName" => @keyname}) if (!@keyname.nil? && @keyname.length > 0)
|
333
|
+
parameters.merge!({"customerKey" => @customer_key}) if (!@customer_key.nil? && @customer_key.length > 0)
|
334
|
+
|
335
|
+
|
336
|
+
parameters.merge!({ONE_PARAM_SESSION => @session}) if (!@session.nil? && @session.length > 0)
|
337
|
+
#parameters.merge!({ONE_PARAM_TS => @timestamp}) if (!@timestamp.nil?)
|
338
|
+
|
339
|
+
# for each of the properties hash entry, build a name/value pair in the properties collection
|
340
|
+
properties = Array.new
|
341
|
+
passed_properties.each do | key, value |
|
342
|
+
properties << {ONE_PROPERTIES_NAME => key.to_s, ONE_PROPERTIES_VALUE => value} unless (key == :header)
|
343
|
+
end
|
344
|
+
# merge the properties (name / value) connections if there are additonal values to pass
|
345
|
+
parameters.merge!({ONE_PARAM_PROPERTIES => properties}) if (properties.length > 0)
|
346
|
+
|
347
|
+
# return the full parameter hash
|
348
|
+
return(parameters)
|
349
|
+
|
350
|
+
end
|
351
|
+
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'cgi'
|
3
|
+
|
4
|
+
class OEHClient::Realtime::Optimization
|
5
|
+
|
6
|
+
# HASH keys based on the response data
|
7
|
+
OPT_RESPONSE_NAME = "name"
|
8
|
+
OPT_RESPONSE_ASSET = "asset"
|
9
|
+
OPT_RESPONSE_CONTENT = "content"
|
10
|
+
OPT_RESPONSE_CONTENT_URL = "contentUrl"
|
11
|
+
OPT_RESPONSE_MIME_TYPE = "mimeType"
|
12
|
+
OPT_RESPONSE_PROPOSITION = "proposition"
|
13
|
+
OPT_RESPONSE_RESPONSES = "responses"
|
14
|
+
|
15
|
+
# Localized attributes equivalent to the returned Hash object in the response
|
16
|
+
attr_accessor :name, # The name of the action
|
17
|
+
:content, # The decoded content of the asset
|
18
|
+
:content_url, # The URL to the referential asset
|
19
|
+
:mime_type, # The MIME Type of the asset based on the OEH Configuration
|
20
|
+
:proposition, # The code of the proposition, related to the optimization
|
21
|
+
:responses, # The collection of responses valid for this optmization
|
22
|
+
:interaction # The parent interaction object
|
23
|
+
|
24
|
+
# ---- Class Methods
|
25
|
+
|
26
|
+
def self.create(interaction, properties={})
|
27
|
+
|
28
|
+
one_asset = properties[OPT_RESPONSE_ASSET]
|
29
|
+
|
30
|
+
# create a new instance of the OEHClient::Realtime::Optimization class
|
31
|
+
optimization_instance = OEHClient::Realtime::Optimization.new()
|
32
|
+
|
33
|
+
# assign all data attributes based on the properties object that is passed
|
34
|
+
optimization_instance.name = properties[OPT_RESPONSE_NAME] if (properties.has_key?(OPT_RESPONSE_NAME))
|
35
|
+
optimization_instance.proposition = properties[OPT_RESPONSE_PROPOSITION] if (properties.has_key?(OPT_RESPONSE_PROPOSITION))
|
36
|
+
# map the asset data to the class instance variables
|
37
|
+
optimization_instance.content = CGI.unescapeHTML(one_asset[OPT_RESPONSE_CONTENT]) if (one_asset.has_key?(OPT_RESPONSE_CONTENT))
|
38
|
+
optimization_instance.content_url = one_asset[OPT_RESPONSE_CONTENT_URL] if (one_asset.has_key?(OPT_RESPONSE_CONTENT_URL))
|
39
|
+
optimization_instance.mime_type = one_asset[OPT_RESPONSE_MIME_TYPE] if (one_asset.has_key?(OPT_RESPONSE_MIME_TYPE))
|
40
|
+
# map each of the respones to the response object
|
41
|
+
optimization_instance.responses ||= Array.new
|
42
|
+
one_asset[OPT_RESPONSE_RESPONSES].each do | asset_response |
|
43
|
+
optimization_instance.responses << OEHClient::Realtime::Response.create(optimization_instance, asset_response)
|
44
|
+
end
|
45
|
+
# set the parent interaction object
|
46
|
+
optimization_instance.interaction = interaction
|
47
|
+
#return the new instance of the optimzation class
|
48
|
+
optimization_instance
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
# ---- Instance Methods
|
53
|
+
|
54
|
+
|
55
|
+
end
|