mints 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/lib/client.rb +42 -9
- data/lib/generators/mints_config.yml +7 -0
- data/lib/mints/controllers/contact_api_controller.rb +53 -7
- data/lib/mints/controllers/public_api_controller.rb +55 -7
- data/lib/mints/controllers/user_api_controller.rb +52 -6
- metadata +21 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91ef0909fa4361a6a3644378c438c64341bfbf31bc6d9ef7f0ed178bd35b5dac
|
4
|
+
data.tar.gz: 0a4c5c14a4550ac56917259ecc822d9afbe779e95507f9607c9248ce19c53caa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1df0f3a2b3f2ff3c27a61221fab855138508991a0ea552e1111f15df723473e5eeb3ec8c46650aa1670bc00bef8242424fd88e20d90c729d12deac15d25453a4
|
7
|
+
data.tar.gz: c9fb8c066bdcb38237d394bcef4d155bd8bcc29ac3f19b1f4adb905b7483b678c1dbb1625ba8b31be6cada2b565c465f3da3a1d0e6f62cae50c7cfcbc0bfcf44
|
data/Gemfile
CHANGED
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,41 @@ 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}"
|
30
|
+
response = nil
|
29
31
|
if action === 'get'
|
32
|
+
|
33
|
+
config = YAML.load_file("#{Rails.root}/mints_config.yml")
|
34
|
+
url_need_cache = false
|
35
|
+
result_from_cache = false
|
36
|
+
time = 0
|
37
|
+
|
38
|
+
if config['redis_cache']['use_cache']
|
39
|
+
config['redis_cache']['groups'].each do |group|
|
40
|
+
group['urls'].each do |url|
|
41
|
+
if full_url.match url
|
42
|
+
time = group['time']
|
43
|
+
url_need_cache = true
|
44
|
+
@redis_server = Redis.new(host: config['redis_cache']['redis_host'])
|
45
|
+
if @redis_server.get(full_url)
|
46
|
+
response = @redis_server.get(full_url)
|
47
|
+
result_from_cache = true
|
48
|
+
else
|
49
|
+
response = self.send("#{@scope}_#{action}", "#{full_url}")
|
50
|
+
@redis_server.setex(full_url,time,response)
|
51
|
+
end
|
52
|
+
break
|
53
|
+
end
|
54
|
+
end
|
55
|
+
break if url_need_cache
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
if !url_need_cache
|
30
60
|
response = self.send("#{@scope}_#{action}", "#{full_url}")
|
61
|
+
end
|
62
|
+
|
31
63
|
elsif action === 'create' or action === 'post'
|
32
64
|
action = 'post'
|
33
65
|
response = self.send("#{@scope}_#{action}", "#{full_url}", data)
|
@@ -35,14 +67,15 @@ module Mints
|
|
35
67
|
action = 'put'
|
36
68
|
response = self.send("#{@scope}_#{action}", "#{full_url}", data)
|
37
69
|
end
|
38
|
-
if
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
70
|
+
if result_from_cache
|
71
|
+
return parsed_response = JSON.parse(response)
|
72
|
+
else
|
73
|
+
if (response.response.code == "404")
|
74
|
+
raise 'NotFoundError'
|
75
|
+
end
|
76
|
+
parsed_response = JSON.parse(response.body)
|
77
|
+
return parsed_response
|
78
|
+
end
|
46
79
|
end
|
47
80
|
|
48
81
|
def method_missing(name, *args, &block)
|
@@ -2,5 +2,12 @@
|
|
2
2
|
mints:
|
3
3
|
host: http://your_host_goes_here.com
|
4
4
|
api_key: your_mints_api_key_goes_here
|
5
|
+
redis_cache:
|
6
|
+
use_cache: boolean_value_to_enable_and_disable_cache
|
7
|
+
redis_host: your_redis_host
|
8
|
+
groups:
|
9
|
+
- urls:
|
10
|
+
- group_of_urls
|
11
|
+
time: time_that_will_be_applied_to_urls_in_seconds
|
5
12
|
sdk:
|
6
13
|
debug: false
|
@@ -18,12 +18,55 @@ 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
|
-
|
25
|
-
|
26
|
-
|
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
|
27
70
|
end
|
28
71
|
end
|
29
72
|
end
|
@@ -35,7 +78,10 @@ module Mints
|
|
35
78
|
if File.exists?("#{Rails.root}/mints_config.yml")
|
36
79
|
config = YAML.load_file("#{Rails.root}/mints_config.yml")
|
37
80
|
@host = config["mints"]["host"]
|
38
|
-
@api_key = config["mints"]["api_key"]
|
81
|
+
@api_key = config["mints"]["api_key"]
|
82
|
+
@redis_server = Redis.new(host: config['redis_cache']['redis_host']) if config['redis_cache']['use_cache']
|
83
|
+
@redis_config = config['redis_cache']
|
84
|
+
@use_cache = config['redis_cache']['use_cache']
|
39
85
|
end
|
40
86
|
end
|
41
87
|
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,15 +13,59 @@ module Mints
|
|
12
13
|
'Content-Type'=> 'application/json',
|
13
14
|
'Accept'=> 'application/json'
|
14
15
|
}
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
21
65
|
end
|
22
66
|
end
|
23
67
|
end
|
68
|
+
|
24
69
|
end
|
25
70
|
|
26
71
|
private
|
@@ -29,7 +74,10 @@ module Mints
|
|
29
74
|
if File.exists?("#{Rails.root}/mints_config.yml")
|
30
75
|
config = YAML.load_file("#{Rails.root}/mints_config.yml")
|
31
76
|
@host = config["mints"]["host"]
|
32
|
-
@api_key = config["mints"]["api_key"]
|
77
|
+
@api_key = config["mints"]["api_key"]
|
78
|
+
@redis_server = Redis.new(host: config['redis_cache']['redis_host']) if config['redis_cache']['use_cache']
|
79
|
+
@redis_config = config['redis_cache']
|
80
|
+
@use_cache = config['redis_cache']['use_cache']
|
33
81
|
end
|
34
82
|
end
|
35
83
|
end
|
@@ -18,12 +18,55 @@ 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
|
-
|
25
|
-
|
26
|
-
|
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
|
27
70
|
end
|
28
71
|
end
|
29
72
|
end
|
@@ -36,6 +79,9 @@ module Mints
|
|
36
79
|
config = YAML.load_file("#{Rails.root}/mints_config.yml")
|
37
80
|
@host = config["mints"]["host"]
|
38
81
|
@api_key = config["mints"]["api_key"]
|
82
|
+
@redis_server = Redis.new(host: config['redis_cache']['redis_host'])
|
83
|
+
@redis_config = config['redis_cache']
|
84
|
+
@use_cache = config['redis_cache']['use_cache'] if config['redis_cache']['use_cache']
|
39
85
|
end
|
40
86
|
end
|
41
87
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruben Gomez Garcia, Omar Mora, Luis Payan
|
@@ -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
|