mints 0.0.1 → 0.0.6

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: 74526a6438b7de0c89f91b1d7c14b8e34a77c139f911783cfe37437eb6c32b20
4
- data.tar.gz: cef5273897e15545fe8a1f831fbbe1069cb1c4c2a14486221bdea94f3f70e6bb
3
+ metadata.gz: 2df25d921d402fcd499d478e74ff9aabcd88c6bd80570eb02dff24db03bfc10d
4
+ data.tar.gz: 85d5e0a2bec7138f2c9f878e3a4f7399581113071a83387615957f58a7c79486
5
5
  SHA512:
6
- metadata.gz: f7da303e2dd55c10fb990d17a49628a99d95c8a3e06985cecc4e80d23784abe4c131afd6a16ff4780d639813ccbb0c0e5c6f75732b294f5a4a8cc9f607378d44
7
- data.tar.gz: f521865904561b3c73e82ae099ca39dfdd9af23e424faf9cb4a1e219676e8d5eeb43426a6a1623a7f44e4bac88dbcf9603a786953f9d56c7a0a0527c6f41fc29
6
+ metadata.gz: 2835d608df2fd2156b7b58c7930ffc359b4dddb9994a32a3e77bd84eeab1425ee7224afa936f7758d684aba2a6920f2b4aa93e1237da50c415bf950971d74273
7
+ data.tar.gz: 1b3e7fe4d5a7611b21a9f86970a774ab1935395f3efa61b442bb0d56cf34c7f7408ce405d19b9846693102cc49faca4d50232b954bcd3d9bc01936b4ec671809
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,25 +9,31 @@ 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
- def raw(action, url, data = nil, base_url = nil)
20
- base_url = @base_url if !base_url
21
- full_url = "#{@host}#{base_url}#{url}"
22
- p full_url
20
+ def raw(action, url, options = nil, data = nil, base_url = nil)
21
+ base_url = @base_url if !base_url
22
+ uri = ""
23
+ if (options && options.class == Hash)
24
+ uri = Addressable::URI.new
25
+ uri.query_values = options
26
+ end
27
+
28
+ full_url = "#{@host}#{base_url}#{url}#{uri}"
23
29
  if action === 'get'
24
- response = self.send("#{@scope}_#{action}", full_url)
30
+ response = self.send("#{@scope}_#{action}", "#{full_url}")
25
31
  elsif action === 'create' or action === 'post'
26
32
  action = 'post'
27
- response = self.send("#{@scope}_#{action}", full_url, data)
33
+ response = self.send("#{@scope}_#{action}", "#{full_url}", data)
28
34
  elsif action === 'put' or action === 'patch' or action ==='update'
29
35
  action = 'put'
30
- response = self.send("#{@scope}_#{action}", full_url, data)
36
+ response = self.send("#{@scope}_#{action}", "#{full_url}", data)
31
37
  end
32
38
  if (response.response.code == "404")
33
39
  raise 'NotFoundError'
@@ -39,7 +45,8 @@ module Mints
39
45
  return parsed_response
40
46
  end
41
47
 
42
- def method_missing(name, *args, &block)
48
+ def method_missing(name, *args, &block)
49
+ puts name
43
50
  name.to_s.include?("__") ? separator = "__" : separator = "_"
44
51
  # split the name to identify their elements
45
52
  name_spplited = name.to_s.split(separator)
@@ -63,37 +70,54 @@ module Mints
63
70
  route_array.push n
64
71
  end
65
72
  route = route_array.join("/")
66
- if args.first.class == Hash
67
- uri = Addressable::URI.new
68
- uri.query_values = args.first if action === 'get'
69
- elsif args.first.class == String or Integer
70
- slug = args.first
71
- uri = Addressable::URI.new
72
- uri.query_values = args[1] if action === 'get'
73
- end
74
- if (slug)
75
- url = "#{@host}#{@base_url}/#{route}/#{object}/#{slug}#{uri}"
76
- else
77
- url = "#{@host}#{@base_url}/#{route}/#{object}#{uri}"
78
- end
79
- if action === 'get'
73
+
74
+
75
+ slug = nil
76
+ uri = Addressable::URI.new
77
+ if action == "get"
78
+ if args.first.class == Hash
79
+ uri.query_values = args.first
80
+ elsif args.first.class == String or Integer
81
+ slug = args.first
82
+ uri.query_values = args[1]
83
+ end
84
+ url = self.get_url(route, object, uri, slug)
80
85
  response = self.send("#{@scope}_#{action}", url)
81
- elsif action === 'post' or action === 'create'
86
+ elsif action == "post" or action == "create"
87
+ if args[1].class == Hash
88
+ uri.query_values = args[1]
89
+ end
90
+ url = self.get_url(route, object, uri, slug)
82
91
  action = 'post'
83
92
  data = args[0]
84
93
  response = self.send("#{@scope}_#{action}", url, {data: data})
85
- elsif action === 'put' or action === 'update'
94
+ elsif action == "put" or action == "update"
95
+ if args.first.class == String or Integer
96
+ slug = args.first
97
+ uri.query_values = args[2]
98
+ end
99
+ url = self.get_url(route, object, uri, slug)
86
100
  action = 'put'
87
101
  id = args[0]
88
102
  data = args[1]
89
- response = self.send("#{@scope}_#{action}", "#{url}", data)
103
+ response = self.send("#{@scope}_#{action}", "#{url}", {data: data})
90
104
  end
91
- p url
92
- if (response.response.code == "404")
93
- raise 'NotFoundError'
105
+
106
+ if response.response.code == "404"
107
+ raise 'NotFoundError'
108
+ elsif response.response.code == "500"
109
+ raise 'InternalServerError'
94
110
  end
95
111
  return JSON.parse(response.body)
96
112
  end
113
+
114
+ def get_url(route, object, uri, slug = nil)
115
+ if (slug)
116
+ return "#{@host}#{@base_url}/#{route}/#{object}/#{slug}#{uri}"
117
+ else
118
+ return "#{@host}#{@base_url}/#{route}/#{object}#{uri}"
119
+ end
120
+ end
97
121
 
98
122
  def replacements
99
123
  return [
@@ -120,16 +144,38 @@ module Mints
120
144
  ##### HTTTP CLIENTS ######
121
145
  # Simple HTTP GET
122
146
  def http_get(url, headers = nil)
147
+ if @debug
148
+ puts "Url:"
149
+ puts url
150
+ puts "Headers:"
151
+ puts headers
152
+ end
123
153
  return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
124
154
  end
125
155
 
126
156
  # Simple HTTP POST
127
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
128
166
  return headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data)
129
167
  end
130
168
 
131
169
  # Simple HTTP PUT
132
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
133
179
  return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
134
180
  end
135
181
 
@@ -184,7 +230,7 @@ module Mints
184
230
  headers = {
185
231
  "ApiKey" => @api_key,
186
232
  "Accept" => "application/json",
187
- "Contet-Type" => "application/json"
233
+ "Content-Type" => "application/json"
188
234
  }
189
235
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
190
236
  return self.http_put(url, headers, data)
@@ -192,7 +238,12 @@ module Mints
192
238
  # End User Context
193
239
 
194
240
  def public_get(url, headers = nil)
195
- 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
+ }
196
247
  if headers
197
248
  headers.each do |k,v|
198
249
  h[k] = v
@@ -202,7 +253,12 @@ module Mints
202
253
  end
203
254
 
204
255
  def public_post(url, headers = nil, data)
205
- 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
+ }
206
262
  if headers
207
263
  headers.each do |k,v|
208
264
  h[k] = v
@@ -212,7 +268,12 @@ module Mints
212
268
  end
213
269
 
214
270
  def public_put(url, headers = nil, data)
215
- 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
+ }
216
277
  if headers
217
278
  headers.each do |k,v|
218
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,
@@ -13,55 +21,91 @@ module Mints
13
21
  email: email,
14
22
  password: password
15
23
  }
16
- return @client.raw("post", "/contacts/register", {data: data})
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,
22
34
  password: password
23
35
  }
24
- response = @client.raw("post", "/contacts/login", {data: data})
36
+ response = @client.raw("post", "/contacts/login", nil, {data: data})
25
37
  if response.key? "session_token"
26
38
  @client.session_token = response["session_token"]
27
39
  end
28
40
  return response
29
41
  end
30
42
 
43
+ ##
44
+ # === Logout.
45
+ # Ends a contact session
46
+ #
31
47
  def logout
32
- response = @client.raw("post", "/contacts/logout", nil) if session_token?
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
 
39
- def change_password
40
- return @client.raw("post", "/contacts/change-password", data)
55
+ ##
56
+ # === Change Password.
57
+ # Change password
58
+ #
59
+ def change_password(data)
60
+ return @client.raw("post", "/contacts/change-password", nil, data)
41
61
  end
42
62
 
43
- def recover_password
44
- return @client.raw("post", "/contacts/recover-password", data)
63
+ ##
64
+ # === Recover Password.
65
+ # Recover password
66
+ #
67
+ def recover_password(data)
68
+ return @client.raw("post", "/contacts/recover-password", nil, data)
45
69
  end
46
70
 
47
- def reset_password
48
- return @client.raw("post", "/contacts/reset-password", data)
71
+ ##
72
+ # === Reset Password.
73
+ # Reset password
74
+ #
75
+ def reset_password(data)
76
+ return @client.raw("post", "/contacts/reset-password", nil, data)
49
77
  end
50
78
 
51
- def auth_login
52
- return @client.raw("post", "/contacts/oauth-login", data)
79
+ ##
80
+ # === OAuth Login.
81
+ # Login a contact using oauth
82
+ #
83
+ def oauth_login(data)
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
64
- return @client.raw("put", "/contacts/update", data)
103
+ ##
104
+ # === Update.
105
+ # Update logged contact attributes
106
+ #
107
+ def update(data)
108
+ return @client.raw("put", "/contacts/update", nil, data)
65
109
  end
66
110
 
67
111
  private
@@ -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,2 @@
1
+ class Api::V1::MintsContactController < Mints::ContactAPIController
2
+ 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