mints 0.0.19 → 0.0.22

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.
data/lib/client.rb CHANGED
@@ -1,362 +1,394 @@
1
- require "httparty"
2
- require "json"
3
- require "addressable"
4
- require "redis"
5
- module Mints
6
- class Client
7
- attr_reader :host
8
- attr_reader :api_key
9
- attr_reader :scope
10
- attr_reader :base_url
11
- attr_accessor :session_token
12
- attr_accessor :contact_token_id
13
-
14
- def initialize(host, api_key, scope = nil, session_token = nil, contact_token_id = nil, debug = false)
15
- @host = host
16
- @api_key = api_key
17
- @session_token = session_token
18
- @contact_token_id = contact_token_id
19
- @debug = debug
20
- self.set_scope(scope)
21
- end
22
-
23
- def raw(action, url, options = nil, data = nil, base_url = nil, compatibility_options = {})
24
- base_url = @base_url if !base_url
25
- uri = ""
26
- if (options && options.class == Hash)
27
- if (options[:jfilters] && options[:jfilters].class == Hash)
28
- options[:jfilters] = Base64.encode64(JSON.generate(options[:jfilters]))
29
- end
30
- uri = Addressable::URI.new
31
- uri.query_values = options
32
- end
33
-
34
- full_url = "#{@host}#{base_url}#{url}#{uri}"
35
- response = nil
36
- if action === 'get'
37
-
38
- template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
39
- config = YAML.load template.result(binding)
40
- url_need_cache = false
41
- result_from_cache = false
42
- time = 0
43
-
44
- if config['redis_cache']['use_cache']
45
- config['redis_cache']['groups'].each do |group|
46
- group['urls'].each do |url|
47
- if full_url.match url
48
- time = group['time']
49
- url_need_cache = true
50
- @redis_server = Redis.new(host: config['redis_cache']['redis_host'], port: config['redis_cache']['redis_port'] ? config['redis_cache']['redis_port'] : 6379, db: config['redis_cache']['redis_db'] ? config['redis_cache']['redis_db'] : 1)
51
- if @redis_server.get(full_url)
52
- response = @redis_server.get(full_url)
53
- result_from_cache = true
54
- else
55
- response = self.send("#{@scope}_#{action}", "#{full_url}", compatibility_options)
56
- @redis_server.setex(full_url,time,response)
57
- end
58
- break
59
- end
60
- end
61
- break if url_need_cache
62
- end
63
- end
64
-
65
- if !url_need_cache
66
- response = self.send("#{@scope}_#{action}", "#{full_url}", compatibility_options)
67
- end
68
-
69
- elsif action === 'create' or action === 'post'
70
- action = 'post'
71
- response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
72
- elsif action === 'put' or action === 'patch' or action ==='update'
73
- action = 'put'
74
- response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
75
- elsif action === 'delete' or action === 'destroy'
76
- action = 'delete'
77
- response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
78
- end
79
- if result_from_cache
80
- return parsed_response = JSON.parse(response)
81
- else
82
- if (response.response.code == "404")
83
- raise 'NotFoundError'
84
- end
85
- parsed_response = JSON.parse(response.body)
86
- return parsed_response
87
- end
88
- end
89
-
90
- def method_missing(name, *args, &block)
91
- puts name
92
- name.to_s.include?("__") ? separator = "__" : separator = "_"
93
- # split the name to identify their elements
94
- name_spplited = name.to_s.split(separator)
95
- # count the elments
96
- name_len = name_spplited.size
97
- # the action always be the first element
98
- action = name_spplited.first
99
- raise 'NoActionError' unless ['get', 'create', 'post', 'update', 'put', 'delete', 'destroy'].include?(action)
100
- # the object always be the last element
101
- object = separator == "__" ? name_spplited.last.gsub("_","-") : name_spplited.last
102
- # get intermediate url elements
103
- route_array = []
104
- (name_len-1).times do |n|
105
- if n == 0 or n == name_len-1
106
- next
107
- end
108
- n = name_spplited[n]
109
- self.replacements().each do |object|
110
- n = n.gsub(object[:old_value], object[:new_value])
111
- end
112
- route_array.push n
113
- end
114
- route = route_array.join("/")
115
-
116
-
117
- slug = nil
118
- uri = Addressable::URI.new
119
- if action == "get"
120
- if args.first.class == Hash
121
- uri.query_values = args.first
122
- elsif args.first.class == String or Integer
123
- slug = args.first
124
- uri.query_values = args[1]
125
- end
126
- url = self.get_url(route, object, uri, slug)
127
- response = self.send("#{@scope}_#{action}", url, compatibility_options)
128
- elsif action == "post" or action == "create"
129
- if args[1].class == Hash
130
- uri.query_values = args[1]
131
- end
132
- url = self.get_url(route, object, uri, slug)
133
- action = 'post'
134
- data = args[0]
135
- response = self.send("#{@scope}_#{action}", url, {data: data}, compatibility_options)
136
- elsif action == "put" or action == "update"
137
- if args.first.class == String or Integer
138
- slug = args.first
139
- uri.query_values = args[2]
140
- end
141
- url = self.get_url(route, object, uri, slug)
142
- action = 'put'
143
- id = args[0]
144
- data = args[1]
145
- response = self.send("#{@scope}_#{action}", "#{url}", {data: data}, compatibility_options)
146
- end
147
-
148
- if response.response.code == "404"
149
- raise 'NotFoundError'
150
- elsif response.response.code == "500"
151
- raise 'InternalServerError'
152
- end
153
- return JSON.parse(response.body)
154
- end
155
-
156
- def get_url(route, object, uri, slug = nil)
157
- if (slug)
158
- return "#{@host}#{@base_url}/#{route}/#{object}/#{slug}#{uri}"
159
- else
160
- return "#{@host}#{@base_url}/#{route}/#{object}#{uri}"
161
- end
162
- end
163
-
164
- def replacements
165
- return [
166
- {old_value: '_', new_value: '-'},
167
- {old_value: 'people', new_value: 'crm'},
168
- {old_value: 'store', new_value: 'ecommerce'}
169
- ]
170
- end
171
-
172
- def set_scope(scope)
173
- @scope = scope
174
- if scope == "public"
175
- @base_url = "/api/v1"
176
- elsif scope == "contact"
177
- @base_url = "/api/v1"
178
- elsif scope == "user"
179
- @base_url = "/api/user/v1"
180
- else
181
- @scope = "public"
182
- @base_url = "/api/v1"
183
- end
184
- end
185
-
186
- ##### HTTTP CLIENTS ######
187
- # Simple HTTP GET
188
- def http_get(url, headers = nil)
189
- if @debug
190
- puts "Url:"
191
- puts url
192
- puts "Headers:"
193
- puts headers
194
- puts "Method: get"
195
- end
196
- return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
197
- end
198
-
199
- # Simple HTTP POST
200
- def http_post(url, headers = nil, data = nil)
201
- if @debug
202
- puts "Url:"
203
- puts url
204
- puts "Headers:"
205
- puts headers
206
- puts "Data:"
207
- puts data
208
- puts "Method: post"
209
- end
210
- return headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data)
211
- end
212
-
213
- # Simple HTTP PUT
214
- def http_put(url, headers = nil, data = nil)
215
- if @debug
216
- puts "Url:"
217
- puts url
218
- puts "Headers:"
219
- puts headers
220
- puts "Data:"
221
- puts data
222
- puts "Method: put"
223
- end
224
- return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
225
- end
226
-
227
- # Simple HTTP DELETE
228
- def http_delete(url, headers = nil, data = nil)
229
- if @debug
230
- puts "Url:"
231
- puts url
232
- puts "Headers:"
233
- puts headers
234
- puts "Data:"
235
- puts data
236
- puts "Method: delete"
237
- end
238
- return headers ? HTTParty.delete(url, :headers=> headers, :body => data) : HTTParty.delete(url, :body => data)
239
- end
240
-
241
- # Start contact context
242
- def contact_get(url, compatibility_options)
243
- headers = {
244
- "ApiKey" => @api_key,
245
- "Accept" => "application/json",
246
- "ContactToken" => @contact_token_id
247
- }
248
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
249
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
250
- return self.http_get(url, headers)
251
- end
252
-
253
- def contact_post(url, data, compatibility_options)
254
- headers = {
255
- "ApiKey" => @api_key,
256
- "Accept" => "application/json",
257
- "ContactToken" => @contact_token_id
258
- }
259
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
260
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
261
- return self.http_post(url, headers, data)
262
- end
263
-
264
- def contact_put(url, data, compatibility_options)
265
- headers = {
266
- "ApiKey" => @api_key,
267
- "Accept" => "application/json",
268
- "ContactToken" => @contact_token_id
269
- }
270
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
271
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
272
- return self.http_put(url, headers, data)
273
- end
274
-
275
- # Start User context
276
- def user_get(url, compatibility_options)
277
- headers = {
278
- "Accept" => "application/json",
279
- "ApiKey" => @api_key
280
- }
281
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
282
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
283
- return self.http_get(url, headers)
284
- end
285
-
286
- def user_post(url, data, compatibility_options)
287
- headers = {
288
- "Accept" => "application/json",
289
- "ApiKey" => @api_key
290
- }
291
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
292
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
293
- return self.http_post(url, headers, data)
294
- end
295
-
296
- def user_put(url, data, compatibility_options)
297
- headers = {
298
- "Accept" => "application/json",
299
- "ApiKey" => @api_key
300
- }
301
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
302
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
303
- return self.http_put(url, headers, data)
304
- end
305
-
306
- def user_delete(url, data, compatibility_options)
307
- headers = {
308
- "Accept" => "application/json",
309
- "ApiKey" => @api_key
310
- }
311
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
312
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
313
- return self.http_delete(url, headers, data)
314
- end
315
- # End User Context
316
-
317
- def public_get(url, headers = nil, compatibility_options)
318
- h = {
319
- "Accept" => "application/json",
320
- "ApiKey" => @api_key
321
- }
322
- h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
323
- h["ContactToken"] = @contact_token_id if @contact_token_id
324
- if headers
325
- headers.each do |k,v|
326
- h[k] = v
327
- end
328
- end
329
- self.http_get(url, h)
330
- end
331
-
332
- def public_post(url, headers = nil, data, compatibility_options)
333
- h = {
334
- "Accept" => "application/json",
335
- "ApiKey" => @api_key
336
- }
337
- h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
338
- h["ContactToken"] = @session_token if @session_token
339
- if headers
340
- headers.each do |k,v|
341
- h[k] = v
342
- end
343
- end
344
- self.http_post(url, h, data)
345
- end
346
-
347
- def public_put(url, headers = nil, data, compatibility_options)
348
- h = {
349
- "Accept" => "application/json",
350
- "ApiKey" => @api_key
351
- }
352
- h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
353
- h["ContactToken"] = @contact_token_id if @contact_token_id
354
- if headers
355
- headers.each do |k,v|
356
- h[k] = v
357
- end
358
- end
359
- self.http_put(url, h, data)
360
- end
361
- end
362
- end
1
+ require "httparty"
2
+ require "json"
3
+ require "addressable"
4
+ require "redis"
5
+ module Mints
6
+ class Client
7
+ attr_reader :host
8
+ attr_reader :api_key
9
+ attr_reader :scope
10
+ attr_reader :base_url
11
+ attr_accessor :session_token
12
+ attr_accessor :contact_token_id
13
+
14
+ def initialize(host, api_key, scope = nil, session_token = nil, contact_token_id = nil, visit_id = nil, debug = false)
15
+ @host = host
16
+ @api_key = api_key
17
+ @session_token = session_token
18
+ @contact_token_id = contact_token_id
19
+ @visit_id = visit_id
20
+ @debug = debug
21
+ self.set_scope(scope)
22
+ end
23
+
24
+ def raw(action, url, options = nil, data = nil, base_url = nil, compatibility_options = {}, only_tracking = false)
25
+ if compatibility_options === nil
26
+ compatibility_options = {}
27
+ end
28
+ base_url = @base_url if !base_url
29
+ uri = ""
30
+ if (options && options.class == Hash)
31
+ if (options[:jfilters] && options[:jfilters].class == Hash)
32
+ options[:jfilters] = Base64.encode64(JSON.generate(options[:jfilters]))
33
+ end
34
+ uri = Addressable::URI.new
35
+ uri.query_values = options
36
+ end
37
+
38
+ full_url = "#{@host}#{base_url}#{url}#{uri}"
39
+ response = nil
40
+ if action === 'get'
41
+
42
+ template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
43
+ config = YAML.load template.result(binding)
44
+ url_need_cache = false
45
+ result_from_cache = false
46
+ time = 0
47
+
48
+ if config['redis_cache']['use_cache']
49
+ config['redis_cache']['groups'].each do |group|
50
+ group['urls'].each do |url|
51
+ if full_url.match url
52
+ time = group['time']
53
+ url_need_cache = true
54
+ @redis_server = Redis.new(host: config['redis_cache']['redis_host'], port: config['redis_cache']['redis_port'] ? config['redis_cache']['redis_port'] : 6379, db: config['redis_cache']['redis_db'] ? config['redis_cache']['redis_db'] : 1)
55
+ redis_response = @redis_server.get(full_url)
56
+ if redis_response
57
+ response = redis_response
58
+ result_from_cache = true
59
+
60
+ headers = nil
61
+ if only_tracking
62
+ headers = {"Only-Tracking" => "true"}
63
+ #when is already in redis notify to California to register the object usage
64
+ cali_response = self.send("#{@scope}_#{action}", "#{full_url}", headers, compatibility_options)
65
+ if @debug
66
+ puts "CALI TRACKING RESPONSE: #{cali_response}"
67
+ end
68
+ end
69
+
70
+ if @debug
71
+ puts "REDIS RESPONSE: #{redis_response}"
72
+ end
73
+ else
74
+ response = self.send("#{@scope}_#{action}", "#{full_url}", nil, compatibility_options)
75
+ @redis_server.setex(full_url,time,response)
76
+ end
77
+ break
78
+ end
79
+ end
80
+ break if url_need_cache
81
+ end
82
+ end
83
+
84
+ if !url_need_cache
85
+ response = self.send("#{@scope}_#{action}", "#{full_url}", nil, compatibility_options)
86
+ end
87
+
88
+ elsif action === 'create' or action === 'post'
89
+ action = 'post'
90
+ response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
91
+ elsif action === 'put' or action === 'patch' or action ==='update'
92
+ action = 'put'
93
+ response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
94
+ elsif action === 'delete' or action === 'destroy'
95
+ action = 'delete'
96
+ response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
97
+ end
98
+ begin
99
+ if result_from_cache
100
+ return JSON.parse(response)
101
+ else
102
+ if (response.response.code == "404")
103
+ raise 'NotFoundError'
104
+ end
105
+ return JSON.parse(response.body)
106
+ end
107
+ rescue
108
+ return response
109
+ end
110
+ end
111
+
112
+ def method_missing(name, *args, &block)
113
+ name.to_s.include?("__") ? separator = "__" : separator = "_"
114
+ # split the name to identify their elements
115
+ name_spplited = name.to_s.split(separator)
116
+ # count the elments
117
+ name_len = name_spplited.size
118
+ # the action always be the first element
119
+ action = name_spplited.first
120
+ raise 'NoActionError' unless ['get', 'create', 'post', 'update', 'put', 'delete', 'destroy'].include?(action)
121
+ # the object always be the last element
122
+ object = separator == "__" ? name_spplited.last.gsub("_","-") : name_spplited.last
123
+ # get intermediate url elements
124
+ route_array = []
125
+ (name_len-1).times do |n|
126
+ if n == 0 or n == name_len-1
127
+ next
128
+ end
129
+ n = name_spplited[n]
130
+ self.replacements().each do |object|
131
+ n = n.gsub(object[:old_value], object[:new_value])
132
+ end
133
+ route_array.push n
134
+ end
135
+ route = route_array.join("/")
136
+
137
+
138
+ slug = nil
139
+ uri = Addressable::URI.new
140
+ if action == "get"
141
+ if args.first.class == Hash
142
+ uri.query_values = args.first
143
+ elsif args.first.class == String or Integer
144
+ slug = args.first
145
+ uri.query_values = args[1]
146
+ end
147
+ url = self.get_url(route, object, uri, slug)
148
+ response = self.send("#{@scope}_#{action}", url, nil, compatibility_options)
149
+ elsif action == "post" or action == "create"
150
+ if args[1].class == Hash
151
+ uri.query_values = args[1]
152
+ end
153
+ url = self.get_url(route, object, uri, slug)
154
+ action = 'post'
155
+ data = args[0]
156
+ response = self.send("#{@scope}_#{action}", url, {data: data}, compatibility_options)
157
+ elsif action == "put" or action == "update"
158
+ if args.first.class == String or Integer
159
+ slug = args.first
160
+ uri.query_values = args[2]
161
+ end
162
+ url = self.get_url(route, object, uri, slug)
163
+ action = 'put'
164
+ id = args[0]
165
+ data = args[1]
166
+ response = self.send("#{@scope}_#{action}", "#{url}", {data: data}, compatibility_options)
167
+ end
168
+
169
+ if response.response.code == "404"
170
+ raise 'NotFoundError'
171
+ elsif response.response.code == "500"
172
+ raise 'InternalServerError'
173
+ end
174
+ return JSON.parse(response.body)
175
+ end
176
+
177
+ def get_url(route, object, uri, slug = nil)
178
+ if (slug)
179
+ return "#{@host}#{@base_url}/#{route}/#{object}/#{slug}#{uri}"
180
+ else
181
+ return "#{@host}#{@base_url}/#{route}/#{object}#{uri}"
182
+ end
183
+ end
184
+
185
+ def replacements
186
+ return [
187
+ {old_value: '_', new_value: '-'},
188
+ {old_value: 'people', new_value: 'crm'},
189
+ {old_value: 'store', new_value: 'ecommerce'}
190
+ ]
191
+ end
192
+
193
+ def set_scope(scope)
194
+ @scope = scope
195
+ if scope == "public"
196
+ @base_url = "/api/v1"
197
+ elsif scope == "contact"
198
+ @base_url = "/api/v1"
199
+ elsif scope == "user"
200
+ @base_url = "/api/user/v1"
201
+ else
202
+ @scope = "public"
203
+ @base_url = "/api/v1"
204
+ end
205
+ end
206
+
207
+ ##### HTTTP CLIENTS ######
208
+ # Simple HTTP GET
209
+ def http_get(url, headers = nil)
210
+ if @debug
211
+ puts "Url:"
212
+ puts url
213
+ puts "Headers:"
214
+ puts headers
215
+ puts "Method: get"
216
+ end
217
+ return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
218
+ end
219
+
220
+ # Simple HTTP POST
221
+ def http_post(url, headers = nil, data = nil)
222
+ if @debug
223
+ puts "Url:"
224
+ puts url
225
+ puts "Headers:"
226
+ puts headers
227
+ puts "Data:"
228
+ puts data
229
+ puts "Method: post"
230
+ end
231
+ return headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data)
232
+ end
233
+
234
+ # Simple HTTP PUT
235
+ def http_put(url, headers = nil, data = nil)
236
+ if @debug
237
+ puts "Url:"
238
+ puts url
239
+ puts "Headers:"
240
+ puts headers
241
+ puts "Data:"
242
+ puts data
243
+ puts "Method: put"
244
+ end
245
+ return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
246
+ end
247
+
248
+ # Simple HTTP DELETE
249
+ def http_delete(url, headers = nil, data = nil)
250
+ if @debug
251
+ puts "Url:"
252
+ puts url
253
+ puts "Headers:"
254
+ puts headers
255
+ puts "Data:"
256
+ puts data
257
+ puts "Method: delete"
258
+ end
259
+ return headers ? HTTParty.delete(url, :headers=> headers, :body => data) : HTTParty.delete(url, :body => data)
260
+ end
261
+
262
+ # Start contact context
263
+ def contact_get(url, headers = nil, compatibility_options)
264
+ h = {
265
+ "ApiKey" => @api_key,
266
+ "Accept" => "application/json",
267
+ "ContactToken" => @contact_token_id
268
+ }
269
+ h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
270
+ h["Authorization"] = "Bearer #{@session_token}" if @session_token
271
+ if headers
272
+ headers.each do |k,v|
273
+ h[k] = v
274
+ end
275
+ end
276
+ return self.http_get(url, h)
277
+ end
278
+
279
+ def contact_post(url, data, compatibility_options)
280
+ headers = {
281
+ "ApiKey" => @api_key,
282
+ "Accept" => "application/json",
283
+ "ContactToken" => @contact_token_id
284
+ }
285
+ headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
286
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
287
+ return self.http_post(url, headers, data)
288
+ end
289
+
290
+ def contact_put(url, data, compatibility_options)
291
+ headers = {
292
+ "ApiKey" => @api_key,
293
+ "Accept" => "application/json",
294
+ "ContactToken" => @contact_token_id
295
+ }
296
+ headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
297
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
298
+ return self.http_put(url, headers, data)
299
+ end
300
+
301
+ # Start User context
302
+ def user_get(url, headers = nil, compatibility_options)
303
+ h = {
304
+ "Accept" => "application/json",
305
+ "ApiKey" => @api_key
306
+ }
307
+ h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
308
+ h["Authorization"] = "Bearer #{@session_token}" if @session_token
309
+ if headers
310
+ headers.each do |k,v|
311
+ h[k] = v
312
+ end
313
+ end
314
+ return self.http_get(url, h)
315
+ end
316
+
317
+ def user_post(url, data, compatibility_options)
318
+ headers = {
319
+ "Accept" => "application/json",
320
+ "ApiKey" => @api_key
321
+ }
322
+ headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
323
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
324
+ return self.http_post(url, headers, data)
325
+ end
326
+
327
+ def user_put(url, data, compatibility_options)
328
+ headers = {
329
+ "Accept" => "application/json",
330
+ "ApiKey" => @api_key
331
+ }
332
+ headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
333
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
334
+ return self.http_put(url, headers, data)
335
+ end
336
+
337
+ def user_delete(url, data, compatibility_options)
338
+ headers = {
339
+ "Accept" => "application/json",
340
+ "ApiKey" => @api_key
341
+ }
342
+ headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
343
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
344
+ return self.http_delete(url, headers, data)
345
+ end
346
+ # End User Context
347
+
348
+ def public_get(url, headers = nil, compatibility_options)
349
+ h = {
350
+ "Accept" => "application/json",
351
+ "ApiKey" => @api_key
352
+ }
353
+ h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
354
+ h["ContactToken"] = @contact_token_id if @contact_token_id
355
+ h["Visit-Id"] = @visit_id if @visit_id
356
+ if headers
357
+ headers.each do |k,v|
358
+ h[k] = v
359
+ end
360
+ end
361
+ self.http_get(url, h)
362
+ end
363
+
364
+ def public_post(url, headers = nil, data, compatibility_options)
365
+ h = {
366
+ "Accept" => "application/json",
367
+ "ApiKey" => @api_key
368
+ }
369
+ h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
370
+ h["ContactToken"] = @session_token if @session_token
371
+ if headers
372
+ headers.each do |k,v|
373
+ h[k] = v
374
+ end
375
+ end
376
+ self.http_post(url, h, data)
377
+ end
378
+
379
+ def public_put(url, headers = nil, data, compatibility_options)
380
+ h = {
381
+ "Accept" => "application/json",
382
+ "ApiKey" => @api_key
383
+ }
384
+ h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
385
+ h["ContactToken"] = @contact_token_id if @contact_token_id
386
+ if headers
387
+ headers.each do |k,v|
388
+ h[k] = v
389
+ end
390
+ end
391
+ self.http_put(url, h, data)
392
+ end
393
+ end
394
+ end