mints 0.0.2 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf8cfd66c96475748e1eaa88e97ee51b2c3fc7b2a682fc558f4bb7eda98d31ea
4
- data.tar.gz: 5175fb3ceaa8f4c9c607b76d90ff2cec26eec6a11e487f91e7d99645dd36687f
3
+ metadata.gz: 0d223884b10724746bee9c19bca2cfb4bed7678c48401fa5d5ff06424c81cf76
4
+ data.tar.gz: 80476fdc59a576a8d13a99176a0b2c91980fce8bb47b2a86d71201d751fe4004
5
5
  SHA512:
6
- metadata.gz: e4e6b98fa27ba280d9f912b91c4c8f0da3ebd8f1750c2ee8d06429d1983c56dbad0cb34c8f9d3b6595155ebced1af354c8c36a39104e412cb04d2ea9628d5684
7
- data.tar.gz: 9f1952c50204dde682a49fc1c9039f3e3f2da84078f711f227f236b8b9969c795cbebf9bba9a0350ba205dee403762789413e9381c3912c107a602494e781d16
6
+ metadata.gz: 5a948225647c432423b0ed981f3e0ddd0605e0e4d5237fa2fa54836a21561cc63cb4dc77f4f5e368f4943210095265c38f0c94cdc4395c38e236611f5289af54
7
+ data.tar.gz: 9d2959b4c166326e63de097993a8d06184fc53e3fbee80c3d85357f644ab25873d1e23932880b458765606def346aaaee5dea0ce382a3e014dd852371128c290
data/Gemfile CHANGED
@@ -2,4 +2,5 @@ source :rubygems
2
2
 
3
3
  gem 'json'
4
4
  gem 'httparty'
5
- gem 'addressable'
5
+ gem 'addressable'
6
+ gem 'rails-reverse-proxy', '~> 0.9.1'
data/Readme.md CHANGED
@@ -6,8 +6,7 @@ This is a library to connect apps built on ruby to Mints.Cloud
6
6
 
7
7
  Add gem to the Gemfile
8
8
  ```bash
9
- gem 'mints', git: 'https://github.com/rubengomez/mints-ruby-sdk'
10
- bundle install
9
+ gem 'mints'
11
10
  ```
12
11
 
13
12
  ## Usage
@@ -30,3 +29,56 @@ con = Mints::User.new(mints_url, api_key)
30
29
  con.login(email, password)
31
30
  con.get_contacts
32
31
  ```
32
+ ## Generate mints files
33
+ This command will generate the mints_config.yml file, API controlles and routes to have available the mints endpoints
34
+ ```bash
35
+ rails generate mints_files
36
+ ```
37
+ ## Contact tracking usage
38
+ Your app controller needs to be inherited from Mints::BaseController
39
+ ```ruby
40
+ # application_controller.rb
41
+
42
+ class ApplicationController < Mints::BaseController
43
+ end
44
+ ```
45
+ This heritance will make the following class variables available:
46
+
47
+ | Variable | Description |
48
+ | --- | :---: |
49
+ | @host | Host defined in mints_config.yml file |
50
+ | @api_key | API key defined in mints_config.yml file |
51
+ | @mints_pub | An already instanced public client |
52
+ | @contact_token | A token used by mints to identify the contact |
53
+ | @visit_id | An identifier of the visit registered |
54
+ | @mints_contact | An already instanced contact client (not usable until call the contact login method) |
55
+
56
+ And the following controller methods:
57
+ | Method | Parameters | Return value | Description |
58
+ | --- | :---: | :---: | :---: |
59
+ | mints_contact_signed_in? | none | boolean | Indicates if the contact has an active session |
60
+ | mints_contact_login | email, password| void | Starts a contact session |
61
+ | mints_contact_logout | none | void | Ends a contact session |
62
+
63
+ ## Admin controller usage
64
+ If want to have a private section where only a mints user can acces and use the private user api is needed to inherit from the AdminBaseController.
65
+
66
+ ```ruby
67
+ # admin_controller.rb
68
+
69
+ class AdminController < Mints::AdminBaseController
70
+ end
71
+ ```
72
+
73
+ This heritance will make the following class variables available:
74
+ | Variable | Description |
75
+ | --- | :---: |
76
+ | @host | Host defined in mints_config.yml file |
77
+ | @api_key | API key defined in mints_config.yml file |
78
+ | @mints_user | An already instanced user client (not usable until call the user login method) |
79
+
80
+ And the following controller methods:
81
+ | Method | Parameters | Return value | Description |
82
+ | --- | :---: | :---: | :---: |
83
+ | mints_user_login | email, password| void | Starts a user session |
84
+ | mints_user_logout | none | void | Ends a user session |
@@ -9,21 +9,20 @@ module Mints
9
9
  attr_reader :base_url
10
10
  attr_accessor :session_token
11
11
 
12
- def initialize(host, api_key, scope = nil, session_token = nil)
12
+ def initialize(host, api_key, scope = nil, session_token = nil, debug = false)
13
13
  @host = host
14
14
  @api_key = api_key
15
15
  @session_token = session_token
16
+ @debug = debug
16
17
  self.set_scope(scope)
17
18
  end
18
19
 
19
20
  def raw(action, url, options = nil, data = nil, base_url = nil)
20
- base_url = @base_url if !base_url
21
-
21
+ base_url = @base_url if !base_url
22
+ uri = ""
22
23
  if (options && options.class == Hash)
23
24
  uri = Addressable::URI.new
24
25
  uri.query_values = options
25
- else
26
- uri = ""
27
26
  end
28
27
 
29
28
  full_url = "#{@host}#{base_url}#{url}#{uri}"
@@ -46,7 +45,8 @@ module Mints
46
45
  return parsed_response
47
46
  end
48
47
 
49
- def method_missing(name, *args, &block)
48
+ def method_missing(name, *args, &block)
49
+ puts name
50
50
  name.to_s.include?("__") ? separator = "__" : separator = "_"
51
51
  # split the name to identify their elements
52
52
  name_spplited = name.to_s.split(separator)
@@ -144,16 +144,38 @@ module Mints
144
144
  ##### HTTTP CLIENTS ######
145
145
  # Simple HTTP GET
146
146
  def http_get(url, headers = nil)
147
+ if @debug
148
+ puts "Url:"
149
+ puts url
150
+ puts "Headers:"
151
+ puts headers
152
+ end
147
153
  return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
148
154
  end
149
155
 
150
156
  # Simple HTTP POST
151
157
  def http_post(url, headers = nil, data = nil)
158
+ if @debug
159
+ puts "Url:"
160
+ puts url
161
+ puts "Headers:"
162
+ puts headers
163
+ puts "Data:"
164
+ puts data
165
+ end
152
166
  return headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data)
153
167
  end
154
168
 
155
169
  # Simple HTTP PUT
156
170
  def http_put(url, headers = nil, data = nil)
171
+ if @debug
172
+ puts "Url:"
173
+ puts url
174
+ puts "Headers:"
175
+ puts headers
176
+ puts "Data:"
177
+ puts data
178
+ end
157
179
  return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
158
180
  end
159
181
 
@@ -208,7 +230,7 @@ module Mints
208
230
  headers = {
209
231
  "ApiKey" => @api_key,
210
232
  "Accept" => "application/json",
211
- "Contet-Type" => "application/json"
233
+ "Content-Type" => "application/json"
212
234
  }
213
235
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
214
236
  return self.http_put(url, headers, data)
@@ -216,7 +238,12 @@ module Mints
216
238
  # End User Context
217
239
 
218
240
  def public_get(url, headers = nil)
219
- h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
241
+ h = {
242
+ "Accept" => "application/json",
243
+ "Content-Type" => "application/json",
244
+ "ApiKey" => @api_key,
245
+ "ContactToken" => @session_token
246
+ }
220
247
  if headers
221
248
  headers.each do |k,v|
222
249
  h[k] = v
@@ -226,7 +253,12 @@ module Mints
226
253
  end
227
254
 
228
255
  def public_post(url, headers = nil, data)
229
- h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
256
+ h = {
257
+ "Accept" => "application/json",
258
+ "Content-Type" => "application/json",
259
+ "ApiKey" => @api_key,
260
+ "ContactToken" => @session_token
261
+ }
230
262
  if headers
231
263
  headers.each do |k,v|
232
264
  h[k] = v
@@ -236,7 +268,12 @@ module Mints
236
268
  end
237
269
 
238
270
  def public_put(url, headers = nil, data)
239
- h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
271
+ h = {
272
+ "Accept" => "application/json",
273
+ "Content-Type" => "application/json",
274
+ "ApiKey" => @api_key,
275
+ "ContactToken" => @session_token
276
+ }
240
277
  if headers
241
278
  headers.each do |k,v|
242
279
  h[k] = v
@@ -2,10 +2,18 @@ require_relative "./client.rb"
2
2
  module Mints
3
3
  class Contact
4
4
  attr_reader :client
5
- def initialize(host, api_key, session_token = nil)
6
- @client = Mints::Client.new(host, api_key, "contact", session_token)
5
+ ##
6
+ # === Initialize.
7
+ # Class constructor
8
+ #
9
+ def initialize(host, api_key, session_token = nil, debug = false)
10
+ @client = Mints::Client.new(host, api_key, "contact", session_token, debug)
7
11
  end
8
12
 
13
+ ##
14
+ # === Register.
15
+ # Register a new contact
16
+ #
9
17
  def register(given_name, last_name, email, password)
10
18
  data = {
11
19
  given_name: given_name,
@@ -16,6 +24,10 @@ module Mints
16
24
  return @client.raw("post", "/contacts/register", nil, {data: data})
17
25
  end
18
26
 
27
+ ##
28
+ # === Login.
29
+ # Starts a contact session
30
+ #
19
31
  def login(email, password)
20
32
  data = {
21
33
  email: email,
@@ -28,39 +40,71 @@ module Mints
28
40
  return response
29
41
  end
30
42
 
43
+ ##
44
+ # === Logout.
45
+ # Ends a contact session
46
+ #
31
47
  def logout
32
48
  response = @client.raw("post", "/contacts/logout") if session_token?
33
49
  if response["success"]
34
50
  @client.session_token = nil
35
- end
51
+ end
36
52
  return response
37
53
  end
38
54
 
55
+ ##
56
+ # === Change Password.
57
+ # Change password
58
+ #
39
59
  def change_password(data)
40
60
  return @client.raw("post", "/contacts/change-password", nil, data)
41
61
  end
42
62
 
63
+ ##
64
+ # === Recover Password.
65
+ # Recover password
66
+ #
43
67
  def recover_password(data)
44
68
  return @client.raw("post", "/contacts/recover-password", nil, data)
45
69
  end
46
70
 
71
+ ##
72
+ # === Reset Password.
73
+ # Reset password
74
+ #
47
75
  def reset_password(data)
48
76
  return @client.raw("post", "/contacts/reset-password", nil, data)
49
77
  end
50
78
 
51
- def auth_login(data)
79
+ ##
80
+ # === OAuth Login.
81
+ # Login a contact using oauth
82
+ #
83
+ def oauth_login(data)
52
84
  return @client.raw("post", "/contacts/oauth-login", nil, data)
53
85
  end
54
86
 
87
+ ##
88
+ # === Me.
89
+ # Get contact logged info
90
+ #
55
91
  def me
56
92
  return @client.raw("get", "/contacts/me")
57
93
  end
58
94
 
95
+ ##
96
+ # === Status.
97
+ # Get contact logged status
98
+ #
59
99
  def status
60
100
  return @client.raw("get", "/contacts/status")
61
101
  end
62
102
 
63
- def update
103
+ ##
104
+ # === Update.
105
+ # Update logged contact attributes
106
+ #
107
+ def update(data)
64
108
  return @client.raw("put", "/contacts/update", nil, data)
65
109
  end
66
110
 
@@ -0,0 +1,6 @@
1
+ # Mints connection configuration
2
+ mints:
3
+ host: http://your_host_goes_here.com
4
+ api_key: your_mints_api_key_goes_here
5
+ sdk:
6
+ debug: false
@@ -0,0 +1,3 @@
1
+ require 'reverse_proxy/client'
2
+ class Api::V1::MintsContactController < Mints::ContactAPIController
3
+ end
@@ -0,0 +1,20 @@
1
+ class MintsFilesGenerator < Rails::Generators::Base
2
+ source_root(File.expand_path(File.dirname(__FILE__)))
3
+ include Rails::Generators::Actions
4
+ def create_mints_files
5
+ copy_file 'mints_config.yml', 'mints_config.yml'
6
+ copy_file 'mints_user_controller.rb', './app/controllers/api/mints_user_controller.rb'
7
+ copy_file 'mints_contact_controller.rb', './app/controllers/api/v1/mints_contact_controller.rb'
8
+ copy_file 'mints_public_controller.rb', './app/controllers/api/v1/mints_public_controller.rb'
9
+ route <<-eos
10
+ # Mints auto-generated routes (proxy to send request to mints.cloud)
11
+ namespace :api, defaults: { format: :json } do
12
+ match '/user/v1/*path' => 'mints_user#index', via: [:get, :post, :put, :patch, :delete]
13
+ namespace :v1 do
14
+ match '/contacts/*path' => 'mints_contact#index', via: [:get, :post, :put, :patch, :delete]
15
+ match '/*path' => 'mints_public#index', via: [:get, :post, :put, :patch, :delete]
16
+ end
17
+ end
18
+ eos
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ require 'reverse_proxy/client'
2
+ class Api::V1::MintsPublicController < Mints::PublicAPIController
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'reverse_proxy/client'
2
+ class Api::MintsUserController < Mints::UserAPIController
3
+ end
@@ -1,5 +1,10 @@
1
1
  require_relative "./pub.rb"
2
2
  require_relative "./contact.rb"
3
3
  require_relative "./user.rb"
4
+ require_relative "./mints/controllers/base_controller.rb"
5
+ require_relative "./mints/controllers/admin_base_controller.rb"
6
+ require_relative "./mints/controllers/public_api_controller.rb"
7
+ require_relative "./mints/controllers/contact_api_controller.rb"
8
+ require_relative "./mints/controllers/user_api_controller.rb"
4
9
  module Mints
5
10
  end
@@ -0,0 +1,56 @@
1
+ module Mints
2
+ class AdminBaseController < ActionController::Base
3
+ before_action :set_mints_user_client
4
+
5
+ # def mints_user_signed_in?
6
+ # # Check status in mints
7
+ # response = @mints_user.status
8
+ # status = response['success'] ? response['success'] : false
9
+ # unless status
10
+ # # if mints response is negative delete the session cookie
11
+ # cookies.delete(:mints_user_session_token)
12
+ # end
13
+ # return status
14
+ # end
15
+
16
+ ##
17
+ # === Mints user Login.
18
+ # Starts a user session in mints.cloud and set a session cookie
19
+ def mints_user_login(email, password)
20
+ # Login in mints
21
+ response = @mints_user.login(email, password)
22
+ # Get session token from response
23
+ session_token = response['api_token']
24
+ # Set a permanent cookie with the session token
25
+ cookies.permanent[:mints_user_session_token] = session_token
26
+ end
27
+
28
+ ##
29
+ # === Mints user Logout.
30
+ # Destroy session from mints.cloud and delete local session cookie
31
+ def mints_user_logout
32
+ # Logout from mints
33
+ # @mints_user.logout
34
+ # Delete local cookie
35
+ cookies.delete(:mints_user_session_token)
36
+ end
37
+
38
+ private
39
+
40
+ ##
41
+ # === Set Mints user client.
42
+ # Initialize the public client and set the user token
43
+ def set_mints_user_client
44
+ if File.exists?("#{Rails.root}/mints_config.yml")
45
+ config = YAML.load_file("#{Rails.root}/mints_config.yml")
46
+ @host = config["mints"]["host"]
47
+ @api_key = config["mints"]["api_key"]
48
+ @debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
49
+ else
50
+ raise 'MintsBadCredentialsError'
51
+ end
52
+ # Initialize mints user client
53
+ @mints_user = Mints::User.new(@host, @api_key, nil, @debug)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,86 @@
1
+ module Mints
2
+ class BaseController < ActionController::Base
3
+ before_action :set_contact_token
4
+ before_action :set_mints_pub_client
5
+ before_action :register_visit
6
+ before_action :set_mints_contact_client
7
+
8
+ def mints_contact_signed_in?
9
+ # Check status in mints
10
+ response = @mints_contact.status
11
+ status = response['success'] ? response['success'] : false
12
+ unless status
13
+ # if mints response is negative delete the session cookie
14
+ cookies.delete(:mints_contact_session_token)
15
+ end
16
+ return status
17
+ end
18
+
19
+ ##
20
+ # === Mints Contact Login.
21
+ # Starts a contact session in mints.cloud and set a session cookie
22
+ def mints_contact_login(email, password)
23
+ # Login in mints
24
+ response = @mints_contact.login(email, password)
25
+ # Get session token from response
26
+ session_token = response['session_token']
27
+ # Set a permanent cookie with the session token
28
+ cookies.permanent[:mints_contact_session_token] = session_token
29
+ end
30
+
31
+ ##
32
+ # === Mints Contact Logout.
33
+ # Destroy session from mints.cloud and delete local session cookie
34
+ def mints_contact_logout
35
+ # Logout from mints
36
+ @mints_contact.logout
37
+ # Delete local cookie
38
+ cookies.delete(:mints_contact_session_token)
39
+ end
40
+
41
+ private
42
+
43
+ ##
44
+ # === Register visit.
45
+ # Call register visit method from the public client and set/renew the cookie mints_contact_id
46
+ def register_visit
47
+ response = @mints_pub.register_visit(request)
48
+ @contact_token = response['user_token']
49
+ @visit_id = response['visit_id']
50
+ cookies.permanent[:mints_contact_id] = @contact_token
51
+ end
52
+
53
+ ##
54
+ # === Set mints pub.
55
+ # Initialize the public client and set the contact token
56
+ def set_mints_pub_client
57
+ if File.exists?("#{Rails.root}/mints_config.yml")
58
+ config = YAML.load_file("#{Rails.root}/mints_config.yml")
59
+ @host = config["mints"]["host"]
60
+ @api_key = config["mints"]["api_key"]
61
+ @debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
62
+ else
63
+ raise 'MintsBadCredentialsError'
64
+ end
65
+ # Initialize mints pub client, credentials taken from mints_config.yml file
66
+ @mints_pub = Mints::Pub.new(@host, @api_key, nil, @debug)
67
+ # Set contact token from cookie
68
+ @mints_pub.client.session_token = @contact_token
69
+ end
70
+
71
+ ##
72
+ # === Set contact token.
73
+ # Set contact token variable from the mints_contact_id cookie value
74
+ def set_contact_token
75
+ @contact_token = cookies[:mints_contact_id]
76
+ end
77
+
78
+ ##
79
+ # === Set mints contact client.
80
+ # Initialize the public client and set the contact token
81
+ def set_mints_contact_client
82
+ # Initialize mints clontact client
83
+ @mints_contact = Mints::Contact.new(@host, @api_key, nil, @debug)
84
+ end
85
+ end
86
+ end