mints 0.0.7 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/{Readme.md → README.md} +5 -5
- data/lib/client.rb +57 -20
- data/lib/contact.rb +1 -14
- data/lib/generators/mints_config.yml.erb +23 -0
- data/lib/generators/mints_contact_controller.rb +0 -0
- data/lib/generators/mints_files_generator.rb +1 -1
- data/lib/generators/mints_public_controller.rb +0 -0
- data/lib/generators/mints_user_controller.rb +0 -0
- data/lib/mints.rb +1 -0
- data/lib/mints/controllers/admin_base_controller.rb +3 -2
- data/lib/mints/controllers/base_api_controller.rb +53 -0
- data/lib/mints/controllers/base_controller.rb +9 -3
- data/lib/mints/controllers/contact_api_controller.rb +57 -7
- data/lib/mints/controllers/public_api_controller.rb +59 -7
- data/lib/mints/controllers/user_api_controller.rb +56 -6
- data/lib/pub.rb +5 -5
- data/lib/user.rb +0 -0
- metadata +26 -5
- data/lib/generators/mints_config.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97aa9d0890796f3b8d99a99377253cdf6e94dedeb87a7048318073903fbfbe1d
|
4
|
+
data.tar.gz: 9e4e17583e943160852667e6fa853504818f6ba330529834416c4c2dc232bd85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 762ab3452de8a7e288e51c498daf58141fe2b7068904f68b455871c87493b32943a3e0225b6b2a90e9bd3d87f1331995d82e207ac214b6c545440a2362122e4a
|
7
|
+
data.tar.gz: 14b2ccb45e7bf43d7c23e43708e2916970e11f49d117e8700e06ff0a00f5f0e7bbb2d7a42ab206879e670e524fb5c87677b4d71de518080026d84c3b23e432da
|
data/Gemfile
CHANGED
data/{Readme.md → README.md}
RENAMED
@@ -30,7 +30,7 @@ con.login(email, password)
|
|
30
30
|
con.get_contacts
|
31
31
|
```
|
32
32
|
## Generate mints files
|
33
|
-
This command will generate the mints_config.yml file, API controlles and routes to have available the mints endpoints
|
33
|
+
This command will generate the mints_config.yml.erb file, API controlles and routes to have available the mints endpoints
|
34
34
|
```bash
|
35
35
|
rails generate mints_files
|
36
36
|
```
|
@@ -46,8 +46,8 @@ This heritance will make the following class variables available:
|
|
46
46
|
|
47
47
|
| Variable | Description |
|
48
48
|
| --- | :---: |
|
49
|
-
| @host | Host defined in mints_config.yml file |
|
50
|
-
| @api_key | API key defined in mints_config.yml file |
|
49
|
+
| @host | Host defined in mints_config.yml.erb file |
|
50
|
+
| @api_key | API key defined in mints_config.yml.erb file |
|
51
51
|
| @mints_pub | An already instanced public client |
|
52
52
|
| @contact_token | A token used by mints to identify the contact |
|
53
53
|
| @visit_id | An identifier of the visit registered |
|
@@ -73,8 +73,8 @@ end
|
|
73
73
|
This heritance will make the following class variables available:
|
74
74
|
| Variable | Description |
|
75
75
|
| --- | :---: |
|
76
|
-
| @host | Host defined in mints_config.yml file |
|
77
|
-
| @api_key | API key defined in mints_config.yml file |
|
76
|
+
| @host | Host defined in mints_config.yml.erb file |
|
77
|
+
| @api_key | API key defined in mints_config.yml.erb file |
|
78
78
|
| @mints_user | An already instanced user client (not usable until call the user login method) |
|
79
79
|
|
80
80
|
And the following controller methods:
|
data/lib/client.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "httparty"
|
2
2
|
require "json"
|
3
3
|
require "addressable"
|
4
|
+
require "redis"
|
4
5
|
module Mints
|
5
6
|
class Client
|
6
7
|
attr_reader :host
|
@@ -24,10 +25,42 @@ module Mints
|
|
24
25
|
uri = Addressable::URI.new
|
25
26
|
uri.query_values = options
|
26
27
|
end
|
27
|
-
|
28
|
+
|
28
29
|
full_url = "#{@host}#{base_url}#{url}#{uri}"
|
29
|
-
|
30
|
-
|
30
|
+
response = nil
|
31
|
+
if action === 'get'
|
32
|
+
|
33
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
34
|
+
config = YAML.load template.result(binding)
|
35
|
+
url_need_cache = false
|
36
|
+
result_from_cache = false
|
37
|
+
time = 0
|
38
|
+
|
39
|
+
if config['redis_cache']['use_cache']
|
40
|
+
config['redis_cache']['groups'].each do |group|
|
41
|
+
group['urls'].each do |url|
|
42
|
+
if full_url.match url
|
43
|
+
time = group['time']
|
44
|
+
url_need_cache = true
|
45
|
+
@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)
|
46
|
+
if @redis_server.get(full_url)
|
47
|
+
response = @redis_server.get(full_url)
|
48
|
+
result_from_cache = true
|
49
|
+
else
|
50
|
+
response = self.send("#{@scope}_#{action}", "#{full_url}")
|
51
|
+
@redis_server.setex(full_url,time,response)
|
52
|
+
end
|
53
|
+
break
|
54
|
+
end
|
55
|
+
end
|
56
|
+
break if url_need_cache
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
if !url_need_cache
|
61
|
+
response = self.send("#{@scope}_#{action}", "#{full_url}")
|
62
|
+
end
|
63
|
+
|
31
64
|
elsif action === 'create' or action === 'post'
|
32
65
|
action = 'post'
|
33
66
|
response = self.send("#{@scope}_#{action}", "#{full_url}", data)
|
@@ -35,14 +68,15 @@ module Mints
|
|
35
68
|
action = 'put'
|
36
69
|
response = self.send("#{@scope}_#{action}", "#{full_url}", data)
|
37
70
|
end
|
38
|
-
if
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
71
|
+
if result_from_cache
|
72
|
+
return parsed_response = JSON.parse(response)
|
73
|
+
else
|
74
|
+
if (response.response.code == "404")
|
75
|
+
raise 'NotFoundError'
|
76
|
+
end
|
77
|
+
parsed_response = JSON.parse(response.body)
|
78
|
+
return parsed_response
|
79
|
+
end
|
46
80
|
end
|
47
81
|
|
48
82
|
def method_missing(name, *args, &block)
|
@@ -183,7 +217,8 @@ module Mints
|
|
183
217
|
def contact_get(url)
|
184
218
|
headers = {
|
185
219
|
"ApiKey" => @api_key,
|
186
|
-
"Accept" => "application/json"
|
220
|
+
"Accept" => "application/json",
|
221
|
+
"ContactToken" => @contact_token
|
187
222
|
}
|
188
223
|
headers["Authorization"] = "Bearer #{@session_token}" if @session_token
|
189
224
|
return self.http_get(url, headers)
|
@@ -192,7 +227,8 @@ module Mints
|
|
192
227
|
def contact_post(url, data)
|
193
228
|
headers = {
|
194
229
|
"ApiKey" => @api_key,
|
195
|
-
"Accept" => "application/json"
|
230
|
+
"Accept" => "application/json",
|
231
|
+
"ContactToken" => @contact_token
|
196
232
|
}
|
197
233
|
headers["Authorization"] = "Bearer #{@session_token}" if @session_token
|
198
234
|
return self.http_post(url, headers, data)
|
@@ -201,7 +237,8 @@ module Mints
|
|
201
237
|
def contact_put(url, data)
|
202
238
|
headers = {
|
203
239
|
"ApiKey" => @api_key,
|
204
|
-
"Accept" => "application/json"
|
240
|
+
"Accept" => "application/json",
|
241
|
+
"ContactToken" => @contact_token
|
205
242
|
}
|
206
243
|
headers["Authorization"] = "Bearer #{@session_token}" if @session_token
|
207
244
|
return self.http_post(url, headers, data)
|
@@ -241,9 +278,9 @@ module Mints
|
|
241
278
|
h = {
|
242
279
|
"Accept" => "application/json",
|
243
280
|
"Content-Type" => "application/json",
|
244
|
-
"ApiKey" => @api_key
|
245
|
-
"ContactToken" => @session_token
|
281
|
+
"ApiKey" => @api_key
|
246
282
|
}
|
283
|
+
h["ContactToken"] = @contact_token if @contact_token
|
247
284
|
if headers
|
248
285
|
headers.each do |k,v|
|
249
286
|
h[k] = v
|
@@ -256,9 +293,9 @@ module Mints
|
|
256
293
|
h = {
|
257
294
|
"Accept" => "application/json",
|
258
295
|
"Content-Type" => "application/json",
|
259
|
-
"ApiKey" => @api_key
|
260
|
-
"ContactToken" => @session_token
|
296
|
+
"ApiKey" => @api_key
|
261
297
|
}
|
298
|
+
h["ContactToken"] = @session_token if @session_token
|
262
299
|
if headers
|
263
300
|
headers.each do |k,v|
|
264
301
|
h[k] = v
|
@@ -271,9 +308,9 @@ module Mints
|
|
271
308
|
h = {
|
272
309
|
"Accept" => "application/json",
|
273
310
|
"Content-Type" => "application/json",
|
274
|
-
"ApiKey" => @api_key
|
275
|
-
"ContactToken" => @session_token
|
311
|
+
"ApiKey" => @api_key
|
276
312
|
}
|
313
|
+
h["ContactToken"] = @contact_token if @contact_token
|
277
314
|
if headers
|
278
315
|
headers.each do |k,v|
|
279
316
|
h[k] = v
|
data/lib/contact.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require_relative "./client.rb"
|
2
|
+
include ActionController::Cookies
|
2
3
|
module Mints
|
3
4
|
class Contact
|
4
5
|
attr_reader :client
|
@@ -10,20 +11,6 @@ module Mints
|
|
10
11
|
@client = Mints::Client.new(host, api_key, "contact", session_token, debug)
|
11
12
|
end
|
12
13
|
|
13
|
-
##
|
14
|
-
# === Register.
|
15
|
-
# Register a new contact
|
16
|
-
#
|
17
|
-
def register(given_name, last_name, email, password)
|
18
|
-
data = {
|
19
|
-
given_name: given_name,
|
20
|
-
last_name: last_name,
|
21
|
-
email: email,
|
22
|
-
password: password
|
23
|
-
}
|
24
|
-
return @client.raw("post", "/contacts/register", nil, {data: data})
|
25
|
-
end
|
26
|
-
|
27
14
|
##
|
28
15
|
# === Login.
|
29
16
|
# Starts a contact session
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Mints connection configuration
|
2
|
+
mints:
|
3
|
+
host: http://your_host_goes_here.com
|
4
|
+
api_key: your_mints_api_key_goes_here
|
5
|
+
mints_slug: slug_id #save id and token in redis
|
6
|
+
user_email: mints_user_email
|
7
|
+
user_password: mints_user_password
|
8
|
+
redis_cache:
|
9
|
+
use_cache: boolean_value_to_enable_and_disable_cache
|
10
|
+
redis_host: your_redis_server_host
|
11
|
+
redis_port: your_redis_server_port
|
12
|
+
redis_db: your_redis_database
|
13
|
+
groups:
|
14
|
+
- urls:
|
15
|
+
- group_of_urls
|
16
|
+
time: time_that_will_be_applied_to_urls_in_seconds
|
17
|
+
sdk:
|
18
|
+
debug: false
|
19
|
+
cookies_iframe:
|
20
|
+
activated: boolean_value_to_enable_and_disable_cookies_iframe
|
21
|
+
expire_time: expire_time_of_cookies_iframe_in_hours
|
22
|
+
hosts:
|
23
|
+
- array_of_host_to_send_cookies
|
File without changes
|
@@ -2,7 +2,7 @@ class MintsFilesGenerator < Rails::Generators::Base
|
|
2
2
|
source_root(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
include Rails::Generators::Actions
|
4
4
|
def create_mints_files
|
5
|
-
copy_file 'mints_config.yml', 'mints_config.yml'
|
5
|
+
copy_file 'mints_config.yml.erb', 'mints_config.yml.erb'
|
6
6
|
copy_file 'mints_user_controller.rb', './app/controllers/api/mints_user_controller.rb'
|
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'
|
File without changes
|
File without changes
|
data/lib/mints.rb
CHANGED
@@ -2,6 +2,7 @@ require_relative "./pub.rb"
|
|
2
2
|
require_relative "./contact.rb"
|
3
3
|
require_relative "./user.rb"
|
4
4
|
require_relative "./mints/controllers/base_controller.rb"
|
5
|
+
require_relative "./mints/controllers/base_api_controller.rb"
|
5
6
|
require_relative "./mints/controllers/admin_base_controller.rb"
|
6
7
|
require_relative "./mints/controllers/public_api_controller.rb"
|
7
8
|
require_relative "./mints/controllers/contact_api_controller.rb"
|
@@ -41,8 +41,9 @@ module Mints
|
|
41
41
|
# === Set Mints user client.
|
42
42
|
# Initialize the public client and set the user token
|
43
43
|
def set_mints_user_client
|
44
|
-
if File.exists?("#{Rails.root}/mints_config.yml")
|
45
|
-
|
44
|
+
if File.exists?("#{Rails.root}/mints_config.yml.erb")
|
45
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
46
|
+
config = YAML.load template.result(binding)
|
46
47
|
@host = config["mints"]["host"]
|
47
48
|
@api_key = config["mints"]["api_key"]
|
48
49
|
@debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Mints
|
2
|
+
class BaseApiController < ActionController::Base
|
3
|
+
before_action :set_mints_pub_client
|
4
|
+
|
5
|
+
##
|
6
|
+
# === Mints Contact Login.
|
7
|
+
# Starts a contact session in mints.cloud and set a session cookie
|
8
|
+
def mints_contact_login(email, password)
|
9
|
+
# Login in mints
|
10
|
+
response = @mints_contact.login(email, password)
|
11
|
+
# Get session token from response
|
12
|
+
session_token = response['session_token']
|
13
|
+
id_token = response['contact']['id_token']
|
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
|
17
|
+
@contact_token = id_token
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# === Mints Contact Logout.
|
22
|
+
# Destroy session from mints.cloud and delete local session cookie
|
23
|
+
def mints_contact_logout
|
24
|
+
# Logout from mints
|
25
|
+
@mints_contact.logout
|
26
|
+
# Delete local cookie
|
27
|
+
cookies.delete(:mints_contact_session_token)
|
28
|
+
cookies.delete(:mints_contact_id)
|
29
|
+
@contact_token = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
##
|
35
|
+
# === Set mints pub.
|
36
|
+
# Initialize the public client and set the contact token
|
37
|
+
def set_mints_pub_client
|
38
|
+
if File.exists?("#{Rails.root}/mints_config.yml.erb")
|
39
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
40
|
+
config = YAML.load template.result(binding)
|
41
|
+
@host = config["mints"]["host"]
|
42
|
+
@api_key = config["mints"]["api_key"]
|
43
|
+
@debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
|
44
|
+
else
|
45
|
+
raise 'MintsBadCredentialsError'
|
46
|
+
end
|
47
|
+
# Initialize mints pub client, credentials taken from mints_config.yml.erb file
|
48
|
+
@mints_pub = Mints::Pub.new(@host, @api_key, nil, @debug)
|
49
|
+
# Set contact token from cookie
|
50
|
+
@mints_pub.client.session_token = @contact_token
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -24,8 +24,11 @@ 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
28
|
# Set a permanent cookie with the session token
|
28
29
|
cookies.permanent[:mints_contact_session_token] = session_token
|
30
|
+
cookies.permanent[:mints_contact_id] = id_token
|
31
|
+
@contact_token = id_token
|
29
32
|
end
|
30
33
|
|
31
34
|
##
|
@@ -36,6 +39,8 @@ module Mints
|
|
36
39
|
@mints_contact.logout
|
37
40
|
# Delete local cookie
|
38
41
|
cookies.delete(:mints_contact_session_token)
|
42
|
+
cookies.delete(:mints_contact_id)
|
43
|
+
@contact_token = nil
|
39
44
|
end
|
40
45
|
|
41
46
|
private
|
@@ -54,15 +59,16 @@ module Mints
|
|
54
59
|
# === Set mints pub.
|
55
60
|
# Initialize the public client and set the contact token
|
56
61
|
def set_mints_pub_client
|
57
|
-
if File.exists?("#{Rails.root}/mints_config.yml")
|
58
|
-
|
62
|
+
if File.exists?("#{Rails.root}/mints_config.yml.erb")
|
63
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
64
|
+
config = YAML.load template.result(binding)
|
59
65
|
@host = config["mints"]["host"]
|
60
66
|
@api_key = config["mints"]["api_key"]
|
61
67
|
@debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
|
62
68
|
else
|
63
69
|
raise 'MintsBadCredentialsError'
|
64
70
|
end
|
65
|
-
# Initialize mints pub client, credentials taken from mints_config.yml file
|
71
|
+
# Initialize mints pub client, credentials taken from mints_config.yml.erb file
|
66
72
|
@mints_pub = Mints::Pub.new(@host, @api_key, nil, @debug)
|
67
73
|
# Set contact token from cookie
|
68
74
|
@mints_pub.client.session_token = @contact_token
|
@@ -18,10 +18,56 @@ module Mints
|
|
18
18
|
session_token = cookies[:mints_contact_session_token]
|
19
19
|
headers["Authorization"] = "Bearer #{session_token}"
|
20
20
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
|
22
|
+
url_need_cache = false
|
23
|
+
result_from_cache = false
|
24
|
+
time = 0
|
25
|
+
full_url = request.original_url
|
26
|
+
|
27
|
+
if request.method == "GET"
|
28
|
+
if @use_cache
|
29
|
+
@redis_config['groups'].each do |group|
|
30
|
+
group['urls'].each do |url|
|
31
|
+
if full_url.match url
|
32
|
+
time = group['time']
|
33
|
+
url_need_cache = true
|
34
|
+
break
|
35
|
+
end
|
36
|
+
end
|
37
|
+
break if url_need_cache
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
if url_need_cache
|
43
|
+
if @redis_server.get(full_url)
|
44
|
+
response = @redis_server.get(full_url)
|
45
|
+
result_from_cache = true
|
46
|
+
render json: response
|
47
|
+
else
|
48
|
+
reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
|
49
|
+
# Request succeded!
|
50
|
+
config.on_response do |code, response|
|
51
|
+
@redis_server.setex(full_url,time,response.body)
|
52
|
+
end
|
53
|
+
# Request failed!
|
54
|
+
config.on_missing do |code, response|
|
55
|
+
# We got a 404!
|
56
|
+
if code == 404
|
57
|
+
raise ActionController::RoutingError.new('Not Found')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
else
|
63
|
+
reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
|
64
|
+
# Request failed!
|
65
|
+
config.on_missing do |code, response|
|
66
|
+
# We got a 404!
|
67
|
+
if code == 404
|
68
|
+
raise ActionController::RoutingError.new('Not Found')
|
69
|
+
end
|
70
|
+
end
|
25
71
|
end
|
26
72
|
end
|
27
73
|
end
|
@@ -29,10 +75,14 @@ module Mints
|
|
29
75
|
private
|
30
76
|
|
31
77
|
def set_config_variables
|
32
|
-
if File.exists?("#{Rails.root}/mints_config.yml")
|
33
|
-
|
78
|
+
if File.exists?("#{Rails.root}/mints_config.yml.erb")
|
79
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
80
|
+
config = YAML.load template.result(binding)
|
34
81
|
@host = config["mints"]["host"]
|
35
|
-
@api_key = config["mints"]["api_key"]
|
82
|
+
@api_key = config["mints"]["api_key"]
|
83
|
+
@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) if config['redis_cache']['use_cache']
|
84
|
+
@redis_config = config['redis_cache']
|
85
|
+
@use_cache = config['redis_cache']['use_cache']
|
36
86
|
end
|
37
87
|
end
|
38
88
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'reverse_proxy/controller'
|
2
2
|
require 'reverse_proxy/client'
|
3
|
+
require 'redis'
|
3
4
|
module Mints
|
4
5
|
class PublicAPIController < ActionController::API
|
5
6
|
include ReverseProxy::Controller
|
@@ -12,21 +13,72 @@ module Mints
|
|
12
13
|
'Content-Type'=> 'application/json',
|
13
14
|
'Accept'=> 'application/json'
|
14
15
|
}
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
|
17
|
+
url_need_cache = false
|
18
|
+
result_from_cache = false
|
19
|
+
time = 0
|
20
|
+
full_url = request.original_url
|
21
|
+
|
22
|
+
if request.method == "GET"
|
23
|
+
if @use_cache
|
24
|
+
@redis_config['groups'].each do |group|
|
25
|
+
group['urls'].each do |url|
|
26
|
+
if full_url.match url
|
27
|
+
time = group['time']
|
28
|
+
url_need_cache = true
|
29
|
+
break
|
30
|
+
end
|
31
|
+
end
|
32
|
+
break if url_need_cache
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
if url_need_cache
|
38
|
+
if @redis_server.get(full_url)
|
39
|
+
response = @redis_server.get(full_url)
|
40
|
+
result_from_cache = true
|
41
|
+
render json: response
|
42
|
+
else
|
43
|
+
reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
|
44
|
+
# Request succeded!
|
45
|
+
config.on_response do |code, response|
|
46
|
+
@redis_server.setex(full_url,time,response.body)
|
47
|
+
end
|
48
|
+
# Request failed!
|
49
|
+
config.on_missing do |code, response|
|
50
|
+
# We got a 404!
|
51
|
+
if code == 404
|
52
|
+
raise ActionController::RoutingError.new('Not Found')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
else
|
58
|
+
reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
|
59
|
+
# Request failed!
|
60
|
+
config.on_missing do |code, response|
|
61
|
+
# We got a 404!
|
62
|
+
if code == 404
|
63
|
+
raise ActionController::RoutingError.new('Not Found')
|
64
|
+
end
|
65
|
+
end
|
19
66
|
end
|
20
67
|
end
|
68
|
+
|
21
69
|
end
|
22
70
|
|
23
71
|
private
|
24
72
|
|
25
73
|
def set_config_variables
|
26
|
-
if File.exists?("#{Rails.root}/mints_config.yml")
|
27
|
-
|
74
|
+
if File.exists?("#{Rails.root}/mints_config.yml.erb")
|
75
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
76
|
+
config = YAML.load template.result(binding)
|
28
77
|
@host = config["mints"]["host"]
|
29
|
-
@api_key = config["mints"]["api_key"]
|
78
|
+
@api_key = config["mints"]["api_key"]
|
79
|
+
@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) if config['redis_cache']['use_cache']
|
80
|
+
@redis_config = config['redis_cache']
|
81
|
+
@use_cache = config['redis_cache']['use_cache']
|
30
82
|
end
|
31
83
|
end
|
32
84
|
end
|
@@ -18,10 +18,56 @@ module Mints
|
|
18
18
|
session_token = cookies[:mints_user_session_token]
|
19
19
|
headers["Authorization"] = "Bearer #{session_token}"
|
20
20
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
|
22
|
+
url_need_cache = false
|
23
|
+
result_from_cache = false
|
24
|
+
time = 0
|
25
|
+
full_url = request.original_url
|
26
|
+
|
27
|
+
if request.method == "GET"
|
28
|
+
if url_need_cache
|
29
|
+
@redis_config['groups'].each do |group|
|
30
|
+
group['urls'].each do |url|
|
31
|
+
if full_url.match url
|
32
|
+
time = group['time']
|
33
|
+
url_need_cache = true
|
34
|
+
break
|
35
|
+
end
|
36
|
+
end
|
37
|
+
break if url_need_cache
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
if url_need_cache
|
43
|
+
if @redis_server.get(full_url)
|
44
|
+
response = @redis_server.get(full_url)
|
45
|
+
result_from_cache = true
|
46
|
+
render json: response
|
47
|
+
else
|
48
|
+
reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
|
49
|
+
# Request succeded!
|
50
|
+
config.on_response do |code, response|
|
51
|
+
@redis_server.setex(full_url,time,response.body)
|
52
|
+
end
|
53
|
+
# Request failed!
|
54
|
+
config.on_missing do |code, response|
|
55
|
+
# We got a 404!
|
56
|
+
if code == 404
|
57
|
+
raise ActionController::RoutingError.new('Not Found')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
else
|
63
|
+
reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
|
64
|
+
# Request failed!
|
65
|
+
config.on_missing do |code, response|
|
66
|
+
# We got a 404!
|
67
|
+
if code == 404
|
68
|
+
raise ActionController::RoutingError.new('Not Found')
|
69
|
+
end
|
70
|
+
end
|
25
71
|
end
|
26
72
|
end
|
27
73
|
end
|
@@ -29,10 +75,14 @@ module Mints
|
|
29
75
|
private
|
30
76
|
|
31
77
|
def set_config_variables
|
32
|
-
if File.exists?("#{Rails.root}/mints_config.yml")
|
33
|
-
|
78
|
+
if File.exists?("#{Rails.root}/mints_config.yml.erb")
|
79
|
+
template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
|
80
|
+
config = YAML.load template.result(binding)
|
34
81
|
@host = config["mints"]["host"]
|
35
82
|
@api_key = config["mints"]["api_key"]
|
83
|
+
@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) if config['redis_cache']['use_cache']
|
84
|
+
@redis_config = config['redis_cache']
|
85
|
+
@use_cache = config['redis_cache']['use_cache'] if config['redis_cache']['use_cache']
|
36
86
|
end
|
37
87
|
end
|
38
88
|
end
|
data/lib/pub.rb
CHANGED
@@ -7,7 +7,7 @@ module Mints
|
|
7
7
|
# == Usage example
|
8
8
|
# Initialize
|
9
9
|
# pub = Mints::Pub.new(mints_url, api_key)
|
10
|
-
# or if host and api_key are provided by mints_config.yml
|
10
|
+
# or if host and api_key are provided by mints_config.yml.erb
|
11
11
|
# pub = Mints::Pub.new
|
12
12
|
# Call any function
|
13
13
|
# pub.get_products
|
@@ -42,8 +42,8 @@ module Mints
|
|
42
42
|
# * +contact_token+ - [String] Cookie 'mints_contact_id' value (mints_contact_token)
|
43
43
|
# ==== Return
|
44
44
|
# Returns a Client object
|
45
|
-
def initialize(host, api_key, contact_token = nil, debug = false)
|
46
|
-
@client = Mints::Client.new(host, api_key, contact_token, debug)
|
45
|
+
def initialize(host, api_key, contact_token = nil, debug = false)
|
46
|
+
@client = Mints::Client.new(host, api_key, 'public', contact_token, debug)
|
47
47
|
end
|
48
48
|
|
49
49
|
##
|
@@ -61,7 +61,7 @@ module Mints
|
|
61
61
|
user_agent: user_agent || request.user_agent,
|
62
62
|
url: url || request.fullpath
|
63
63
|
}
|
64
|
-
response = @client.raw("post", "/register-visit", nil, data)
|
64
|
+
response = @client.raw("post", "/register-visit", nil, data.to_json)
|
65
65
|
return response
|
66
66
|
end
|
67
67
|
|
@@ -178,7 +178,7 @@ module Mints
|
|
178
178
|
# ==== Parameters
|
179
179
|
# * +data+ - [Hash] Data to be submited
|
180
180
|
def submit_form(data)
|
181
|
-
return @client.raw("post", "/forms/
|
181
|
+
return @client.raw("post", "/content/forms/submit", nil, data)
|
182
182
|
end
|
183
183
|
|
184
184
|
##
|
data/lib/user.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mints
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Ruben Gomez Garcia, Omar Mora
|
7
|
+
- Ruben Gomez Garcia, Omar Mora, Luis Payan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -50,6 +50,26 @@ dependencies:
|
|
50
50
|
- - ">="
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: 0.18.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: redis
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 4.2.2
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 4.2.2
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 4.2.2
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 4.2.2
|
53
73
|
- !ruby/object:Gem::Dependency
|
54
74
|
name: addressable
|
55
75
|
requirement: !ruby/object:Gem::Requirement
|
@@ -97,16 +117,17 @@ extensions: []
|
|
97
117
|
extra_rdoc_files: []
|
98
118
|
files:
|
99
119
|
- Gemfile
|
100
|
-
-
|
120
|
+
- README.md
|
101
121
|
- lib/client.rb
|
102
122
|
- lib/contact.rb
|
103
|
-
- lib/generators/mints_config.yml
|
123
|
+
- lib/generators/mints_config.yml.erb
|
104
124
|
- lib/generators/mints_contact_controller.rb
|
105
125
|
- lib/generators/mints_files_generator.rb
|
106
126
|
- lib/generators/mints_public_controller.rb
|
107
127
|
- lib/generators/mints_user_controller.rb
|
108
128
|
- lib/mints.rb
|
109
129
|
- lib/mints/controllers/admin_base_controller.rb
|
130
|
+
- lib/mints/controllers/base_api_controller.rb
|
110
131
|
- lib/mints/controllers/base_controller.rb
|
111
132
|
- lib/mints/controllers/contact_api_controller.rb
|
112
133
|
- lib/mints/controllers/public_api_controller.rb
|