mints 0.0.28 → 0.0.30
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/client.rb +287 -385
- data/lib/contact/config/appointments.rb +201 -0
- data/lib/contact/config/config.rb +7 -0
- data/lib/contact/content/content.rb +7 -0
- data/lib/contact/content/conversations.rb +121 -0
- data/lib/contact/ecommerce/ecommerce.rb +14 -0
- data/lib/contact/ecommerce/order_items.rb +19 -0
- data/lib/contact/ecommerce/order_items_groups.rb +95 -0
- data/lib/contact/ecommerce/orders.rb +132 -0
- data/lib/contact/ecommerce/vouchers.rb +18 -0
- data/lib/contact.rb +54 -603
- data/lib/errors.rb +4 -2
- data/lib/generators/mints_assets_controller.rb +4 -1
- data/lib/generators/mints_contact_controller.rb +4 -1
- data/lib/generators/mints_files_generator.rb +16 -13
- data/lib/generators/mints_link.rb +7 -4
- data/lib/generators/mints_public_controller.rb +4 -1
- data/lib/generators/mints_user_controller.rb +3 -0
- data/lib/generators/short_link_controller.rb +4 -1
- data/lib/mints/controllers/admin_base_controller.rb +5 -3
- data/lib/mints/controllers/base_api_controller.rb +6 -4
- data/lib/mints/controllers/base_controller.rb +5 -3
- data/lib/mints/controllers/concerns/mints_clients.rb +4 -2
- data/lib/mints/controllers/concerns/read_config_file.rb +3 -1
- data/lib/mints/controllers/contact_api_controller.rb +12 -10
- data/lib/mints/controllers/public_api_controller.rb +10 -8
- data/lib/mints/controllers/user_api_controller.rb +11 -9
- data/lib/mints/helpers/contact_auth_helper.rb +5 -3
- data/lib/mints/helpers/mints_helper.rb +39 -38
- data/lib/mints/helpers/proxy_controllers_methods.rb +24 -24
- data/lib/mints/helpers/user_auth_helper.rb +5 -3
- data/lib/mints.rb +12 -10
- data/lib/pub/config/attributes.rb +13 -0
- data/lib/pub/config/config.rb +15 -0
- data/lib/pub/config/public_folders.rb +51 -0
- data/lib/pub/config/tags.rb +42 -0
- data/lib/pub/config/taxonomies.rb +49 -0
- data/lib/pub/content/assets.rb +16 -0
- data/lib/pub/content/content.rb +22 -0
- data/lib/pub/content/content_bundles.rb +40 -0
- data/lib/pub/content/content_instance_versions.rb +56 -0
- data/lib/pub/content/content_instances.rb +37 -0
- data/lib/pub/content/forms.rb +47 -0
- data/lib/pub/content/stories.rb +44 -0
- data/lib/pub/content/story_versions.rb +46 -0
- data/lib/pub/ecommerce/ecommerce.rb +14 -0
- data/lib/pub/ecommerce/locations.rb +25 -0
- data/lib/pub/ecommerce/orders.rb +31 -0
- data/lib/pub/ecommerce/products.rb +45 -0
- data/lib/pub.rb +22 -461
- data/lib/user/config/api_keys.rb +63 -61
- data/lib/user/config/appointments.rb +217 -215
- data/lib/user/config/attribute_groups.rb +74 -72
- data/lib/user/config/attributes.rb +83 -81
- data/lib/user/config/calendars.rb +88 -86
- data/lib/user/config/config.rb +31 -63
- data/lib/user/config/public_folders.rb +105 -104
- data/lib/user/config/relationships.rb +135 -132
- data/lib/user/config/roles.rb +76 -79
- data/lib/user/config/seeds.rb +52 -51
- data/lib/user/config/system_settings.rb +50 -49
- data/lib/user/config/tags.rb +57 -59
- data/lib/user/config/taxonomies.rb +115 -119
- data/lib/user/config/teams.rb +2 -0
- data/lib/user/config/users.rb +2 -0
- data/lib/user/contacts/contacts.rb +2 -0
- data/lib/user/content/assets.rb +3 -3
- data/lib/user/content/content.rb +224 -223
- data/lib/user/content/content_instances.rb +4 -2
- data/lib/user/content/content_templates.rb +2 -0
- data/lib/user/content/conversations.rb +2 -0
- data/lib/user/content/dam.rb +2 -0
- data/lib/user/content/forms.rb +2 -0
- data/lib/user/content/message_templates.rb +2 -0
- data/lib/user/content/messages.rb +2 -0
- data/lib/user/content/pages.rb +4 -2
- data/lib/user/content/stories.rb +3 -3
- data/lib/user/content/story_templates.rb +5 -3
- data/lib/user/content/story_versions.rb +2 -0
- data/lib/user/crm/companies.rb +3 -1
- data/lib/user/crm/contacts.rb +2 -0
- data/lib/user/crm/crm.rb +2 -0
- data/lib/user/crm/deals.rb +2 -0
- data/lib/user/crm/favorites.rb +2 -0
- data/lib/user/crm/segments.rb +2 -0
- data/lib/user/crm/users.rb +2 -0
- data/lib/user/crm/workflow_step_objects.rb +2 -0
- data/lib/user/crm/workflow_steps.rb +2 -0
- data/lib/user/crm/workflows.rb +2 -0
- data/lib/user/ecommerce/ecommerce.rb +4 -0
- data/lib/user/ecommerce/item_prices.rb +2 -0
- data/lib/user/ecommerce/locations.rb +4 -2
- data/lib/user/ecommerce/order_items_groups.rb +5 -2
- data/lib/user/ecommerce/order_statuses.rb +2 -0
- data/lib/user/ecommerce/orders.rb +4 -1
- data/lib/user/ecommerce/price_lists.rb +2 -0
- data/lib/user/ecommerce/product_templates.rb +3 -1
- data/lib/user/ecommerce/product_variations.rb +2 -0
- data/lib/user/ecommerce/products.rb +3 -1
- data/lib/user/ecommerce/skus.rb +2 -0
- data/lib/user/ecommerce/taxes.rb +2 -0
- data/lib/user/ecommerce/variant_options.rb +2 -0
- data/lib/user/ecommerce/variant_values.rb +2 -0
- data/lib/user/ecommerce/vouchers.rb +90 -0
- data/lib/user/helpers/helpers.rb +2 -0
- data/lib/user/helpers/object_activities.rb +2 -0
- data/lib/user/helpers/object_folders.rb +2 -0
- data/lib/user/helpers/user_folders.rb +2 -0
- data/lib/user/marketing/marketing.rb +2 -0
- data/lib/user/profile/profile.rb +2 -0
- data/lib/user.rb +32 -41
- metadata +42 -16
- data/lib/user/config/importers.rb +0 -184
data/lib/client.rb
CHANGED
@@ -1,438 +1,340 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require 'json'
|
5
|
+
require 'addressable'
|
6
|
+
require 'redis'
|
7
|
+
require_relative './mints/controllers/concerns/read_config_file'
|
8
|
+
|
5
9
|
module Mints
|
6
10
|
class Client
|
7
|
-
|
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
|
11
|
+
extend ActiveSupport::Concern
|
23
12
|
|
24
|
-
|
25
|
-
compatibility_options = {} if compatibility_options.nil?
|
13
|
+
include ReadConfigFile
|
26
14
|
|
27
|
-
|
28
|
-
|
15
|
+
attr_reader :host
|
16
|
+
attr_reader :api_key
|
17
|
+
attr_reader :scope
|
18
|
+
attr_reader :base_url
|
19
|
+
attr_accessor :session_token
|
20
|
+
attr_accessor :contact_token_id
|
29
21
|
|
30
|
-
|
31
|
-
|
32
|
-
|
22
|
+
def initialize(host, api_key, scope = nil, session_token = nil, contact_token_id = nil, visit_id = nil, debug = false)
|
23
|
+
@host = host
|
24
|
+
@api_key = api_key
|
25
|
+
@session_token = session_token
|
26
|
+
@contact_token_id = contact_token_id
|
27
|
+
@visit_id = visit_id
|
28
|
+
@debug = debug
|
33
29
|
|
34
|
-
|
35
|
-
|
36
|
-
end
|
30
|
+
self.set_scope(scope)
|
31
|
+
end
|
37
32
|
|
38
|
-
|
39
|
-
|
40
|
-
end
|
33
|
+
def raw(action, url, options = nil, data = nil, base_url = nil, compatibility_options = {}, only_tracking = false)
|
34
|
+
compatibility_options = {} if compatibility_options.nil?
|
41
35
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
46
|
-
config = YAML.safe_load template.result(binding)
|
47
|
-
|
48
|
-
if action === 'get'
|
49
|
-
url_need_cache = false
|
50
|
-
result_from_cache = false
|
51
|
-
time = 0
|
52
|
-
|
53
|
-
if config['redis_cache']['use_cache']
|
54
|
-
config['redis_cache']['groups'].each do |group|
|
55
|
-
group['urls'].each do |url|
|
56
|
-
if full_url.match url
|
57
|
-
time = group['time']
|
58
|
-
url_need_cache = true
|
59
|
-
@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)
|
60
|
-
redis_response = @redis_server.get(full_url)
|
61
|
-
if redis_response
|
62
|
-
response = redis_response
|
63
|
-
result_from_cache = true
|
64
|
-
|
65
|
-
headers = nil
|
66
|
-
if only_tracking
|
67
|
-
headers = {"Only-Tracking" => "true"}
|
68
|
-
#when is already in redis notify to California to register the object usage
|
69
|
-
#cali_response = self.send("#{@scope}_#{action}", "#{full_url}", headers, compatibility_options)
|
70
|
-
end
|
71
|
-
else
|
72
|
-
response = self.send("#{@scope}_#{action}", "#{full_url}", nil, compatibility_options)
|
73
|
-
@redis_server.setex(full_url,time,response)
|
74
|
-
end
|
75
|
-
break
|
76
|
-
end
|
77
|
-
end
|
78
|
-
break if url_need_cache
|
79
|
-
end
|
80
|
-
end
|
36
|
+
base_url = @base_url unless base_url
|
37
|
+
uri = ''
|
81
38
|
|
82
|
-
|
83
|
-
|
84
|
-
|
39
|
+
if options&.class == Hash
|
40
|
+
need_encoding = %w[jfilters afilters rfilters]
|
41
|
+
found_options_with_encoding = options.keys.select { |key| need_encoding.include?(key.to_s.downcase) and options[key]&.class == Hash }
|
85
42
|
|
86
|
-
|
87
|
-
|
88
|
-
response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
|
89
|
-
elsif action === 'put' or action === 'patch' or action ==='update'
|
90
|
-
action = 'put'
|
91
|
-
response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
|
92
|
-
elsif action === 'delete' or action === 'destroy'
|
93
|
-
action = 'delete'
|
94
|
-
response = self.send("#{@scope}_#{action}", "#{full_url}", data, compatibility_options)
|
43
|
+
found_options_with_encoding.each do |key|
|
44
|
+
options[key] = CGI::escape(Base64.encode64(options[key].to_json))
|
95
45
|
end
|
96
46
|
|
97
|
-
|
98
|
-
|
99
|
-
begin
|
100
|
-
if result_from_cache
|
101
|
-
if @debug
|
102
|
-
puts "Method: #{action} \nURL: #{url} \nOptions: #{options.to_json} \nOnly tracking: #{only_tracking} \nResponse from: REDIS"
|
103
|
-
puts "Data: #{data.to_json}" if data
|
104
|
-
end
|
105
|
-
|
106
|
-
return JSON.parse(response)
|
107
|
-
else
|
108
|
-
|
109
|
-
if @debug
|
110
|
-
puts "Method: #{action} \nURL: #{url} \nOptions: #{options.to_json} \nOnly tracking: #{only_tracking} \nResponse from: CALI"
|
111
|
-
puts "Data: #{data.to_json}" if data
|
112
|
-
end
|
113
|
-
|
114
|
-
return JSON.parse(response.body)
|
115
|
-
end
|
116
|
-
rescue
|
117
|
-
return response
|
118
|
-
end
|
47
|
+
uri = Addressable::URI.new
|
48
|
+
uri.query_values = options
|
119
49
|
end
|
120
50
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
51
|
+
full_url = "#{@host}#{base_url}#{url}#{uri}"
|
52
|
+
response = nil
|
53
|
+
|
54
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
55
|
+
config = YAML.safe_load template.result(binding)
|
56
|
+
result_from_cache = false
|
57
|
+
|
58
|
+
if action === 'get'
|
59
|
+
url_need_cache = false
|
60
|
+
|
61
|
+
if config['redis_cache']['use_cache']
|
62
|
+
config['redis_cache']['groups'].each do |group|
|
63
|
+
group['urls'].each do |url|
|
64
|
+
if full_url.match url
|
65
|
+
time = group['time']
|
66
|
+
url_need_cache = true
|
67
|
+
@redis_server = Redis.new(
|
68
|
+
host: config['redis_cache']['redis_host'],
|
69
|
+
port: config['redis_cache']['redis_port'] ? config['redis_cache']['redis_port'] : 6379,
|
70
|
+
db: config['redis_cache']['redis_db'] ? config['redis_cache']['redis_db'] : 1
|
71
|
+
)
|
72
|
+
redis_response = @redis_server.get(full_url)
|
73
|
+
|
74
|
+
if redis_response
|
75
|
+
response = redis_response
|
76
|
+
result_from_cache = true
|
77
|
+
|
78
|
+
if only_tracking
|
79
|
+
# headers = { 'Only-Tracking' => 'true' }
|
80
|
+
#when is already in redis notify to California to register the object usage
|
81
|
+
#cali_response = self.send("#{@scope}_#{action}", full_url, headers, compatibility_options)
|
82
|
+
end
|
83
|
+
else
|
84
|
+
response = self.send("#{@scope}_#{action}", full_url, nil, compatibility_options)
|
85
|
+
@redis_server.setex(full_url, time, response)
|
86
|
+
end
|
87
|
+
break
|
88
|
+
end
|
141
89
|
end
|
142
|
-
route_array.push n
|
143
|
-
end
|
144
|
-
route = route_array.join("/")
|
145
90
|
|
146
|
-
|
147
|
-
slug = nil
|
148
|
-
uri = Addressable::URI.new
|
149
|
-
if action == 'get'
|
150
|
-
if args.first.class == Hash
|
151
|
-
uri.query_values = args.first
|
152
|
-
elsif args.first.class == String or Integer
|
153
|
-
slug = args.first
|
154
|
-
uri.query_values = args[1]
|
155
|
-
end
|
156
|
-
url = self.get_url(route, object, uri, slug)
|
157
|
-
response = self.send("#{@scope}_#{action}", url, nil, compatibility_options)
|
158
|
-
elsif action == 'post' or action == "create"
|
159
|
-
if args[1].class == Hash
|
160
|
-
uri.query_values = args[1]
|
161
|
-
end
|
162
|
-
url = self.get_url(route, object, uri, slug)
|
163
|
-
action = 'post'
|
164
|
-
data = args[0]
|
165
|
-
response = self.send("#{@scope}_#{action}", url, {data: data}, compatibility_options)
|
166
|
-
elsif action == 'put' or action == "update"
|
167
|
-
if args.first.class == String or Integer
|
168
|
-
slug = args.first
|
169
|
-
uri.query_values = args[2]
|
91
|
+
break if url_need_cache
|
170
92
|
end
|
171
|
-
url = self.get_url(route, object, uri, slug)
|
172
|
-
action = 'put'
|
173
|
-
id = args[0]
|
174
|
-
data = args[1]
|
175
|
-
response = self.send("#{@scope}_#{action}", "#{url}", {data: data}, compatibility_options)
|
176
93
|
end
|
177
94
|
|
178
|
-
|
179
|
-
|
180
|
-
elsif response.response.code == "500"
|
181
|
-
raise 'InternalServerError'
|
182
|
-
end
|
183
|
-
return JSON.parse(response.body)
|
184
|
-
end
|
185
|
-
|
186
|
-
def get_url(route, object, uri, slug = nil)
|
187
|
-
if (slug)
|
188
|
-
return "#{@host}#{@base_url}/#{route}/#{object}/#{slug}#{uri}"
|
189
|
-
else
|
190
|
-
return "#{@host}#{@base_url}/#{route}/#{object}#{uri}"
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
def replacements
|
195
|
-
return [
|
196
|
-
{old_value: '_', new_value: '-'},
|
197
|
-
{old_value: 'people', new_value: 'crm'},
|
198
|
-
{old_value: 'store', new_value: 'ecommerce'}
|
199
|
-
]
|
200
|
-
end
|
201
|
-
|
202
|
-
def set_scope(scope)
|
203
|
-
@scope = scope
|
204
|
-
if scope == "public"
|
205
|
-
@base_url = "/api/v1"
|
206
|
-
elsif scope == "contact"
|
207
|
-
@base_url = "/api/v1"
|
208
|
-
elsif scope == "user"
|
209
|
-
@base_url = "/api/user/v1"
|
210
|
-
else
|
211
|
-
@scope = "public"
|
212
|
-
@base_url = "/api/v1"
|
95
|
+
unless url_need_cache
|
96
|
+
response = self.send("#{@scope}_#{action}", full_url, nil, compatibility_options)
|
213
97
|
end
|
214
|
-
end
|
215
98
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
#
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
# end
|
226
|
-
headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
|
99
|
+
elsif action === 'create' or action === 'post'
|
100
|
+
action = 'post'
|
101
|
+
response = self.send("#{@scope}_#{action}", full_url, data, compatibility_options)
|
102
|
+
elsif action === 'put' or action === 'patch' or action === 'update'
|
103
|
+
action = 'put'
|
104
|
+
response = self.send("#{@scope}_#{action}", full_url, data, compatibility_options)
|
105
|
+
elsif action === 'delete' or action === 'destroy'
|
106
|
+
action = 'delete'
|
107
|
+
response = self.send("#{@scope}_#{action}", full_url, data, compatibility_options)
|
227
108
|
end
|
228
109
|
|
229
|
-
|
230
|
-
def http_post(url, headers = nil, data = nil)
|
231
|
-
# if @debug
|
232
|
-
# puts "Url:"
|
233
|
-
# puts url
|
234
|
-
# puts "Headers:"
|
235
|
-
# puts headers
|
236
|
-
# puts "Data:"
|
237
|
-
# puts data
|
238
|
-
# puts "Method: post"
|
239
|
-
# end
|
240
|
-
headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data)
|
241
|
-
end
|
110
|
+
verify_response_status(response, config['sdk']['ignore_http_errors'])
|
242
111
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
# puts "Headers:"
|
249
|
-
# puts headers
|
250
|
-
# puts "Data:"
|
251
|
-
# puts data
|
252
|
-
# puts "Method: put"
|
253
|
-
# end
|
254
|
-
headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
|
255
|
-
end
|
256
|
-
|
257
|
-
# Simple HTTP DELETE
|
258
|
-
def http_delete(url, headers = nil, data = nil)
|
259
|
-
# if @debug
|
260
|
-
# puts "Url:"
|
261
|
-
# puts url
|
262
|
-
# puts "Headers:"
|
263
|
-
# puts headers
|
264
|
-
# puts "Data:"
|
265
|
-
# puts data
|
266
|
-
# puts "Method: delete"
|
267
|
-
# end
|
268
|
-
headers ? HTTParty.delete(url, :headers=> headers, :body => data) : HTTParty.delete(url, :body => data)
|
269
|
-
end
|
270
|
-
|
271
|
-
# Start contact context
|
272
|
-
def contact_get(url, headers = nil, compatibility_options)
|
273
|
-
h = {
|
274
|
-
"ApiKey" => @api_key,
|
275
|
-
"Accept" => "application/json",
|
276
|
-
"ContactToken" => @contact_token_id
|
277
|
-
}
|
278
|
-
h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
|
279
|
-
h["Authorization"] = "Bearer #{@session_token}" if @session_token
|
280
|
-
|
281
|
-
if headers
|
282
|
-
headers.each do |k,v|
|
283
|
-
h[k] = v
|
112
|
+
begin
|
113
|
+
if result_from_cache
|
114
|
+
if @debug
|
115
|
+
puts "Method: #{action} \nURL: #{url} \nOptions: #{options&.to_json} \nOnly tracking: #{only_tracking} \nResponse from: REDIS"
|
116
|
+
puts "Data: #{data.to_json}" if data
|
284
117
|
end
|
285
|
-
end
|
286
118
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
def contact_post(url, data, compatibility_options)
|
291
|
-
headers = {
|
292
|
-
"ApiKey" => @api_key,
|
293
|
-
"Accept" => "application/json",
|
294
|
-
"ContactToken" => @contact_token_id
|
295
|
-
}
|
119
|
+
return JSON.parse(response)
|
120
|
+
else
|
296
121
|
|
297
|
-
|
298
|
-
|
122
|
+
if @debug
|
123
|
+
puts "Method: #{action} \nURL: #{url} \nOptions: #{options&.to_json} \nOnly tracking: #{only_tracking} \nResponse from: CALI"
|
124
|
+
puts "Data: #{data.to_json}" if data
|
125
|
+
end
|
299
126
|
|
300
|
-
|
127
|
+
return JSON.parse(response&.body)
|
128
|
+
end
|
129
|
+
rescue
|
130
|
+
return response
|
301
131
|
end
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
132
|
+
end
|
133
|
+
|
134
|
+
def method_missing(name, *args, &block)
|
135
|
+
name.to_s.include?('__') ? separator = '__' : separator = '_'
|
136
|
+
# split the name to identify their elements
|
137
|
+
name_splitted = name.to_s.split(separator)
|
138
|
+
# count the elements
|
139
|
+
name_len = name_splitted.size
|
140
|
+
# the action always be the first element
|
141
|
+
action = name_splitted.first
|
142
|
+
raise 'NoActionError' unless %w[get create post update put delete destroy verify_response_status].include?(action)
|
143
|
+
# the object always be the last element
|
144
|
+
object = separator == '__' ? name_splitted.last.gsub('_', '-') : name_splitted.last
|
145
|
+
# get intermediate url elements
|
146
|
+
route_array = []
|
147
|
+
(name_len - 1).times do |n|
|
148
|
+
next if n === 0 or n === name_len - 1
|
149
|
+
|
150
|
+
n = name_splitted[n]
|
151
|
+
self.replacements.each do |object|
|
152
|
+
n = n.gsub(object[:old_value], object[:new_value])
|
153
|
+
end
|
154
|
+
route_array.push n
|
313
155
|
end
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
end
|
156
|
+
route = route_array.join('/')
|
157
|
+
|
158
|
+
slug = nil
|
159
|
+
response = nil
|
160
|
+
uri = Addressable::URI.new
|
161
|
+
|
162
|
+
if action == 'get'
|
163
|
+
if args.first.class == Hash
|
164
|
+
uri.query_values = args.first
|
165
|
+
elsif args.first.class == String or Integer
|
166
|
+
slug = args.first
|
167
|
+
uri.query_values = args[1]
|
327
168
|
end
|
328
169
|
|
329
|
-
self.
|
330
|
-
|
170
|
+
url = self.get_url(route, object, uri, slug)
|
171
|
+
response = self.send("#{@scope}_#{action}", url, nil, compatibility_options)
|
172
|
+
elsif action == 'post' or action == 'create'
|
173
|
+
if args[1].class == Hash
|
174
|
+
uri.query_values = args[1]
|
175
|
+
end
|
331
176
|
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
177
|
+
url = self.get_url(route, object, uri, slug)
|
178
|
+
action = 'post'
|
179
|
+
data = args[0]
|
180
|
+
response = self.send("#{@scope}_#{action}", url, { data: data }, compatibility_options)
|
181
|
+
elsif action == 'put' or action == 'update'
|
182
|
+
if args.first.class == String or Integer
|
183
|
+
slug = args.first
|
184
|
+
uri.query_values = args[2]
|
185
|
+
end
|
339
186
|
|
340
|
-
self.
|
187
|
+
url = self.get_url(route, object, uri, slug)
|
188
|
+
action = 'put'
|
189
|
+
data = args[1]
|
190
|
+
response = self.send("#{@scope}_#{action}", "#{url}", { data: data }, compatibility_options)
|
341
191
|
end
|
342
192
|
|
343
|
-
|
344
|
-
headers = {
|
345
|
-
"Accept" => "application/json",
|
346
|
-
"ApiKey" => @api_key
|
347
|
-
}
|
348
|
-
headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
|
349
|
-
headers["Authorization"] = "Bearer #{@session_token}" if @session_token
|
193
|
+
verify_response_status(response, config['sdk']['ignore_http_errors'])
|
350
194
|
|
351
|
-
|
352
|
-
|
195
|
+
response ? JSON.parse(response.body) : nil
|
196
|
+
end
|
353
197
|
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
}
|
359
|
-
headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
|
360
|
-
headers["Authorization"] = "Bearer #{@session_token}" if @session_token
|
361
|
-
|
362
|
-
self.http_delete(url, headers, data)
|
198
|
+
def get_url(route, object, uri, slug = nil)
|
199
|
+
if slug
|
200
|
+
"#{@host}#{@base_url}/#{route}/#{object}/#{slug}#{uri}"
|
201
|
+
else
|
202
|
+
"#{@host}#{@base_url}/#{route}/#{object}#{uri}"
|
363
203
|
end
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
}
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
204
|
+
end
|
205
|
+
|
206
|
+
def replacements
|
207
|
+
[
|
208
|
+
{ old_value: '_', new_value: '-' },
|
209
|
+
{ old_value: 'people', new_value: 'crm' },
|
210
|
+
{ old_value: 'store', new_value: 'ecommerce' }
|
211
|
+
]
|
212
|
+
end
|
213
|
+
|
214
|
+
def set_scope(scope)
|
215
|
+
@scope = scope
|
216
|
+
if scope === 'public' or scope === 'contact'
|
217
|
+
@base_url = '/api/v1'
|
218
|
+
elsif scope === 'user'
|
219
|
+
@base_url = '/api/user/v1'
|
220
|
+
else
|
221
|
+
@scope = 'public'
|
222
|
+
@base_url = '/api/v1'
|
381
223
|
end
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
224
|
+
end
|
225
|
+
|
226
|
+
##### HTTP CLIENTS ######
|
227
|
+
# Simple HTTP GET
|
228
|
+
def http_get(url, headers = nil)
|
229
|
+
HTTParty.get(url, headers: headers)
|
230
|
+
end
|
231
|
+
|
232
|
+
# Simple HTTP POST
|
233
|
+
def http_post(url, headers = nil, data = nil)
|
234
|
+
HTTParty.post(url, headers: headers, body: data)
|
235
|
+
end
|
236
|
+
|
237
|
+
# Simple HTTP PUT
|
238
|
+
def http_put(url, headers = nil, data = nil)
|
239
|
+
HTTParty.put(url, headers: headers, body: data)
|
240
|
+
end
|
241
|
+
|
242
|
+
# Simple HTTP DELETE
|
243
|
+
def http_delete(url, headers = nil, data = nil)
|
244
|
+
HTTParty.delete(url, headers: headers, body: data)
|
245
|
+
end
|
246
|
+
|
247
|
+
# Start contact context
|
248
|
+
def contact_get(url, headers = nil, compatibility_options)
|
249
|
+
h = set_headers(compatibility_options, headers)
|
250
|
+
|
251
|
+
self.http_get(url, h)
|
252
|
+
end
|
253
|
+
|
254
|
+
def contact_post(url, data, compatibility_options)
|
255
|
+
headers = set_headers(compatibility_options)
|
256
|
+
|
257
|
+
self.http_post(url, headers, data)
|
258
|
+
end
|
259
|
+
|
260
|
+
def contact_put(url, data, compatibility_options)
|
261
|
+
headers = set_headers(compatibility_options)
|
262
|
+
|
263
|
+
self.http_put(url, headers, data)
|
264
|
+
end
|
265
|
+
|
266
|
+
# Start User context
|
267
|
+
def user_get(url, headers = nil, compatibility_options)
|
268
|
+
h = set_headers(compatibility_options, headers)
|
269
|
+
|
270
|
+
self.http_get(url, h)
|
271
|
+
end
|
272
|
+
|
273
|
+
def user_post(url, data, compatibility_options)
|
274
|
+
self.http_post(url, set_headers(compatibility_options), data)
|
275
|
+
end
|
276
|
+
|
277
|
+
def user_put(url, data, compatibility_options)
|
278
|
+
self.http_put(url, set_headers(compatibility_options), data)
|
279
|
+
end
|
280
|
+
|
281
|
+
def user_delete(url, data, compatibility_options)
|
282
|
+
self.http_delete(url, set_headers(compatibility_options), data)
|
283
|
+
end
|
284
|
+
|
285
|
+
# End User Context
|
286
|
+
|
287
|
+
def public_get(url, headers = nil, compatibility_options)
|
288
|
+
self.http_get(url, set_headers(compatibility_options, headers))
|
289
|
+
end
|
290
|
+
|
291
|
+
def public_post(url, headers = nil, data, compatibility_options)
|
292
|
+
self.http_post(url, set_headers(compatibility_options, headers), data)
|
293
|
+
end
|
294
|
+
|
295
|
+
def public_put(url, headers = nil, data, compatibility_options)
|
296
|
+
self.http_put(url, set_headers(compatibility_options, headers), data)
|
297
|
+
end
|
298
|
+
|
299
|
+
def set_headers(compatibility_options, headers = nil)
|
300
|
+
h = {
|
301
|
+
'Accept' => 'application/json',
|
302
|
+
'ApiKey' => @api_key
|
303
|
+
}
|
304
|
+
h['Content-Type'] = 'application/json' unless compatibility_options['no_content_type']
|
305
|
+
h['ContactToken'] = @contact_token_id if @contact_token_id
|
306
|
+
h['Visit-Id'] = @visit_id if @visit_id
|
307
|
+
h['Authorization'] = "Bearer #{@session_token}" if @session_token
|
308
|
+
|
309
|
+
if headers
|
310
|
+
headers.each do |k, v|
|
311
|
+
h[k] = v
|
395
312
|
end
|
396
|
-
|
397
|
-
self.http_post(url, h, data)
|
398
313
|
end
|
399
314
|
|
400
|
-
|
401
|
-
|
402
|
-
"Accept" => "application/json",
|
403
|
-
"ApiKey" => @api_key
|
404
|
-
}
|
405
|
-
h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
|
406
|
-
h["ContactToken"] = @contact_token_id if @contact_token_id
|
407
|
-
if headers
|
408
|
-
headers.each do |k,v|
|
409
|
-
h[k] = v
|
410
|
-
end
|
411
|
-
end
|
412
|
-
|
413
|
-
self.http_put(url, h, data)
|
414
|
-
end
|
315
|
+
h
|
316
|
+
end
|
415
317
|
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
318
|
+
def verify_response_status(response, ignore_http_errors)
|
319
|
+
# Verify if the response is cached
|
320
|
+
unless response.kind_of? String
|
321
|
+
# Raise an error if response code is not 2xx
|
322
|
+
http_status = response&.response&.code&.to_i || 500
|
323
|
+
is_success = (http_status >= 200 and http_status < 300)
|
422
324
|
|
423
|
-
|
424
|
-
|
425
|
-
|
325
|
+
if !is_success and !ignore_http_errors
|
326
|
+
title = "Request failed with status #{http_status}"
|
327
|
+
detail = response&.response&.message || 'Unknown error'
|
426
328
|
|
427
|
-
|
428
|
-
|
329
|
+
puts "Error detected: #{http_status}" if @debug
|
330
|
+
error_class = Errors::DynamicError.new(self, title, detail, http_status, response&.parsed_response)
|
429
331
|
|
430
|
-
|
332
|
+
raise error_class if @debug
|
431
333
|
|
432
|
-
|
433
|
-
end
|
334
|
+
raise error_class.error
|
434
335
|
end
|
435
336
|
end
|
337
|
+
end
|
436
338
|
|
437
339
|
end
|
438
340
|
end
|