mints 0.0.20 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,8 @@ class MintsFilesGenerator < Rails::Generators::Base
7
7
  copy_file 'mints_contact_controller.rb', './app/controllers/api/v1/mints_contact_controller.rb'
8
8
  copy_file 'mints_public_controller.rb', './app/controllers/api/v1/mints_public_controller.rb'
9
9
  copy_file 'mints_assets_controller.rb', './app/controllers/mints_assets_controller.rb'
10
+ copy_file 'short_link_controller.rb', './app/controllers/short_link_controller.rb'
11
+ copy_file 'mints_link.rb', './config/initializers/mints_link.rb'
10
12
  route <<-eos
11
13
  # Mints auto-generated routes (proxy to send request to mints.cloud)
12
14
  match '/public-assets/*path' => 'mints_assets#index', via: [:get, :post, :put, :patch, :delete]
@@ -18,6 +20,10 @@ class MintsFilesGenerator < Rails::Generators::Base
18
20
  match '/*path' => 'mints_public#index', via: [:get, :post, :put, :patch, :delete]
19
21
  end
20
22
  end
23
+ # Mints short link
24
+ get '/sl/visits', to: 'short_link#visits'
25
+ post '/sl/generate', to: 'short_link#generate'
26
+ get '/sl/:code', to: 'short_link#redirect'
21
27
  eos
22
28
  end
23
29
  end
@@ -0,0 +1,61 @@
1
+ class MintsLink
2
+ def initialize
3
+ @host = ENV['MONGO_HOST'] || '127.0.0.1'
4
+ @database = ENV['MONGO_DATABASE'] || 'mints'
5
+ @port = ENV['MONGO_PORT'] || '27017'
6
+ @client = Mongo::Client.new([ "#{@host}:#{@port}" ], :database => @database)
7
+ @short_links = @client[:short_links]
8
+ @sl_visits = @client[:sl_visits]
9
+ generate_indexes
10
+ end
11
+
12
+ def generate(url)
13
+ code = random_string.upcase
14
+ collection = @short_links
15
+ doc = {
16
+ url: url,
17
+ code: code
18
+ }
19
+
20
+ result = collection.insert_one(doc)
21
+ if result.n === 1
22
+ code
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ def get_url(code)
29
+ collection = @short_links
30
+ record = collection.find( { 'code' => code } ).first
31
+ record["url"]
32
+ end
33
+
34
+ def visit(code, url, contact_id, user_agent, ip)
35
+ collection = @sl_visits
36
+ doc = {
37
+ code: code,
38
+ url: url,
39
+ contact_id: contact_id,
40
+ user_agent: user_agent,
41
+ ip: ip
42
+ }
43
+ result = collection.insert_one(doc)
44
+ result.n === 1
45
+ end
46
+
47
+ def get_visits(filter, page = 1, pageSize = 1000)
48
+ collection = @sl_visits
49
+ collection.find(filter).sort( {_id: 1}).skip(page * pageSize - pageSize).limit(pageSize)
50
+ end
51
+
52
+ private
53
+ def random_string(length = 6)
54
+ rand(32**length).to_s(32)
55
+ end
56
+
57
+ def generate_indexes
58
+ collection = @short_links
59
+ collection.indexes.create_one({ code: 1 }, {unique: true })
60
+ end
61
+ end
@@ -0,0 +1,41 @@
1
+ class ShortLinkController < Mints::BaseController
2
+ skip_before_action :verify_authenticity_token
3
+ def redirect
4
+ code = params[:code]
5
+ mints_link = MintsLink.new
6
+ url = mints_link.get_url(code)
7
+ contact_id = cookies[:mints_contact_id]
8
+ user_agent = request.user_agent
9
+ ip = request.remote_ip
10
+ mints_link.visit(code, url, contact_id, user_agent, ip)
11
+ redirect_to url
12
+ end
13
+
14
+ def generate
15
+ url = params[:url]
16
+ if url =~ /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/
17
+ mints_link = MintsLink.new
18
+ code = mints_link.generate(url)
19
+ render json: { code: code }
20
+ else
21
+ render json: false
22
+ end
23
+ end
24
+
25
+ def visits
26
+ code = params[:code]
27
+ contact_id = params[:contact_id]
28
+ page = params[:page] ? params[:page].to_i : 1
29
+ page_size = params[:page_size] ? params[:page_size].to_i : 1000
30
+ filter = {}
31
+ if code
32
+ filter['code'] = code
33
+ end
34
+ if contact_id
35
+ filter['contact_id'] = contact_id
36
+ end
37
+ mints_link = MintsLink.new
38
+ visits = mints_link.get_visits(filter, page, page_size)
39
+ render json: { data: {visits: visits }}
40
+ end
41
+ end
data/lib/mints_helper.rb CHANGED
@@ -10,9 +10,9 @@ module MintsHelper
10
10
  #
11
11
  def get_query_results(url, options = nil, use_post = true)
12
12
  if use_post
13
- return @client.raw("post", "#{url}/query", options)
13
+ return @client.raw("post", "#{url}/query", options)
14
14
  else
15
- return @client.raw("get", url, options)
15
+ return @client.raw("get", url, options)
16
16
  end
17
17
  end
18
18
 
data/lib/pub.rb CHANGED
@@ -103,7 +103,7 @@ module Mints
103
103
  # "user_agent" => "User Agent",
104
104
  # "fullpath" => "https://fullpath/example"
105
105
  # }
106
- # @mints_pub.register_visit(request, request["remote_ip"], request["user_agent"], request["fullpath"])
106
+ # @data = @mints_pub.register_visit(request, request["remote_ip"], request["user_agent"], request["fullpath"])
107
107
  def register_visit(request, ip = nil, user_agent = nil, url = nil)
108
108
  data = {
109
109
  ip_address: ip || request.remote_ip,
@@ -123,11 +123,13 @@ module Mints
123
123
  # time:: (Integer) -- The visitor's browser user agent.
124
124
  #
125
125
  # ==== Example
126
- # @mints_pub.register_visit_timer("60da2325d29acc7e55684472", 4)
126
+ # @data = @mints_pub.register_visit_timer("60da2325d29acc7e55684472", 4)
127
127
  def register_visit_timer(visit, time)
128
128
  return @client.raw("get", "/register-visit-timer?visit=#{visit}&time=#{time}")
129
129
  end
130
130
 
131
+ ### V1/CONTENT ###
132
+
131
133
  ##
132
134
  # === Get Asset Info.
133
135
  # Get a description of an Asset.
@@ -136,22 +138,35 @@ module Mints
136
138
  # slug:: (String) -- It's the string identifier of the asset.
137
139
  #
138
140
  # ==== Example
139
- # @mints_pub.get_asset_info("asset_slug")
141
+ # @data = @mints_pub.get_asset_info("asset_slug")
140
142
  def get_asset_info(slug)
141
143
  return @client.raw("get", "/content/asset-info/#{slug}")
142
144
  end
143
-
145
+
144
146
  ##
145
147
  # === Get Stories.
146
148
  # Get a collection of stories.
147
149
  #
148
150
  # ==== Parameters
149
151
  # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
152
+ # use_post:: (Boolean) -- Variable to determine if the request is by 'post' or 'get' functions.
150
153
  #
151
- # ==== Example
152
- # @mints_pub.get_stories
153
- def get_stories(options = nil)
154
- return @client.raw("get", "/content/stories", options)
154
+ # ==== First Example
155
+ # @data = @mints_pub.get_stories
156
+ #
157
+ # ==== Second Example
158
+ # options = {
159
+ # "fields": "id, title"
160
+ # }
161
+ # @data = @mints_pub.get_stories(options)
162
+ #
163
+ # ==== Third Example
164
+ # options = {
165
+ # "fields": "id, title"
166
+ # }
167
+ # @data = @mints_pub.get_stories(options, false)
168
+ def get_stories(options = nil, use_post = true)
169
+ return get_query_results("/content/stories", options, use_post)
155
170
  end
156
171
 
157
172
  ##
@@ -162,8 +177,11 @@ module Mints
162
177
  # slug:: (String) -- It's the string identifier generated by Mints.
163
178
  # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
164
179
  #
165
- # ==== Example
166
- # @mints_pub.get_story("story_slug")
180
+ # ==== First Example
181
+ # @data = @mints_pub.get_story("story_slug")
182
+ #
183
+ # ==== Second Example
184
+ # @data = @mints_pub.get_story("story_slug", options.to_json)
167
185
  def get_story(slug, options = nil)
168
186
  return @client.raw("get", "/content/stories/#{slug}", options, nil, nil, nil, true)
169
187
  end
@@ -172,13 +190,10 @@ module Mints
172
190
  # === Get Forms.
173
191
  # Get a collection of forms.
174
192
  #
175
- # ==== Parameters
176
- # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
177
- #
178
193
  # ==== Example
179
- # @mints_pub.get_forms
180
- def get_forms(options = nil)
181
- return @client.raw("get", "/content/forms", options)
194
+ # @data = @mints_pub.get_forms
195
+ def get_forms
196
+ return @client.raw("get", "/content/forms")
182
197
  end
183
198
 
184
199
  ##
@@ -187,12 +202,11 @@ module Mints
187
202
  #
188
203
  # ==== Parameters
189
204
  # slug:: (String) -- It's the string identifier generated by Mints.
190
- # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
191
205
  #
192
206
  # ==== Example
193
- # @mints_pub.get_form("form_slug")
194
- def get_form(slug, options = nil)
195
- return @client.raw("get", "/content/forms/#{slug}", options, nil, nil, nil, true)
207
+ # @data = @mints_pub.get_form("form_slug")
208
+ def get_form(slug)
209
+ return @client.raw("get", "/content/forms/#{slug}", nil, nil, nil, nil, true)
196
210
  end
197
211
 
198
212
  ##
@@ -222,7 +236,20 @@ module Mints
222
236
  #
223
237
  # ==== Parameters
224
238
  # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
225
- def get_content_instances(options = nil)
239
+ #
240
+ # ==== First Example
241
+ # options = {
242
+ # "template": "content_instance_template_slug"
243
+ # }
244
+ # @data = @mints_pub.get_content_instances(options)
245
+ #
246
+ # ==== Second Example
247
+ # options = {
248
+ # "template": "content_instance_template_slug",
249
+ # "sort": "-id"
250
+ # }
251
+ # @data = @mints_pub.get_content_instances(options)
252
+ def get_content_instances(options)
226
253
  return @client.raw("get", "/content/content-instances", options)
227
254
  end
228
255
 
@@ -232,15 +259,14 @@ module Mints
232
259
  #
233
260
  # ==== Parameters
234
261
  # slug:: (String) -- It's the string identifier generated by Mints.
235
- # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
236
262
  #
237
263
  # ==== Example
238
- # @mints_pub.get_content_instance("content_instance_slug")
239
- def get_content_instance(slug, options = nil)
240
- return @client.raw("get", "/content/content-instances/#{slug}", options)
264
+ # @data = @mints_pub.get_content_instance("content_instance_slug")
265
+ def get_content_instance(slug)
266
+ return @client.raw("get", "/content/content-instances/#{slug}")
241
267
  end
242
268
 
243
- #TODO: This method is commented for future use
269
+ #Note: This method is commented for future use
244
270
  ##
245
271
  # === Get Content Pages.
246
272
  # Get all content pages.
@@ -257,38 +283,57 @@ module Mints
257
283
  #
258
284
  # ==== Parameters
259
285
  # slug:: (String) -- It's the string identifier generated by Mints.
260
- # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
261
286
  #
262
287
  # ==== Example
263
- # @mints_pub.get_content_page("test-page")
264
- def get_content_page(slug, options = nil)
265
- return @client.raw("get", "/content/content-pages/#{slug}", options, nil, nil, nil, true)
288
+ # @data = @mints_pub.get_content_page("test-page")
289
+ def get_content_page(slug)
290
+ return @client.raw("get", "/content/content-pages/#{slug}", nil, nil, nil, nil, true)
266
291
  end
267
292
 
293
+ ### V1/ECOMMERCE ###
294
+
268
295
  ##
269
296
  # === Get Locations.
270
297
  # Get all locations.
271
298
  #
272
299
  # ==== Parameters
273
300
  # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
301
+ # use_post:: (Boolean) -- Variable to determine if the request is by 'post' or 'get' functions.
274
302
  #
275
- # ==== Example
276
- # @mints_pub.get_locations
277
- def get_locations(options = nil)
278
- return @client.raw("get", "/ecommerce/locations", options)
303
+ # ==== First Example
304
+ # @data = @mints_pub.get_locations
305
+ #
306
+ # ==== Second Example
307
+ # options = { "fields": "title" }
308
+ # @data = @mints_pub.get_locations(options)
309
+ #
310
+ # ==== Third Example
311
+ # options = { "fields": "title" }
312
+ # @data = @mints_pub.get_locations(options, false)
313
+ def get_locations(options = nil, use_post = true)
314
+ return get_query_results("/ecommerce/locations", options, use_post)
279
315
  end
280
-
316
+
281
317
  ##
282
318
  # === Get Products.
283
319
  # Get a collection of products.
284
320
  #
285
321
  # ==== Parameters
286
322
  # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
323
+ # use_post:: (Boolean) -- Variable to determine if the request is by 'post' or 'get' functions.
287
324
  #
288
- # ==== Example
289
- # @mints_pub.get_products
290
- def get_products(options = nil)
291
- return @client.raw("get", "/ecommerce/products", options)
325
+ # ==== First Example
326
+ # @data = @mints_pub.get_products
327
+ #
328
+ # ==== Second Example
329
+ # options = { "fields": "title" }
330
+ # @data = @mints_pub.get_products(options)
331
+ #
332
+ # ==== Third Example
333
+ # options = { "fields": "title" }
334
+ # @data = @mints_pub.get_products(options, false)
335
+ def get_products(options = nil, use_post = true)
336
+ return get_query_results("/ecommerce/products", options, use_post)
292
337
  end
293
338
 
294
339
  ##
@@ -299,43 +344,66 @@ module Mints
299
344
  # slug:: (String) -- It's the string identifier generated by Mints.
300
345
  # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
301
346
  #
302
- # ==== Example
303
- # @mints_pub.get_product("product_slug")
347
+ # ==== First Example
348
+ # @data = @mints_pub.get_product("product_slug")
349
+ #
350
+ # ==== Second Example
351
+ # options = {
352
+ # "fields": "id, slug"
353
+ # }
354
+ # @data = @mints_pub.get_product("lego-set", options)
304
355
  def get_product(slug, options = nil)
305
356
  return @client.raw("get", "/ecommerce/products/#{slug}", options, nil, nil, nil, true)
306
357
  end
307
358
 
359
+ ### V1/CONFIG ###
360
+
308
361
  ##
309
- # === Get categories.
310
- # Get a collection of categories.
362
+ # === Get Public Folders.
363
+ # Get a collection of public folders.
311
364
  #
312
365
  # ==== Parameters
313
- # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
366
+ # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
314
367
  #
315
- # ==== Example
368
+ # ==== First Example
316
369
  # options = {
317
- # "object_type": "stories"
370
+ # "object_type": "products"
371
+ # }
372
+ # @data = @mints_pub.get_public_folders(options)
373
+ #
374
+ # ==== Second Example
375
+ # options = {
376
+ # "object_type": "products",
377
+ # "fields": "id",
378
+ # "sort": "-id"
318
379
  # }
319
- # @mints_pub.get_categories(options)
320
- def get_categories(options = nil)
321
- return @client.raw("get", "/config/categories", options)
380
+ # @data = @mints_pub.get_public_folders(options)
381
+ def get_public_folders(options)
382
+ return @client.raw("get", "/config/public-folders", options)
322
383
  end
323
384
 
324
385
  ##
325
- # === Get Category.
326
- # Get a single category.
386
+ # === Get Public Folder.
387
+ # Get a public folder info.
327
388
  #
328
389
  # ==== Parameters
329
390
  # slug:: (String) -- It's the string identifier generated by Mints.
330
391
  # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
331
392
  #
332
- # ==== Example
393
+ # ==== First Example
333
394
  # options = {
334
- # "object_type": "locations"
395
+ # "object_type": "products"
335
396
  # }
336
- # @mints_pub.get_category("asset_slug", options)
337
- def get_category(slug, options = nil)
338
- return @client.raw("get", "/config/categories/#{slug}", options)
397
+ # @data = @mints_pub.get_public_folder('yellow', options)
398
+ #
399
+ # ==== Second Example
400
+ # options = {
401
+ # "object_type": "products",
402
+ # "fields": "id, title"
403
+ # }
404
+ # @data = @mints_pub.get_public_folder('yellow', options)
405
+ def get_public_folder(slug, options)
406
+ return @client.raw("get", "/config/public-folders/#{slug}", options)
339
407
  end
340
408
 
341
409
  ##
@@ -345,8 +413,14 @@ module Mints
345
413
  # ==== Parameters
346
414
  # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
347
415
  #
348
- # ==== Example
349
- # @mints_pub.get_tags
416
+ # ==== First Example
417
+ # @data = @mints_pub.get_tags
418
+ #
419
+ # ==== Second Example
420
+ # options = {
421
+ # "fields": "id, tag"
422
+ # }
423
+ # @data = @mints_pub.get_tags(options)
350
424
  def get_tags(options = nil)
351
425
  return @client.raw("get", "/config/tags", options)
352
426
  end
@@ -359,23 +433,42 @@ module Mints
359
433
  # slug:: (String) -- It's the string identifier generated by Mints.
360
434
  # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
361
435
  #
362
- # ==== Example
363
- # @mints_pub.get_tag("tag_slug")
436
+ # ==== First Example
437
+ # @data = @mints_pub.get_tag("tag_slug")
438
+ #
439
+ # ==== Second Example
440
+ # options = {
441
+ # "fields": "id, tag"
442
+ # }
443
+ # @data = @mints_pub.get_tag("velit-0", options)
364
444
  def get_tag(slug, options = nil)
365
445
  return @client.raw("get", "/config/tags/#{slug}", options)
366
446
  end
367
-
447
+
368
448
  ##
369
449
  # === Get Taxonomies.
370
450
  # Get a collection of taxonomies.
371
451
  #
372
452
  # ==== Parameters
373
453
  # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
454
+ # use_post:: (Boolean) -- Variable to determine if the request is by 'post' or 'get' functions.
374
455
  #
375
- # ==== Example
376
- # @mints_pub.get_taxonomies
377
- def get_taxonomies(options = nil)
378
- return @client.raw("get", "/config/taxonomies", options)
456
+ # ==== First Example
457
+ # @data = @mints_pub.get_taxonomies
458
+ #
459
+ # ==== Second Example
460
+ # options = {
461
+ # "fields": "id, title"
462
+ # }
463
+ # @data = @mints_pub.get_taxonomies(options)
464
+ #
465
+ # ==== Third Example
466
+ # options = {
467
+ # "fields": "id, title"
468
+ # }
469
+ # @data = @mints_pub.get_taxonomies(options, false)
470
+ def get_taxonomies(options = nil, use_post = true)
471
+ return get_query_results("/config/taxonomies", options, use_post)
379
472
  end
380
473
 
381
474
  ##
@@ -386,8 +479,14 @@ module Mints
386
479
  # slug:: (String) -- It's the string identifier generated by Mints.
387
480
  # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
388
481
  #
389
- # ==== Example
390
- # @mints_pub.get_taxonomy("taxonomy_slug")
482
+ # ==== First Example
483
+ # @data = @mints_pub.get_taxonomy("taxonomy_slug")
484
+ #
485
+ # ==== Second Example
486
+ # options = {
487
+ # "fields": "title"
488
+ # }
489
+ # @data = @mints_pub.get_taxonomy("taxonomy_slug", options)
391
490
  def get_taxonomy(slug, options = nil)
392
491
  return @client.raw("get", "/config/taxonomies/#{slug}", options)
393
492
  end
@@ -396,14 +495,45 @@ module Mints
396
495
  # === Get Attributes.
397
496
  # Get a collection of attributes.
398
497
  #
498
+ # ==== Example
499
+ # @data = @mints_pub.get_attributes
500
+ def get_attributes
501
+ return @client.raw("get", "/config/attributes")
502
+ end
503
+
504
+ # Methods deleted
505
+ ##
506
+ # === Get categories.
507
+ # Get a collection of categories.
508
+ #
399
509
  # ==== Parameters
400
510
  # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter.
401
511
  #
402
512
  # ==== Example
403
- # @mints_pub.get_attributes
404
- def get_attributes(options = nil)
405
- return @client.raw("get", "/config/attributes", options)
406
- end
513
+ # options = {
514
+ # "object_type": "stories"
515
+ # }
516
+ # @data = @mints_pub.get_categories(options)
517
+ #def get_categories(options = nil)
518
+ # return @client.raw("get", "/config/categories", options)
519
+ #end
520
+
521
+ ##
522
+ # === Get Category.
523
+ # Get a single category.
524
+ #
525
+ # ==== Parameters
526
+ # slug:: (String) -- It's the string identifier generated by Mints.
527
+ # options:: (Hash) -- List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter.
528
+ #
529
+ # ==== Example
530
+ # options = {
531
+ # "object_type": "locations"
532
+ # }
533
+ # @data = @mints_pub.get_category("asset_slug", options)
534
+ #def get_category(slug, options = nil)
535
+ # return @client.raw("get", "/config/categories/#{slug}", options)
536
+ #end
407
537
 
408
538
  private
409
539