mints 0.0.28 → 0.0.30

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.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/lib/client.rb +287 -385
  3. data/lib/contact/config/appointments.rb +201 -0
  4. data/lib/contact/config/config.rb +7 -0
  5. data/lib/contact/content/content.rb +7 -0
  6. data/lib/contact/content/conversations.rb +121 -0
  7. data/lib/contact/ecommerce/ecommerce.rb +14 -0
  8. data/lib/contact/ecommerce/order_items.rb +19 -0
  9. data/lib/contact/ecommerce/order_items_groups.rb +95 -0
  10. data/lib/contact/ecommerce/orders.rb +132 -0
  11. data/lib/contact/ecommerce/vouchers.rb +18 -0
  12. data/lib/contact.rb +54 -603
  13. data/lib/errors.rb +4 -2
  14. data/lib/generators/mints_assets_controller.rb +4 -1
  15. data/lib/generators/mints_contact_controller.rb +4 -1
  16. data/lib/generators/mints_files_generator.rb +16 -13
  17. data/lib/generators/mints_link.rb +7 -4
  18. data/lib/generators/mints_public_controller.rb +4 -1
  19. data/lib/generators/mints_user_controller.rb +3 -0
  20. data/lib/generators/short_link_controller.rb +4 -1
  21. data/lib/mints/controllers/admin_base_controller.rb +5 -3
  22. data/lib/mints/controllers/base_api_controller.rb +6 -4
  23. data/lib/mints/controllers/base_controller.rb +5 -3
  24. data/lib/mints/controllers/concerns/mints_clients.rb +4 -2
  25. data/lib/mints/controllers/concerns/read_config_file.rb +3 -1
  26. data/lib/mints/controllers/contact_api_controller.rb +12 -10
  27. data/lib/mints/controllers/public_api_controller.rb +10 -8
  28. data/lib/mints/controllers/user_api_controller.rb +11 -9
  29. data/lib/mints/helpers/contact_auth_helper.rb +5 -3
  30. data/lib/mints/helpers/mints_helper.rb +39 -38
  31. data/lib/mints/helpers/proxy_controllers_methods.rb +24 -24
  32. data/lib/mints/helpers/user_auth_helper.rb +5 -3
  33. data/lib/mints.rb +12 -10
  34. data/lib/pub/config/attributes.rb +13 -0
  35. data/lib/pub/config/config.rb +15 -0
  36. data/lib/pub/config/public_folders.rb +51 -0
  37. data/lib/pub/config/tags.rb +42 -0
  38. data/lib/pub/config/taxonomies.rb +49 -0
  39. data/lib/pub/content/assets.rb +16 -0
  40. data/lib/pub/content/content.rb +22 -0
  41. data/lib/pub/content/content_bundles.rb +40 -0
  42. data/lib/pub/content/content_instance_versions.rb +56 -0
  43. data/lib/pub/content/content_instances.rb +37 -0
  44. data/lib/pub/content/forms.rb +47 -0
  45. data/lib/pub/content/stories.rb +44 -0
  46. data/lib/pub/content/story_versions.rb +46 -0
  47. data/lib/pub/ecommerce/ecommerce.rb +14 -0
  48. data/lib/pub/ecommerce/locations.rb +25 -0
  49. data/lib/pub/ecommerce/orders.rb +31 -0
  50. data/lib/pub/ecommerce/products.rb +45 -0
  51. data/lib/pub.rb +22 -461
  52. data/lib/user/config/api_keys.rb +63 -61
  53. data/lib/user/config/appointments.rb +217 -215
  54. data/lib/user/config/attribute_groups.rb +74 -72
  55. data/lib/user/config/attributes.rb +83 -81
  56. data/lib/user/config/calendars.rb +88 -86
  57. data/lib/user/config/config.rb +31 -63
  58. data/lib/user/config/public_folders.rb +105 -104
  59. data/lib/user/config/relationships.rb +135 -132
  60. data/lib/user/config/roles.rb +76 -79
  61. data/lib/user/config/seeds.rb +52 -51
  62. data/lib/user/config/system_settings.rb +50 -49
  63. data/lib/user/config/tags.rb +57 -59
  64. data/lib/user/config/taxonomies.rb +115 -119
  65. data/lib/user/config/teams.rb +2 -0
  66. data/lib/user/config/users.rb +2 -0
  67. data/lib/user/contacts/contacts.rb +2 -0
  68. data/lib/user/content/assets.rb +3 -3
  69. data/lib/user/content/content.rb +224 -223
  70. data/lib/user/content/content_instances.rb +4 -2
  71. data/lib/user/content/content_templates.rb +2 -0
  72. data/lib/user/content/conversations.rb +2 -0
  73. data/lib/user/content/dam.rb +2 -0
  74. data/lib/user/content/forms.rb +2 -0
  75. data/lib/user/content/message_templates.rb +2 -0
  76. data/lib/user/content/messages.rb +2 -0
  77. data/lib/user/content/pages.rb +4 -2
  78. data/lib/user/content/stories.rb +3 -3
  79. data/lib/user/content/story_templates.rb +5 -3
  80. data/lib/user/content/story_versions.rb +2 -0
  81. data/lib/user/crm/companies.rb +3 -1
  82. data/lib/user/crm/contacts.rb +2 -0
  83. data/lib/user/crm/crm.rb +2 -0
  84. data/lib/user/crm/deals.rb +2 -0
  85. data/lib/user/crm/favorites.rb +2 -0
  86. data/lib/user/crm/segments.rb +2 -0
  87. data/lib/user/crm/users.rb +2 -0
  88. data/lib/user/crm/workflow_step_objects.rb +2 -0
  89. data/lib/user/crm/workflow_steps.rb +2 -0
  90. data/lib/user/crm/workflows.rb +2 -0
  91. data/lib/user/ecommerce/ecommerce.rb +4 -0
  92. data/lib/user/ecommerce/item_prices.rb +2 -0
  93. data/lib/user/ecommerce/locations.rb +4 -2
  94. data/lib/user/ecommerce/order_items_groups.rb +5 -2
  95. data/lib/user/ecommerce/order_statuses.rb +2 -0
  96. data/lib/user/ecommerce/orders.rb +4 -1
  97. data/lib/user/ecommerce/price_lists.rb +2 -0
  98. data/lib/user/ecommerce/product_templates.rb +3 -1
  99. data/lib/user/ecommerce/product_variations.rb +2 -0
  100. data/lib/user/ecommerce/products.rb +3 -1
  101. data/lib/user/ecommerce/skus.rb +2 -0
  102. data/lib/user/ecommerce/taxes.rb +2 -0
  103. data/lib/user/ecommerce/variant_options.rb +2 -0
  104. data/lib/user/ecommerce/variant_values.rb +2 -0
  105. data/lib/user/ecommerce/vouchers.rb +90 -0
  106. data/lib/user/helpers/helpers.rb +2 -0
  107. data/lib/user/helpers/object_activities.rb +2 -0
  108. data/lib/user/helpers/object_folders.rb +2 -0
  109. data/lib/user/helpers/user_folders.rb +2 -0
  110. data/lib/user/marketing/marketing.rb +2 -0
  111. data/lib/user/profile/profile.rb +2 -0
  112. data/lib/user.rb +32 -41
  113. metadata +42 -16
  114. data/lib/user/config/importers.rb +0 -184
data/lib/client.rb CHANGED
@@ -1,438 +1,340 @@
1
- require "httparty"
2
- require "json"
3
- require "addressable"
4
- require "redis"
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
- 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
11
+ extend ActiveSupport::Concern
23
12
 
24
- def raw(action, url, options = nil, data = nil, base_url = nil, compatibility_options = {}, only_tracking = false)
25
- compatibility_options = {} if compatibility_options.nil?
13
+ include ReadConfigFile
26
14
 
27
- base_url = @base_url unless base_url
28
- uri = ""
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
- if options&.class == Hash
31
- need_encoding = %w[ jfilters afilters rfilters ]
32
- found_options_with_encoding = options.keys.select {|key| need_encoding.include?(key.to_s.downcase) and options[key]&.class == Hash}
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
- found_options_with_encoding.each do |key|
35
- options[key] = CGI::escape(Base64.encode64(options[key].to_json))
36
- end
30
+ self.set_scope(scope)
31
+ end
37
32
 
38
- uri = Addressable::URI.new
39
- uri.query_values = options
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
- full_url = "#{@host}#{base_url}#{url}#{uri}"
43
- response = nil
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
- unless url_need_cache
83
- response = self.send("#{@scope}_#{action}", "#{full_url}", nil, compatibility_options)
84
- end
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
- elsif action === 'create' or action === 'post'
87
- action = 'post'
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
- verify_response_status(response, config['sdk']['ignore_http_errors'])
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
- def method_missing(name, *args, &block)
122
- name.to_s.include?("__") ? separator = "__" : separator = "_"
123
- # split the name to identify their elements
124
- name_spplited = name.to_s.split(separator)
125
- # count the elments
126
- name_len = name_spplited.size
127
- # the action always be the first element
128
- action = name_spplited.first
129
- raise 'NoActionError' unless %w[get create post update put delete destroy verify_response_status].include?(action)
130
- # the object always be the last element
131
- object = separator == "__" ? name_spplited.last.gsub("_","-") : name_spplited.last
132
- # get intermediate url elements
133
- route_array = []
134
- (name_len-1).times do |n|
135
- if n == 0 or n == name_len-1
136
- next
137
- end
138
- n = name_spplited[n]
139
- self.replacements().each do |object|
140
- n = n.gsub(object[:old_value], object[:new_value])
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
- if response.response.code == "404"
179
- raise 'NotFoundError'
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
- ##### HTTP CLIENTS ######
217
- # Simple HTTP GET
218
- def http_get(url, headers = nil)
219
- # if @debug
220
- # puts "Url:"
221
- # puts url
222
- # puts "Headers:"
223
- # puts headers
224
- # puts "Method: get"
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
- # Simple HTTP POST
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
- # Simple HTTP PUT
244
- def http_put(url, headers = nil, data = nil)
245
- # if @debug
246
- # puts "Url:"
247
- # puts url
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
- self.http_get(url, h)
288
- end
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
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
298
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
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
- self.http_post(url, headers, data)
127
+ return JSON.parse(response&.body)
128
+ end
129
+ rescue
130
+ return response
301
131
  end
302
-
303
- def contact_put(url, data, compatibility_options)
304
- headers = {
305
- "ApiKey" => @api_key,
306
- "Accept" => "application/json",
307
- "ContactToken" => @contact_token_id
308
- }
309
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
310
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
311
-
312
- self.http_put(url, headers, data)
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
- # Start User context
316
- def user_get(url, headers = nil, compatibility_options)
317
- h = {
318
- "Accept" => "application/json",
319
- "ApiKey" => @api_key
320
- }
321
- h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
322
- h["Authorization"] = "Bearer #{@session_token}" if @session_token
323
- if headers
324
- headers.each do |k,v|
325
- h[k] = v
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.http_get(url, h)
330
- end
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
- def user_post(url, data, compatibility_options)
333
- headers = {
334
- "Accept" => "application/json",
335
- "ApiKey" => @api_key
336
- }
337
- headers["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
338
- headers["Authorization"] = "Bearer #{@session_token}" if @session_token
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.http_post(url, headers, data)
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
- def user_put(url, data, compatibility_options)
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
- self.http_put(url, headers, data)
352
- end
195
+ response ? JSON.parse(response.body) : nil
196
+ end
353
197
 
354
- def user_delete(url, data, compatibility_options)
355
- headers = {
356
- "Accept" => "application/json",
357
- "ApiKey" => @api_key
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
- # End User Context
365
-
366
- def public_get(url, headers = nil, compatibility_options)
367
- h = {
368
- "Accept" => "application/json",
369
- "ApiKey" => @api_key
370
- }
371
- h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
372
- h["ContactToken"] = @contact_token_id if @contact_token_id
373
- h["Visit-Id"] = @visit_id if @visit_id
374
- if headers
375
- headers.each do |k,v|
376
- h[k] = v
377
- end
378
- end
379
-
380
- self.http_get(url, h)
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
- def public_post(url, headers = nil, data, compatibility_options)
384
- h = {
385
- "Accept" => "application/json",
386
- "ApiKey" => @api_key
387
- }
388
- h["Content-Type"] = "application/json" unless compatibility_options['no_content_type']
389
- h["ContactToken"] = @contact_token_id if @contact_token_id
390
-
391
- if headers
392
- headers.each do |k,v|
393
- h[k] = v
394
- end
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
- def public_put(url, headers = nil, data, compatibility_options)
401
- h = {
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
- def verify_response_status(response, ignore_http_errors)
417
- # Verify if the response is cached
418
- unless response.kind_of? String
419
- # Raise an error if response code is not 2xx
420
- http_status = response&.response&.code&.to_i || 500
421
- is_success = (http_status >= 200 and http_status < 300)
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
- if !is_success and !ignore_http_errors
424
- title = "Request failed with status #{http_status}"
425
- detail = response&.response&.message || 'Unknown error'
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
- puts "Error detected: #{http_status}" if @debug
428
- error_class = Errors::DynamicError.new(self, title, detail, http_status, response&.parsed_response)
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
- raise error_class if @debug
332
+ raise error_class if @debug
431
333
 
432
- raise error_class.error
433
- end
334
+ raise error_class.error
434
335
  end
435
336
  end
337
+ end
436
338
 
437
339
  end
438
340
  end