mints 0.0.26 → 0.0.28

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -1
  3. data/lib/client.rb +75 -37
  4. data/lib/contact.rb +61 -62
  5. data/lib/errors.rb +98 -0
  6. data/lib/generators/mints_config.yml.erb +0 -2
  7. data/lib/generators/mints_link.rb +3 -3
  8. data/lib/mints/controllers/admin_base_controller.rb +8 -66
  9. data/lib/mints/controllers/base_api_controller.rb +16 -134
  10. data/lib/mints/controllers/base_controller.rb +20 -106
  11. data/lib/mints/controllers/concerns/mints_clients.rb +74 -0
  12. data/lib/mints/controllers/concerns/read_config_file.rb +28 -0
  13. data/lib/mints/controllers/contact_api_controller.rb +6 -85
  14. data/lib/mints/controllers/public_api_controller.rb +6 -79
  15. data/lib/mints/controllers/user_api_controller.rb +7 -82
  16. data/lib/mints/helpers/contact_auth_helper.rb +67 -0
  17. data/lib/{mints_helper.rb → mints/helpers/mints_helper.rb} +7 -5
  18. data/lib/mints/helpers/proxy_controllers_methods.rb +126 -0
  19. data/lib/mints/helpers/user_auth_helper.rb +53 -0
  20. data/lib/mints.rb +2 -0
  21. data/lib/pub.rb +56 -56
  22. data/lib/user/config/api_keys.rb +7 -7
  23. data/lib/user/config/appointments.rb +16 -16
  24. data/lib/user/config/attribute_groups.rb +10 -10
  25. data/lib/user/config/attributes.rb +10 -10
  26. data/lib/user/config/calendars.rb +9 -9
  27. data/lib/user/config/config.rb +7 -7
  28. data/lib/user/config/importers.rb +15 -15
  29. data/lib/user/config/public_folders.rb +11 -11
  30. data/lib/user/config/relationships.rb +11 -11
  31. data/lib/user/config/roles.rb +9 -9
  32. data/lib/user/config/seeds.rb +5 -5
  33. data/lib/user/config/system_settings.rb +5 -5
  34. data/lib/user/config/tags.rb +5 -5
  35. data/lib/user/config/taxonomies.rb +15 -15
  36. data/lib/user/config/teams.rb +67 -68
  37. data/lib/user/config/users.rb +70 -71
  38. data/lib/user/contacts/contacts.rb +19 -20
  39. data/lib/user/content/assets.rb +273 -278
  40. data/lib/user/content/content.rb +48 -48
  41. data/lib/user/content/content_instances.rb +136 -138
  42. data/lib/user/content/content_templates.rb +103 -103
  43. data/lib/user/content/conversations.rb +193 -193
  44. data/lib/user/content/dam.rb +83 -80
  45. data/lib/user/content/forms.rb +229 -233
  46. data/lib/user/content/message_templates.rb +152 -151
  47. data/lib/user/content/messages.rb +87 -87
  48. data/lib/user/content/pages.rb +82 -82
  49. data/lib/user/content/stories.rb +101 -101
  50. data/lib/user/content/story_templates.rb +88 -88
  51. data/lib/user/content/story_versions.rb +115 -117
  52. data/lib/user/crm/companies.rb +103 -105
  53. data/lib/user/crm/contacts.rb +285 -292
  54. data/lib/user/crm/crm.rb +19 -19
  55. data/lib/user/crm/deals.rb +103 -103
  56. data/lib/user/crm/favorites.rb +14 -13
  57. data/lib/user/crm/segments.rb +121 -123
  58. data/lib/user/crm/users.rb +20 -20
  59. data/lib/user/crm/workflow_step_objects.rb +84 -84
  60. data/lib/user/crm/workflow_steps.rb +45 -45
  61. data/lib/user/crm/workflows.rb +65 -65
  62. data/lib/user/ecommerce/ecommerce.rb +27 -27
  63. data/lib/user/ecommerce/item_prices.rb +82 -81
  64. data/lib/user/ecommerce/locations.rb +160 -160
  65. data/lib/user/ecommerce/order_items_groups.rb +107 -105
  66. data/lib/user/ecommerce/order_statuses.rb +24 -24
  67. data/lib/user/ecommerce/orders.rb +250 -246
  68. data/lib/user/ecommerce/price_lists.rb +68 -68
  69. data/lib/user/ecommerce/product_templates.rb +99 -99
  70. data/lib/user/ecommerce/product_variations.rb +122 -120
  71. data/lib/user/ecommerce/products.rb +160 -159
  72. data/lib/user/ecommerce/skus.rb +85 -85
  73. data/lib/user/ecommerce/taxes.rb +79 -79
  74. data/lib/user/ecommerce/variant_options.rb +66 -66
  75. data/lib/user/ecommerce/variant_values.rb +69 -69
  76. data/lib/user/helpers/helpers.rb +102 -101
  77. data/lib/user/helpers/object_activities.rb +78 -78
  78. data/lib/user/helpers/object_folders.rb +76 -76
  79. data/lib/user/helpers/user_folders.rb +77 -77
  80. data/lib/user/marketing/marketing.rb +114 -113
  81. data/lib/user/profile/profile.rb +94 -103
  82. data/lib/user.rb +4 -4
  83. metadata +41 -35
@@ -1,144 +1,26 @@
1
+ require_relative "./concerns/mints_clients.rb"
2
+ require_relative "../helpers/user_auth_helper.rb"
3
+ require_relative "../helpers/contact_auth_helper.rb"
4
+
1
5
  module Mints
2
6
  class BaseApiController < ActionController::Base
3
- before_action :set_mints_clients
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']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
14
- # Set a permanent cookie with the session 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
- @contact_token = id_token
18
- end
19
-
20
- ##
21
- # === Mints contact Login.
22
- # Starts a contact session in mints.cloud and set a session cookie
23
- def mints_contact_magic_link_login(hash)
24
- # Login in mints
25
- response = @mints_contact.magic_link_login(hash)
26
- if response['data']
27
- # Get session token from response
28
- session_token = response['data']['session_token']
29
- id_token = response['data']['contact']['contact_token'] ? response['data']['contact']['contact_token'] : response['data']['contact']['id_token']
30
- # Set a permanent cookie with the session 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
- @contact_token = id_token
34
- redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/'
35
- else
36
- redirect_to '/'
37
- end
38
- end
7
+ # Concerns
8
+ include MintsClients
39
9
 
40
- ##
41
- # === Mints Contact Logout.
42
- # Destroy session from mints.cloud and delete local session cookie
43
- def mints_contact_logout
44
- # Logout from mints
45
- @mints_contact.logout
46
- # Delete local cookie
47
- cookies.delete(:mints_contact_session_token)
48
- cookies.delete(:mints_contact_id)
49
- @contact_token = nil
50
- end
10
+ # Helpers
11
+ include ContactAuthHelper
12
+ include UserAuthHelper
51
13
 
52
- ##
53
- # === Mints user Login.
54
- # Starts a user session in mints.cloud and set a session cookie
55
- def mints_user_login(email, password)
56
- # Login in mints
57
- response = @mints_user.login(email, password)
58
- # Get session token from response
59
- session_token = response['api_token']
60
- # Set a permanent cookie with the session token
61
- cookies[:mints_user_session_token] = { value: session_token, secure: true, httponly: true, expires: 1.day }
14
+ def define_mints_clients
15
+ %w[ contact pub ]
62
16
  end
63
17
 
64
18
  ##
65
- # === Mints user Login.
66
- # Starts a user session in mints.cloud and set a session cookie
67
- def mints_user_magic_link_login(hash)
68
- # Login in mints
69
- response = @mints_user.magic_link_login(hash)
70
- if response['data']
71
- # Set a cookie with the session token
72
- cookies[:mints_user_session_token] = { value: response['data']['api_token'], secure: true, httponly: true, expires: 1.day }
73
- redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/'
74
- else
75
- redirect_to '/'
76
- end
77
- end
78
-
79
- ##
80
- # === Mints user Logout.
81
- # Destroy session from mints.cloud and delete local session cookie
82
- def mints_user_logout
83
- # Logout from mints
84
- # @mints_user.logout
85
- # Delete local cookie
86
- cookies.delete(:mints_user_session_token)
87
- end
88
-
89
- private
90
-
91
- ##
92
- # === Set mints clients (pub, user and contact)
93
- # Initialize all clients from mitns
94
- def set_mints_clients
95
- if File.exists?("#{Rails.root}/mints_config.yml.erb")
96
- template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
97
- config = YAML.load template.result(binding)
98
- @host = config["mints"]["host"]
99
- @api_key = config["mints"]["api_key"]
100
- @debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
101
-
102
- #public client
103
- set_mints_pub_client
104
- #contact client
105
- set_mints_contact_client
106
- #user client
107
- set_mints_user_client
108
- else
109
- raise 'MintsBadCredentialsError'
110
- end
111
- end
112
-
113
- ##
114
- # === Set mints pub.
115
- # Initialize the public client and set the contact token
116
- def set_mints_pub_client
117
-
118
- # Initialize mints pub client, credentials taken from mints_config.yml.erb file
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)
121
- # Set contact token from cookie
122
- @mints_pub.client.session_token = @contact_token
123
- end
124
-
125
- ##
126
- # === Set mints contact client.
127
- # Initialize the public client and set the contact token
128
- def set_mints_contact_client
129
- # Initialize mints clontact client
130
- session_token = cookies[:mints_contact_session_token] ? cookies[:mints_contact_session_token] : nil
131
- contact_token_id = cookies[:mints_contact_id] ? cookies[:mints_contact_id] : nil
132
- @mints_contact = Mints::Contact.new(@host, @api_key, session_token, contact_token_id, @debug)
133
- end
134
-
135
- ##
136
- # === Set Mints user client.
137
- # Initialize the public client and set the user token
138
- def set_mints_user_client
139
- # Initialize mints user client
140
- session_token = cookies[:mints_user_session_token] ? cookies[:mints_user_session_token] : nil
141
- @mints_user = Mints::User.new(@host, @api_key, session_token, @debug)
19
+ # === Mints contact Login.
20
+ # This method works to override the base to add the redirect parameter
21
+ # The main method is located in contact_auth_helper.rb
22
+ def mints_contact_magic_link_login(hash)
23
+ super(hash, true)
142
24
  end
143
25
  end
144
26
  end
@@ -1,63 +1,21 @@
1
+ require_relative "./concerns/mints_clients.rb"
2
+ require_relative "../helpers/contact_auth_helper.rb"
3
+
1
4
  module Mints
2
5
  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
6
+ # Concerns
7
+ include MintsClients
18
8
 
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
- id_token = response['contact']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
28
- # Set a permanent cookie with the session 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
- @contact_token = id_token
32
- end
9
+ # Helpers
10
+ include ContactAuthHelper
33
11
 
34
- ##
35
- # === Mints Contact Magic Link Login.
36
- # Starts a contact session in mints.cloud and set a session cookie
37
- def mints_contact_magic_link_login(token)
38
- # Login in mints
39
- response = @mints_contact.login(email, password)
40
- # Get session token from response
41
- session_token = response['session_token']
42
- id_token = response['contact']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
43
- # Set a permanent cookie with the session 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
- @contact_token = id_token
47
- end
12
+ before_action :register_visit
48
13
 
49
- ##
50
- # === Mints Contact Logout.
51
- # Destroy session from mints.cloud and delete local session cookie
52
- def mints_contact_logout
53
- # Logout from mints
54
- @mints_contact.logout
55
- # Delete local cookie
56
- cookies.delete(:mints_contact_session_token)
57
- cookies.delete(:mints_contact_id)
58
- @contact_token = nil
14
+ # Override default values for mints clients concern
15
+ def define_mints_clients
16
+ %w[ contact pub ]
59
17
  end
60
-
18
+
61
19
  private
62
20
 
63
21
  ##
@@ -76,63 +34,19 @@ module Mints
76
34
  puts "REQUEST IP ADDRESS: #{request['ip_address']}"
77
35
  puts "REQUEST REMOTE IP: #{request['remote_ip']}"
78
36
  end
37
+
79
38
  response = @mints_pub.register_visit(request)
80
- if @debug
81
- puts "RESPONSE IN REGISTER VISIT: #{response}"
82
- end
83
- @contact_token = response['contact_token'] ? response['contact_token'] : response['user_token']
39
+
40
+ puts "RESPONSE IN REGISTER VISIT: #{response}" if @debug
41
+
42
+ @contact_token = response['contact_token'] || response['user_token']
84
43
  @visit_id = response['visit_id']
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 }
90
- end
91
44
 
92
- ##
93
- # === Set mints pub.
94
- # Initialize the public client and set the contact token
95
- def set_mints_pub_client
96
- if File.exists?("#{Rails.root}/mints_config.yml.erb")
97
- template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
98
- config = YAML.load template.result(binding)
99
- @host = config["mints"]["host"]
100
- @api_key = config["mints"]["api_key"]
101
- @debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
102
- else
103
- raise 'MintsBadCredentialsError'
104
- end
105
- # Initialize mints pub client, credentials taken from mints_config.yml.erb file
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)
108
- # Set contact token from cookie
109
- @mints_pub.client.session_token = @contact_token
110
- end
45
+ puts "VISIT ID: #{@visit_id}" if @debug
111
46
 
112
- ##
113
- # === Set contact token.
114
- # Set contact token variable from the mints_contact_id cookie value
115
- def set_contact_token
116
- @contact_token = cookies[:mints_contact_id]
47
+ cookies.permanent[:mints_contact_id] = { value: @contact_token, secure: true, httponly: true }
48
+ cookies.permanent[:mints_visit_id] = { value: @visit_id, secure: true, httponly: true }
117
49
  end
118
50
 
119
- ##
120
- # === Set mints contact client.
121
- # Initialize the public client and set the contact token
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
132
- # Initialize mints clontact client
133
- session_token = cookies[:mints_contact_session_token] ? cookies[:mints_contact_session_token] : nil
134
- contact_token_id = cookies[:mints_contact_id] ? cookies[:mints_contact_id] : nil
135
- @mints_contact = Mints::Contact.new(@host, @api_key, session_token, contact_token_id, @debug)
136
- end
137
51
  end
138
52
  end
@@ -0,0 +1,74 @@
1
+ require_relative "./read_config_file.rb"
2
+
3
+ module MintsClients
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ include ReadConfigFile
8
+ before_action :set_mints_clients
9
+ end
10
+
11
+ # Define the clients that will have
12
+ # Override in the controller if you dont wanna all clients
13
+ def define_mints_clients
14
+ %w[ contact user pub service_account ]
15
+ end
16
+
17
+ private
18
+
19
+ ##
20
+ # === Set mints clients (pub, user and contact)
21
+ # Initialize all clients from mints
22
+ def set_mints_clients
23
+ clients = define_mints_clients
24
+
25
+ if @debug
26
+ puts "Clients to initialize:", clients
27
+ puts "Host:", @host
28
+ end
29
+
30
+ if clients.kind_of?(Array) and @host
31
+ clients.each do |client|
32
+ send("set_mints_#{client}_client")
33
+ end
34
+ end
35
+ end
36
+
37
+ ##
38
+ # === Set mints pub.
39
+ # Initialize the public client and set the contact token
40
+ def set_mints_pub_client
41
+ # Initialize mints pub client, credentials taken from mints_config.yml.erb file
42
+ visit_id = cookies[:mints_visit_id]
43
+ contact_token_id = cookies[:mints_contact_id]
44
+
45
+ @mints_pub = Mints::Pub.new(@host, @api_key, contact_token_id, visit_id, @debug)
46
+ end
47
+
48
+ ##
49
+ # === Set mints contact client.
50
+ # Initialize the contact client and set the contact token
51
+ def set_mints_contact_client
52
+ # Initialize mints contact client
53
+ contact_session_token = cookies[:mints_contact_session_token]
54
+ contact_token_id = cookies[:mints_contact_id]
55
+ @mints_contact = Mints::Contact.new(@host, @api_key, contact_session_token, contact_token_id, @debug)
56
+ end
57
+
58
+ ##
59
+ # === Set Mints user client.
60
+ # Initialize the user client
61
+ def set_mints_user_client
62
+ # Initialize mints user client
63
+ user_session_token = cookies[:mints_user_session_token]
64
+ @mints_user = Mints::User.new(@host, @api_key, user_session_token, @debug)
65
+ end
66
+
67
+ ##
68
+ # === Set Mints service account
69
+ # Initialize the service account client
70
+ def set_mints_service_account_client
71
+ # Initialize service account client
72
+ @mints_service_account = Mints::User.new(@host, @api_key, @api_key, @debug)
73
+ end
74
+ end
@@ -0,0 +1,28 @@
1
+ module ReadConfigFile
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :set_config_variables
6
+ end
7
+
8
+ def set_config_variables
9
+ if File.exists?("#{Rails.root}/mints_config.yml.erb")
10
+
11
+ template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
12
+ config = YAML.safe_load template.result(binding)
13
+ @host = config["mints"]["host"]
14
+ @api_key = config["mints"]["api_key"]
15
+ @debug = !!config["sdk"]["debug"]
16
+ @redis_config = config['redis_cache']
17
+ @use_cache = config['redis_cache']['use_cache']
18
+
19
+ if config['redis_cache']['use_cache']
20
+ @redis_server = Redis.new(
21
+ host: config['redis_cache']['redis_host'],
22
+ port: config['redis_cache']['redis_port'] || 6379,
23
+ db: config['redis_cache']['redis_db'] || 1
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,94 +1,15 @@
1
- require 'reverse_proxy/controller'
2
- require 'reverse_proxy/client'
1
+ require_relative "./concerns/read_config_file.rb"
2
+ require_relative "../helpers/proxy_controllers_methods.rb"
3
+
3
4
  module Mints
4
5
  class ContactAPIController < ActionController::API
5
6
  include AbstractController::Helpers
6
- include ActionController::Cookies
7
7
  include ReverseProxy::Controller
8
- before_action :set_config_variables
8
+ include ReadConfigFile
9
+ include ProxyControllersMethods
9
10
 
10
11
  def index
11
- headers = {
12
- 'host' => "#{@host.gsub('http://', '').gsub('https://', '')}",
13
- 'ApiKey' => "#{@api_key}",
14
- 'Content-Type'=> 'application/json',
15
- 'Accept'=> 'application/json'
16
- }
17
- # Set contact token id as header
18
- if cookies[:mints_contact_id]
19
- headers['ContactToken'] = cookies[:mints_contact_id]
20
- end
21
- # Set contact session token as header
22
- if cookies[:mints_contact_session_token]
23
- session_token = cookies[:mints_contact_session_token]
24
- headers["Authorization"] = "Bearer #{session_token}"
25
- end
26
-
27
- url_need_cache = false
28
- result_from_cache = false
29
- time = 0
30
- full_url = request.original_url
31
-
32
- if request.method == "GET"
33
- if @use_cache
34
- @redis_config['groups'].each do |group|
35
- group['urls'].each do |url|
36
- if full_url.match url
37
- time = group['time']
38
- url_need_cache = true
39
- break
40
- end
41
- end
42
- break if url_need_cache
43
- end
44
- end
45
- end
46
-
47
- if url_need_cache
48
- if @redis_server.get(full_url)
49
- response = @redis_server.get(full_url)
50
- result_from_cache = true
51
- render json: response
52
- else
53
- reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
54
- # Request succeded!
55
- config.on_response do |code, response|
56
- @redis_server.setex(full_url,time,response.body)
57
- end
58
- # Request failed!
59
- config.on_missing do |code, response|
60
- # We got a 404!
61
- if code == 404
62
- raise ActionController::RoutingError.new('Not Found')
63
- end
64
- end
65
- end
66
- end
67
- else
68
- reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
69
- # Request failed!
70
- config.on_missing do |code, response|
71
- # We got a 404!
72
- if code == 404
73
- raise ActionController::RoutingError.new('Not Found')
74
- end
75
- end
76
- end
77
- end
78
- end
79
-
80
- private
81
-
82
- def set_config_variables
83
- if File.exists?("#{Rails.root}/mints_config.yml.erb")
84
- template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
85
- config = YAML.load template.result(binding)
86
- @host = config["mints"]["host"]
87
- @api_key = config["mints"]["api_key"]
88
- @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']
89
- @redis_config = config['redis_cache']
90
- @use_cache = config['redis_cache']['use_cache']
91
- end
12
+ super('contact')
92
13
  end
93
14
  end
94
15
  end
@@ -1,85 +1,12 @@
1
- require 'reverse_proxy/controller'
2
- require 'reverse_proxy/client'
3
- require 'redis'
1
+ require_relative "./concerns/read_config_file.rb"
2
+ require_relative "../helpers/proxy_controllers_methods.rb"
3
+
4
4
  module Mints
5
5
  class PublicAPIController < ActionController::API
6
6
  include ReverseProxy::Controller
7
- before_action :set_config_variables
8
-
9
- def index
10
- headers = {
11
- 'host' => "#{@host.gsub('http://', '').gsub('https://', '')}",
12
- 'ApiKey' => "#{@api_key}",
13
- 'Content-Type'=> 'application/json',
14
- 'Accept'=> 'application/json'
15
- }
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
66
- end
67
- end
68
-
69
- end
70
-
71
- private
7
+ include AbstractController::Helpers
8
+ include ReadConfigFile
9
+ include ProxyControllersMethods
72
10
 
73
- def set_config_variables
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)
77
- @host = config["mints"]["host"]
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']
82
- end
83
- end
84
11
  end
85
12
  end