oeh-client 0.1.4 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 523096f4e6ed912ba9438ae57ee5db4c5436a107
4
- data.tar.gz: 0acd9dd22c8d6a825ff85f337425d9862e80a0b8
3
+ metadata.gz: a29637284500a08ab1cb3431a8a963e557041758
4
+ data.tar.gz: 542ba1977b341ec2400719a1b5da7196268383ee
5
5
  SHA512:
6
- metadata.gz: 7c095dfd4864698e800cd3caa046c0069836b44928a57e60e73e9f8cde139c85867244585681f8ae329fd5f1b3651da17246711f8769d467eb87b82eafe73594
7
- data.tar.gz: 0226d6c29edbe1e84e3fcda936f1ccac0aae561ae1074d9c77a117ebd6a7bad28d451301bca55a487fd435be084b58b4860c60ad3d06c319d04700bb456af9f5
6
+ metadata.gz: 7455865c7636b7380d68bb7f0427e9f0e37d7af623a5b0f409eb184c42f12974050d8e31c3d51eb2be5184971977620313541850c0f9937be96aa86284738b65
7
+ data.tar.gz: 494f6251c84dfa8a9bb5004867929d14f7d0678ff8d27f5efff4eb5ac6a5030cc2afb251907a8ec8d93a6eadcd00ebd462aa8d5fdf70690e3cc716bda21e866d
@@ -1,3 +1,6 @@
1
+ require 'erb'
2
+ require 'rest-client'
3
+
1
4
  class OEHClient::Config::Space
2
5
 
3
6
  attr_accessor :site_key, # The ONE Site KEY Value
@@ -5,7 +8,8 @@ class OEHClient::Config::Space
5
8
  :api_key, # The API key provided in the ONE SETTINGS interface
6
9
  :shared_secret, # The shared secret value provided in the ONE SETTINGS interface
7
10
  :username, # The fully-qualified username configured for access to the space
8
- :meta_password # The password needed to access meta-data from the OEH server
11
+ :meta_password, # The password needed to access meta-data from the OEH server
12
+ :cookies # The cookies returned from an OEH API request
9
13
 
10
14
  # ---- Class Methods
11
15
 
@@ -48,6 +52,13 @@ class OEHClient::Config::Space
48
52
  return(OAuth::AccessToken.new(oauth_consumer))
49
53
 
50
54
  end
55
+ # return a new instance of the OAuth::Consumer using the details of the space
56
+ def oauth_consumer()
57
+ OAuth::Consumer.new("#{@api_key}!#{@username}",
58
+ @shared_secret,
59
+ {:site => "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@host}#{OEHClient::Helper::Request::THUNDERHEAD_DOMAIN}",
60
+ :scheme => :header})
61
+ end
51
62
 
52
63
  # is_valid determines if the current class has values for all the attributes. Each attribute is
53
64
  # required with a non-nul / non-blank value to be considered valid
@@ -64,4 +75,34 @@ class OEHClient::Config::Space
64
75
  self.is_valid? && (!@meta_password.blank?)
65
76
  end
66
77
 
78
+ # return the hash that
79
+ def meta_credentials()
80
+ {:username => @username, :password => @meta_password, :rememberMe => "false"}
81
+ end # def meta_credentials
82
+
83
+ def login_url()
84
+ "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@host}#{OEHClient::Helper::Request::THUNDERHEAD_DOMAIN}/one/idm_login"
85
+ end # def login_url
86
+
87
+ def logout_url()
88
+ "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@host}#{OEHClient::Helper::Request::THUNDERHEAD_DOMAIN}/one/logout"
89
+ end
90
+
91
+ # logon to the OEH thinstance with the existing credentials
92
+ def login()
93
+ response = OEHClient.post(login_url, nil, :payload => meta_credentials)
94
+ @cookies = response[:cookies] if (@cookies.blank? && response.has_key?(:cookies))
95
+ end
96
+
97
+ def logout()
98
+ header = Hash.new
99
+ header.merge!(:cookies => @cookies) unless (@cookies.blank?)
100
+ header.merge!(OEHClient::Helper::Request.default_JSON_header)
101
+
102
+ puts "Logout Header: #{header}"
103
+
104
+ response = OEHClient.post(logout_url, nil, :header => header)
105
+ @cookies = nil
106
+ end
107
+
67
108
  end
@@ -33,10 +33,11 @@ class OEHClient::Data::Structure < OEHClient::Data::Node
33
33
 
34
34
  # Use the OEHClient object to call OEH profiles API and create an instance
35
35
  # of this class (OEHClient::Data::Structure) as the return value
36
- response = OEHClient::Helper::Response.handle(OEHClient.get(@space.token,
37
- OEHClient::Helper::Request.format_url(self.request_url(api_name), {:sk => @space.site_key, :ck => customer_key})))
36
+ response = OEHClient.get(self.request_url(api_name),
37
+ @space.oauth_consumer,
38
+ :params => {:sk => @space.site_key, :ck => customer_key})
38
39
  # dynamically map the response object to a new OEHClient::Data::Structure class
39
- OEHClient::Data::Structure.new(response)
40
+ OEHClient::Data::Structure.new(response[:body])
40
41
 
41
42
  else
42
43
 
@@ -2,8 +2,8 @@ module OEHClient
2
2
 
3
3
  module Exception
4
4
 
5
- # Used when an invalid OAuth token is detected as part of the client-side API calls
6
- class InvalidTokenException < ::Exception
5
+ # Used when an invalid OAuth consumer is detected as part of the client-side API calls
6
+ class InvalidConsumerException < ::Exception
7
7
 
8
8
  end
9
9
 
@@ -46,18 +46,30 @@ module OEHClient
46
46
 
47
47
  module Response
48
48
 
49
- def self.handle(api_response)
49
+ # determine if the string that was passed is valid JSON syntax and can be parsed by the the
50
+ # ActiveSupport::JSON.decode method
51
+ def self.valid_json?(json)
52
+ valid_indicator = true
53
+ begin
54
+ ActiveSupport::JSON.decode(json)
55
+ rescue ActiveSupport::JSON.parse_error
56
+ valid_indicator = false
57
+ end
58
+ end
59
+
60
+
61
+ def self.handle(rest_response)
62
+
63
+ valid_response_codes = [100, 200, 302]
50
64
  # raise a generic HTTPRequestException if the the status code is not 100 (Continue) or 200 (OK)
51
- raise OEHClient::Exception::HTTPRequestException.new(api_response.message, api_response.code) if (api_response.code != "200" && api_response.code != "100")
52
-
53
- begin
54
- # Return the Body of the response for valid requests
55
- ActiveSupport::JSON.decode(api_response.body)
56
- rescue ActiveSupport::JSON.parse_error
57
- # otherwise raise a standard processing exception
58
- raise OEHClient::Exception::HTTPRequestException(api.body, 500)
59
- end
65
+ raise OEHClient::Exception::HTTPRequestException.new("HTTP Request Exception with code #{rest_response.code}", rest_response.code) unless (valid_response_codes.include?(rest_response.code))
66
+
67
+ api_response = Hash.new
68
+
69
+ api_response[:body] = (OEHClient::Helper::Response.valid_json?(rest_response.body) ? ActiveSupport::JSON.decode(rest_response.body) : rest_response.body)
70
+ api_response[:cookies] = rest_response.cookies
60
71
 
72
+ api_response
61
73
 
62
74
  end
63
75
 
@@ -1,6 +1,10 @@
1
1
 
2
2
  class OEHClient::Interaction::Interaction
3
3
 
4
+ #
5
+ # -------[ CONSTANTS ]
6
+ #
7
+
4
8
  # constants used to construct the restful API Request URL
5
9
  API_REALTIME = "/interaction"
6
10
  API_OFFLINE = "/offline"
@@ -21,6 +25,10 @@ class OEHClient::Interaction::Interaction
21
25
  ONE_RESPONSE_CAPTURES = "captures"
22
26
 
23
27
 
28
+ #
29
+ # -------[ CLASS ATTRIBUTES ]
30
+ #
31
+
24
32
  attr_accessor :uri, # The full touchpoint/interaction URI used to post the interaction
25
33
  :space, # The configured OEHClient::Config::Space object that represents the OEH workspace
26
34
 
@@ -33,14 +41,16 @@ class OEHClient::Interaction::Interaction
33
41
  :trackers, # The collection of tracking point data that are relevant for the interaction
34
42
  :captures, # The collection of capture point data that are relevant for the interactions
35
43
 
36
- :errors
44
+ :cookies # The JSON object of cookies returned within a ONE Request
45
+
46
+ #
47
+ # -------[ CLASS METHODS ]
48
+ #
37
49
 
38
- ###
39
- ### ------------- Class Methods
40
- ###
41
50
  class << self
42
51
 
43
- # post
52
+ # class-level wrapper to post a new interaction to the OEH server using either the realtime or offline
53
+ # API for an anonymous or known prospects/customer
44
54
  def post(site_key, uri, timestamp=nil, tid=nil, customer_key=nil, properties={})
45
55
 
46
56
  # setup the baseline attributes hash with the site_key and interaction URI, which are the
@@ -87,10 +97,11 @@ class OEHClient::Interaction::Interaction
87
97
 
88
98
  end
89
99
 
90
- ###
91
- ### ------------- Instance Methods
92
- ###
100
+ #
101
+ # -------[ INSTANCE METHODS ]
102
+ #
93
103
 
104
+ # constructor that allows the passed Ruby Hash to be mapped to the
94
105
  def initialize(attributes=nil)
95
106
 
96
107
  # set the instance attributes is the parameter hash is created
@@ -115,7 +126,7 @@ class OEHClient::Interaction::Interaction
115
126
  raise OEHClient::Exception::MissingParameterException.new(missing_minimal_parameters) unless (minimal_parameters?)
116
127
 
117
128
  # call the appropriate method based on the existance of the timestamp value
118
- ((!@timestamp.nil?) ? send_offline(@space.token, parameters) : send_realtime(@space.token, parameters))
129
+ ((!@timestamp.nil?) ? send_offline(parameters) : send_realtime(parameters))
119
130
 
120
131
  # return the current instance interacton
121
132
  self
@@ -157,50 +168,47 @@ class OEHClient::Interaction::Interaction
157
168
  properties ||= Hash.new
158
169
 
159
170
  # Call the PUT method to update the
160
- #OEHClient::Helper::Response.handle(OEHClient.put(@space.token(),
161
- # OEHClient::Helper::Request.format_url(realtime_url, {:sk => @space.site_key}),
162
- # nil,
163
- # request_data(properties))) unless properties.empty?
164
-
165
- send_request(OEHClient::Helper::Request::PUT_METHOD, @space.token(), realtime_url, properties) unless properties.empty?
171
+ send_request(OEHClient::Helper::Request::PUT_METHOD, realtime_url, properties) unless properties.empty?
166
172
 
167
173
  # return the current object
168
174
  self
169
175
  end
170
176
 
171
177
 
178
+ #
179
+ # -------[ PRIVATE METHODS ]
180
+ #
181
+
172
182
  private
173
183
 
174
184
 
175
185
  # send_realtime posts a new interaction occuring at the moment (in realtime). The response attributes
176
186
  # are mapped to the current instance attributes for all valid requests
177
- def send_realtime(token, properties={})
187
+ def send_realtime(properties={})
178
188
  # POST the realtime interaction method
179
- response = send_request(OEHClient::Helper::Request::POST_METHOD, token, realtime_url, properties)
189
+ response = send_request(OEHClient::Helper::Request::POST_METHOD, realtime_url, properties)
180
190
  # map the response message to the current instance attributes
181
191
  map_response(response)
182
192
  end
183
193
 
184
194
  # send_offline posts a historic interaction, using a specified timestamp
185
- def send_offline(token, properties={})
195
+ def send_offline(properties={})
186
196
  # PUT the offline interaction method
187
- response = send_request(OEHClient::Helper::Request::PUT_METHOD, token, offline_url, properties)
197
+ response = send_request(OEHClient::Helper::Request::PUT_METHOD, offline_url, properties)
188
198
  # map the response message to the current instance attributes
189
199
  map_response(response)
190
200
  end
191
201
 
192
202
  # send_request acts as the wrapper to send all client requests to the ONE server in a unified manner
193
- def send_request(method, token, url, properties={})
203
+ def send_request(method, url, properties={})
194
204
  # set the URL parameters for the site_key and the tid, of the value exists
195
205
  url_parameters = {:sk => @space.site_key}
196
206
  url_parameters.merge!({:tid => @tid}) unless (@tid.blank?)
197
207
  # send the POST or PUT methond along with the arguments to the OEHClient class
198
- OEHClient::Helper::Response.handle(OEHClient.send(method.downcase.to_sym,
199
- token,
200
- OEHClient::Helper::Request.format_url(url, url_parameters),
201
- nil,
202
- request_data(properties)
203
- ))
208
+ OEHClient.send(method.downcase.to_sym,
209
+ url,
210
+ @space.oauth_consumer,
211
+ {:params => url_parameters, :payload => ActiveSupport::JSON.encode(request_data(properties))})
204
212
 
205
213
  end
206
214
 
@@ -208,10 +216,11 @@ class OEHClient::Interaction::Interaction
208
216
  # attributes in the current instance of the interaction object
209
217
  def map_response(response)
210
218
 
219
+ body = response[:body]
211
220
 
212
221
  # Save the tid and session data if they where not previously used in the request
213
- @tid = response[ONE_PARAM_TID] if (@tid.nil? || (!@tid.blank? && @tid != response[ONE_PARAM_TID]))
214
- @session = response[ONE_PARAM_SESSION] if @session.nil?
222
+ @tid = body[ONE_PARAM_TID] if (@tid.nil? || (!@tid.blank? && @tid != body[ONE_PARAM_TID]))
223
+ @session = body[ONE_PARAM_SESSION] if @session.nil?
215
224
 
216
225
  # capture the optimizations returned from the request and map it to the OEHClient::Interaction::Optimization
217
226
  # class
@@ -219,19 +228,24 @@ class OEHClient::Interaction::Interaction
219
228
  # initialize the optimizations collection if it is null
220
229
  @optimizations ||= Array.new
221
230
  # map each of the optimizations to the OEHClient::Interaction::Optmization class
222
- response[ONE_RESPONSE_OPTIMIZATIONS].each do | response_optimization |
231
+ body[ONE_RESPONSE_OPTIMIZATIONS].each do | response_optimization |
223
232
  @optimizations << OEHClient::Interaction::Optimization.create(response_optimization)
224
233
  end
225
234
 
235
+ # store the cookies passed back by the system
236
+ @cookies = response[:cookies]
237
+
226
238
  # capture the trackers returned from the request and mpt it to the OEHClient::Interaction::Tracker
227
239
  # class
228
240
  # TODO: Create OEHClient::Interaction::Tracker class
229
- @trackers = response[ONE_RESPONSE_TRACKERS]
241
+ @trackers = body[ONE_RESPONSE_TRACKERS]
230
242
 
231
243
  # capture the capture points returned from the request and map it to the OEHClient::Interaction::Capture
232
244
  # class
233
245
  # TODO: Create OEHClient::Interaction::Capture class
234
- @captures = response[ONE_RESPONSE_CAPTURES]
246
+ @captures = body[ONE_RESPONSE_CAPTURES]
247
+
248
+
235
249
 
236
250
  end
237
251
 
@@ -1,3 +1,3 @@
1
1
  module OEHClient
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/oehclient.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'oauth'
2
+ require 'rest-client'
2
3
  require 'csv'
3
4
  require 'uuidtools'
4
5
  require 'json'
@@ -30,50 +31,83 @@ module OEHClient
30
31
 
31
32
  end
32
33
 
33
- # get wraps the GET method of the access token and manages the response that is returned
34
- def get(token, url, header={})
34
+ # wraps the GET method of the RestClient::Resource and manages the response that is returned
35
+ def get(url, oauth_consumer=nil, *args)
36
+ # retrieved the passed options from the SPLAT
37
+ options = (args.length > 0 ? args[0] : Hash.new)
38
+ # if the oauth_consumer is not a proper OAuth::Consumer, then raise the Oeh::Exception::InvalidTokenException
39
+ # exception to the calling method
40
+ raise OEHClient::Exception::InvalidConsumerException if (!oauth_consumer.nil? && !oauth_consumer.kind_of?(OAuth::Consumer))
35
41
 
36
- # force the new hash. Testing in RSPEC uncovered issue with the default not being followed
37
- header ||= Hash.new
42
+ # reset execution processes
43
+ RestClient.reset_before_execution_procs
44
+ # if this request has an oauth token, sign the RestClient object
45
+ unless (oauth_consumer.nil?)
46
+ # add the oauth signature
47
+ RestClient.add_before_execution_proc { |req, params| oauth_consumer.sign! req }
48
+ end
49
+
50
+ # force the new hash for the header and params if they are NIL
51
+ header = (options.has_key?(:header) ? options[:header] : OEHClient::Helper::Request.default_JSON_header())
52
+ # merge parameters into the header if they are passed
53
+ header.merge!(:params => options[:params] ) if (options.has_key?(:params))
38
54
 
39
- # if the token is not a proper OAuth::AccessToken, then raise the Oeh::Exception::InvalidTokenException
40
- # exception to the calling method
41
- raise OEHClient::Exception::InvalidTokenException unless (token.kind_of?(OAuth::AccessToken))
42
55
  # send the GET request, manage the returned response, and return the body of the response to the
43
56
  # calling method
44
- token.get(url, header.empty? ? OEHClient::Helper::Request.default_JSON_header() : header)
57
+ RestClient.get(url, header) { | response, request, result | OEHClient::Helper::Response.handle(response)}
45
58
 
46
59
  end
47
60
 
48
61
  # post wraps the POST method of the access token and manages the response that is returned
49
- def post(token, url, header={}, body)
62
+ def post(url, oauth_consumer=nil, *args)
63
+ # retrieved the passed options from the SPLAT
64
+ options = (args.length > 0 ? args[0] : Hash.new)
65
+ # if the oauth_consumer is not a proper OAuth::Consumer, then raise the Oeh::Exception::InvalidTokenException
66
+ # exception to the calling method
67
+ raise OEHClient::Exception::InvalidConsumerException if (!oauth_consumer.nil? && !oauth_consumer.kind_of?(OAuth::Consumer))
50
68
 
51
- # force the new hash. Testing in RSPEC uncovered issue with the default not being followed
52
- header ||= Hash.new
69
+ # reset execution processes
70
+ RestClient.reset_before_execution_procs
71
+ # if this request has an oauth token, sign the RestClient object
72
+ unless (oauth_consumer.nil?)
73
+ # add the oauth signature
74
+ RestClient.add_before_execution_proc { |req, params| oauth_consumer.sign! req }
75
+ end
76
+
77
+ # force the new hash for the header and params if they are NIL
78
+ header = (options.has_key?(:header) ? options[:header] : OEHClient::Helper::Request.default_JSON_header())
79
+ # merge parameters into the header if they are passed
80
+ header.merge!(:params => options[:params] ) if (options.has_key?(:params))
53
81
 
54
- # if the token is not a proper OAuth::AccessToken, then raise the Oeh::Exception::InvalidTokenException
55
- # exception to the calling method
56
- raise OEHClient::Exception::InvalidTokenException unless (token.kind_of?(OAuth::AccessToken))
57
82
  # send the POST request, manage the returned response, and return the body of the response to the
58
83
  # calling method
59
- token.post(url, ActiveSupport::JSON.encode(body), header.empty? ? OEHClient::Helper::Request.default_JSON_header() : header)
60
-
61
-
84
+ RestClient.post(url, options[:payload], header) { | response, request, result | OEHClient::Helper::Response.handle(response)}
62
85
  end
63
86
 
64
87
  # put wraps the PUT method of the access token and manages the response that is returned
65
- def put(token, url, header={}, body)
88
+ def put(url, oauth_consumer=nil, *args)
89
+ # retrieved the passed options from the SPLAT
90
+ options = (args.length > 0 ? args[0] : Hash.new)
91
+ # if the oauth_consumer is not a proper OAuth::Consumer, then raise the Oeh::Exception::InvalidTokenException
92
+ # exception to the calling method
93
+ raise OEHClient::Exception::InvalidConsumerException if (!oauth_consumer.nil? && !oauth_consumer.kind_of?(OAuth::Consumer))
66
94
 
67
- # force the new hash. Testing in RSPEC uncovered issue with the default not being followed
68
- header ||= Hash.new
95
+ # reset execution processes
96
+ RestClient.reset_before_execution_procs
97
+ # if this request has an oauth token, sign the RestClient object
98
+ unless (oauth_consumer.nil?)
99
+ # add the oauth signature
100
+ RestClient.add_before_execution_proc { |req, params| oauth_consumer.sign! req }
101
+ end
69
102
 
70
- # if the token is not a proper OAuth::AccessToken, then raise the Oeh::Exception::InvalidTokenException
71
- # exception to the calling method
72
- raise OEHClient::Exception::InvalidTokenException unless (token.kind_of?(OAuth::AccessToken))
73
- # send the PUT request, manage the returned response, and return the body of the response to the
74
- # calling method
75
- token.put(url, ActiveSupport::JSON.encode(body), header.empty? ? OEHClient::Helper::Request.default_JSON_header() : header)
103
+ # force the new hash for the header and params if they are NIL
104
+ header = (options.has_key?(:header) ? options[:header] : OEHClient::Helper::Request.default_JSON_header())
105
+ # merge parameters into the header if they are passed
106
+ header.merge!(:params => options[:params] ) if (options.has_key?(:params))
76
107
 
108
+ # send the POST request, manage the returned response, and return the body of the response to the
109
+ # calling method
110
+ RestClient.put(url, options[:payload], header) { | response, request, result | OEHClient::Helper::Response.handle(response)}
77
111
  end
78
112
 
79
113
  end
data/oeh-client.gemspec CHANGED
@@ -36,5 +36,6 @@ Gem::Specification.new do |spec|
36
36
  spec.add_dependency "uuidtools"
37
37
  spec.add_dependency "activesupport", "~> 4.2.0"
38
38
  spec.add_dependency "railties", ">= 4.2.0"
39
+ spec.add_dependency "rest-client"
39
40
 
40
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oeh-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Balliet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2015-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: 4.2.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rest-client
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description: A simple set of classes that allow access to Interactions, Documents,
126
140
  and Structures from within Thunderhead's ONE Enagement Hub
127
141
  email: