mints 0.0.17 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -1
  3. data/lib/client.rb +96 -37
  4. data/lib/contact.rb +659 -94
  5. data/lib/generators/mints_files_generator.rb +6 -0
  6. data/lib/generators/mints_link.rb +61 -0
  7. data/lib/generators/short_link_controller.rb +41 -0
  8. data/lib/mints/controllers/admin_base_controller.rb +2 -2
  9. data/lib/mints/controllers/base_api_controller.rb +12 -11
  10. data/lib/mints/controllers/base_controller.rb +38 -9
  11. data/lib/mints_helper.rb +47 -0
  12. data/lib/pub.rb +258 -139
  13. data/lib/user/config/api_keys.rb +65 -0
  14. data/lib/user/config/appointments.rb +221 -0
  15. data/lib/user/config/attribute_groups.rb +77 -0
  16. data/lib/user/config/attributes.rb +86 -0
  17. data/lib/user/config/calendars.rb +89 -0
  18. data/lib/user/config/config.rb +65 -0
  19. data/lib/user/config/importers.rb +184 -0
  20. data/lib/user/config/public_folders.rb +108 -0
  21. data/lib/user/config/relationships.rb +138 -0
  22. data/lib/user/config/roles.rb +84 -0
  23. data/lib/user/config/seeds.rb +14 -0
  24. data/lib/user/config/system_settings.rb +53 -0
  25. data/lib/user/config/tags.rb +63 -0
  26. data/lib/user/config/taxonomies.rb +124 -0
  27. data/lib/user/config/teams.rb +70 -0
  28. data/lib/user/config/users.rb +76 -0
  29. data/lib/user/contacts/contacts.rb +21 -0
  30. data/lib/user/content/assets.rb +260 -0
  31. data/lib/user/content/content.rb +235 -0
  32. data/lib/user/content/content_instances.rb +147 -0
  33. data/lib/user/content/content_templates.rb +111 -0
  34. data/lib/user/content/conversations.rb +174 -0
  35. data/lib/user/content/dam.rb +88 -0
  36. data/lib/user/content/forms.rb +168 -0
  37. data/lib/user/content/message_templates.rb +162 -0
  38. data/lib/user/content/messages.rb +90 -0
  39. data/lib/user/content/pages.rb +81 -0
  40. data/lib/user/content/stories.rb +164 -0
  41. data/lib/user/content/story_templates.rb +95 -0
  42. data/lib/user/crm/companies.rb +111 -0
  43. data/lib/user/crm/contacts.rb +312 -0
  44. data/lib/user/crm/crm.rb +21 -0
  45. data/lib/user/crm/deals.rb +111 -0
  46. data/lib/user/crm/favorites.rb +17 -0
  47. data/lib/user/crm/segments.rb +132 -0
  48. data/lib/user/crm/users.rb +22 -0
  49. data/lib/user/crm/workflow_step_objects.rb +89 -0
  50. data/lib/user/crm/workflow_steps.rb +49 -0
  51. data/lib/user/crm/workflows.rb +70 -0
  52. data/lib/user/ecommerce/ecommerce.rb +29 -0
  53. data/lib/user/ecommerce/item_prices.rb +86 -0
  54. data/lib/user/ecommerce/locations.rb +166 -0
  55. data/lib/user/ecommerce/order_items_groups.rb +109 -0
  56. data/lib/user/ecommerce/order_statuses.rb +26 -0
  57. data/lib/user/ecommerce/orders.rb +258 -0
  58. data/lib/user/ecommerce/price_lists.rb +73 -0
  59. data/lib/user/ecommerce/product_templates.rb +104 -0
  60. data/lib/user/ecommerce/product_variations.rb +129 -0
  61. data/lib/user/ecommerce/products.rb +169 -0
  62. data/lib/user/ecommerce/skus.rb +88 -0
  63. data/lib/user/ecommerce/taxes.rb +82 -0
  64. data/lib/user/ecommerce/variant_options.rb +69 -0
  65. data/lib/user/ecommerce/variant_values.rb +72 -0
  66. data/lib/user/helpers/helpers.rb +113 -0
  67. data/lib/user/helpers/object_activities.rb +83 -0
  68. data/lib/user/helpers/object_folders.rb +82 -0
  69. data/lib/user/helpers/user_folders.rb +83 -0
  70. data/lib/user/marketing/marketing.rb +120 -0
  71. data/lib/user/profile/profile.rb +111 -0
  72. data/lib/user.rb +24 -368
  73. metadata +64 -2
@@ -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
@@ -22,7 +22,7 @@ module Mints
22
22
  # Get session token from response
23
23
  session_token = response['api_token']
24
24
  # Set a permanent cookie with the session token
25
- cookies.permanent[:mints_user_session_token] = session_token
25
+ cookies[:mints_user_session_token] = { value: session_token, secure: true, httponly: true, expires: 1.day }
26
26
  end
27
27
 
28
28
  ##
@@ -33,7 +33,7 @@ module Mints
33
33
  response = @mints_user.magic_link_login(hash)
34
34
  if response['data'] && response['data']['redirect_url']
35
35
  # Set a cookie with the session token
36
- cookies[:mints_user_session_token] = { value: response['data']['api_token'], expires: 1.day }
36
+ cookies[:mints_user_session_token] = { value: response['data']['api_token'], expires: 1.day, secure: true, httponly: true }
37
37
  redirect_to response['data']['redirect_url']
38
38
  else
39
39
  redirect_to '/'
@@ -10,26 +10,26 @@ module Mints
10
10
  response = @mints_contact.login(email, password)
11
11
  # Get session token from response
12
12
  session_token = response['session_token']
13
- id_token = response['contact']['id_token']
13
+ id_token = response['contact']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
14
14
  # Set a permanent cookie with the session token
15
- cookies.permanent[:mints_contact_session_token] = session_token
16
- cookies.permanent[:mints_contact_id] = id_token
15
+ cookies.permanent[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
16
+ cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
17
17
  @contact_token = id_token
18
18
  end
19
19
 
20
20
  ##
21
- # === Mints cotnact Login.
22
- # Starts a cotnact session in mints.cloud and set a session cookie
21
+ # === Mints contact Login.
22
+ # Starts a contact session in mints.cloud and set a session cookie
23
23
  def mints_contact_magic_link_login(hash)
24
24
  # Login in mints
25
25
  response = @mints_contact.magic_link_login(hash)
26
26
  if response['data']
27
27
  # Get session token from response
28
28
  session_token = response['data']['session_token']
29
- id_token = response['data']['contact']['id_token']
29
+ id_token = response['data']['contact']['contact_token'] ? response['data']['contact']['contact_token'] : response['data']['contact']['id_token']
30
30
  # Set a permanent cookie with the session token
31
- cookies.permanent[:mints_contact_session_token] = session_token
32
- cookies.permanent[:mints_contact_id] = id_token
31
+ cookies.permanent[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
32
+ cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
33
33
  @contact_token = id_token
34
34
  redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/'
35
35
  else
@@ -58,7 +58,7 @@ module Mints
58
58
  # Get session token from response
59
59
  session_token = response['api_token']
60
60
  # Set a permanent cookie with the session token
61
- cookies.permanent[:mints_user_session_token] = session_token
61
+ cookies[:mints_user_session_token] = { value: session_token, secure: true, httponly: true, expires: 1.day }
62
62
  end
63
63
 
64
64
  ##
@@ -69,7 +69,7 @@ module Mints
69
69
  response = @mints_user.magic_link_login(hash)
70
70
  if response['data']
71
71
  # Set a cookie with the session token
72
- cookies[:mints_user_session_token] = { value: response['data']['api_token'], expires: 1.day }
72
+ cookies[:mints_user_session_token] = { value: response['data']['api_token'], secure: true, httponly: true, expires: 1.day }
73
73
  redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/'
74
74
  else
75
75
  redirect_to '/'
@@ -116,7 +116,8 @@ module Mints
116
116
  def set_mints_pub_client
117
117
 
118
118
  # Initialize mints pub client, credentials taken from mints_config.yml.erb file
119
- @mints_pub = Mints::Pub.new(@host, @api_key, nil, @debug)
119
+ @visit_id = cookies[:mints_visit_id] ? cookies[:mints_visit_id] : nil
120
+ @mints_pub = Mints::Pub.new(@host, @api_key, nil, @visit_id, @debug)
120
121
  # Set contact token from cookie
121
122
  @mints_pub.client.session_token = @contact_token
122
123
  end
@@ -24,10 +24,10 @@ module Mints
24
24
  response = @mints_contact.login(email, password)
25
25
  # Get session token from response
26
26
  session_token = response['session_token']
27
- id_token = response['contact']['id_token']
27
+ id_token = response['contact']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
28
28
  # Set a permanent cookie with the session token
29
- cookies.permanent[:mints_contact_session_token] = session_token
30
- cookies.permanent[:mints_contact_id] = id_token
29
+ cookies.permanent[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
30
+ cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
31
31
  @contact_token = id_token
32
32
  end
33
33
 
@@ -39,10 +39,10 @@ module Mints
39
39
  response = @mints_contact.login(email, password)
40
40
  # Get session token from response
41
41
  session_token = response['session_token']
42
- id_token = response['contact']['id_token']
42
+ id_token = response['contact']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
43
43
  # Set a permanent cookie with the session token
44
- cookies.permanent[:mints_contact_session_token] = session_token
45
- cookies.permanent[:mints_contact_id] = id_token
44
+ cookies.permanent[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
45
+ cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
46
46
  @contact_token = id_token
47
47
  end
48
48
 
@@ -64,10 +64,29 @@ module Mints
64
64
  # === Register visit.
65
65
  # Call register visit method from the public client and set/renew the cookie mints_contact_id
66
66
  def register_visit
67
+ if @debug
68
+ puts "REQUEST IN REGISTER VISIT: #{request}"
69
+ puts "BODY REQUEST: #{request.body}"
70
+ puts "AUTH REQUEST: #{request.authorization}"
71
+ puts "LENGTH REQUEST: #{request.content_length}"
72
+ puts "FORM DATA REQUEST: #{request.form_data?}"
73
+ puts "FULLPATH REQUEST: #{request.fullpath}"
74
+ puts "HEADERS REQUEST: #{request.headers}"
75
+ puts "IP REQUEST: #{request.ip}"
76
+ puts "REQUEST IP ADDRESS: #{request['ip_address']}"
77
+ puts "REQUEST REMOTE IP: #{request['remote_ip']}"
78
+ end
67
79
  response = @mints_pub.register_visit(request)
68
- @contact_token = response['user_token']
80
+ if @debug
81
+ puts "RESPONSE IN REGISTER VISIT: #{response}"
82
+ end
83
+ @contact_token = response['contact_token'] ? response['contact_token'] : response['user_token']
69
84
  @visit_id = response['visit_id']
70
- cookies.permanent[:mints_contact_id] = @contact_token
85
+ if @debug
86
+ puts "VISIT ID: #{@visit_id}"
87
+ end
88
+ cookies.permanent[:mints_contact_id] = { value: @contact_token, secure: true, httponly: true }
89
+ cookies.permanent[:mints_visit_id] = { value: @visit_id, secure: true, httponly: true }
71
90
  end
72
91
 
73
92
  ##
@@ -84,7 +103,8 @@ module Mints
84
103
  raise 'MintsBadCredentialsError'
85
104
  end
86
105
  # Initialize mints pub client, credentials taken from mints_config.yml.erb file
87
- @mints_pub = Mints::Pub.new(@host, @api_key, @contact_token, @debug)
106
+ @visit_id = cookies[:mints_visit_id] ? cookies[:mints_visit_id] : nil
107
+ @mints_pub = Mints::Pub.new(@host, @api_key, @contact_token, @visit_id, @debug)
88
108
  # Set contact token from cookie
89
109
  @mints_pub.client.session_token = @contact_token
90
110
  end
@@ -100,6 +120,15 @@ module Mints
100
120
  # === Set mints contact client.
101
121
  # Initialize the public client and set the contact token
102
122
  def set_mints_contact_client
123
+ if File.exists?("#{Rails.root}/mints_config.yml.erb")
124
+ template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
125
+ config = YAML.load template.result(binding)
126
+ @host = config["mints"]["host"]
127
+ @api_key = config["mints"]["api_key"]
128
+ @debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
129
+ else
130
+ raise 'MintsBadCredentialsError'
131
+ end
103
132
  # Initialize mints clontact client
104
133
  session_token = cookies[:mints_contact_session_token] ? cookies[:mints_contact_session_token] : nil
105
134
  contact_token_id = cookies[:mints_contact_id] ? cookies[:mints_contact_id] : nil
@@ -0,0 +1,47 @@
1
+
2
+ module MintsHelper
3
+ # === Get query results.
4
+ # Method used to give the options to make a 'post' or 'get' request.
5
+ #
6
+ # ==== Parameters
7
+ # url:: (String) -- Url to make the request.
8
+ # options:: (Hash) -- List of {Resource collection Options}[#class-Mints::User-label-Resource+collections+options+] shown above can be used as parameter.
9
+ # use_post:: (Boolean) -- Variable to determine if the request is by 'post' or 'get' functions.
10
+ #
11
+ def get_query_results(url, options = nil, use_post = true)
12
+ if use_post
13
+ return @client.raw("post", "#{url}/query", options)
14
+ else
15
+ return @client.raw("get", url, options)
16
+ end
17
+ end
18
+
19
+ # === Data transform.
20
+ # Transform a 'data' variable to a standardized 'data' variable.
21
+ #
22
+ # ==== Parameters
23
+ # data:: (Hash) -- Data to be submited.
24
+ #
25
+ def data_transform(data)
26
+ data = correct_json(data)
27
+ unless data[:data]
28
+ data = {data: data}
29
+ end
30
+ return data.to_json
31
+ end
32
+
33
+ # === Correct json.
34
+ # Receives a json data and convert it to a symbolized object.
35
+ #
36
+ # ==== Parameters
37
+ # data:: (Hash) -- Data to be submited.
38
+ #
39
+ def correct_json(data)
40
+ if data.is_a? String
41
+ data = JSON.parse(data)
42
+ end
43
+ data = data.symbolize_keys
44
+ return data
45
+ end
46
+
47
+ end