mints 0.0.2 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ require 'reverse_proxy/controller'
2
+ require 'reverse_proxy/client'
3
+ module Mints
4
+ class ContactAPIController < ActionController::API
5
+ include AbstractController::Helpers
6
+ include ActionController::Cookies
7
+ include ReverseProxy::Controller
8
+ before_action :set_config_variables
9
+
10
+ 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
+ if cookies[:mints_contact_session_token]
18
+ session_token = cookies[:mints_contact_session_token]
19
+ headers["Authorization"] = "Bearer #{session_token}"
20
+ end
21
+ reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
22
+ # We got a 404!
23
+ config.on_missing do |code, response|
24
+ raise ActionController::RoutingError.new('Not Found')
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def set_config_variables
32
+ if File.exists?("#{Rails.root}/mints_config.yml")
33
+ config = YAML.load_file("#{Rails.root}/mints_config.yml")
34
+ @host = config["mints"]["host"]
35
+ @api_key = config["mints"]["api_key"]
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,33 @@
1
+ require 'reverse_proxy/controller'
2
+ require 'reverse_proxy/client'
3
+ module Mints
4
+ class PublicAPIController < ActionController::API
5
+ include ReverseProxy::Controller
6
+ before_action :set_config_variables
7
+
8
+ def index
9
+ headers = {
10
+ 'host' => "#{@host.gsub('http://', '').gsub('https://', '')}",
11
+ 'ApiKey' => "#{@api_key}",
12
+ 'Content-Type'=> 'application/json',
13
+ 'Accept'=> 'application/json'
14
+ }
15
+ reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
16
+ # We got a 404!
17
+ config.on_missing do |code, response|
18
+ raise ActionController::RoutingError.new('Not Found')
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def set_config_variables
26
+ if File.exists?("#{Rails.root}/mints_config.yml")
27
+ config = YAML.load_file("#{Rails.root}/mints_config.yml")
28
+ @host = config["mints"]["host"]
29
+ @api_key = config["mints"]["api_key"]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ require 'reverse_proxy/controller'
2
+ require 'reverse_proxy/client'
3
+ module Mints
4
+ class UserAPIController < ActionController::API
5
+ include AbstractController::Helpers
6
+ include ActionController::Cookies
7
+ include ReverseProxy::Controller
8
+ before_action :set_config_variables
9
+
10
+ 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
+ if cookies[:mints_contact_session_token]
18
+ session_token = cookies[:mints_user_session_token]
19
+ headers["Authorization"] = "Bearer #{session_token}"
20
+ end
21
+ reverse_proxy "#{@host}", headers: headers, verify_ssl: false do |config|
22
+ # We got a 404!
23
+ config.on_missing do |code, response|
24
+ raise ActionController::RoutingError.new('Not Found')
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def set_config_variables
32
+ if File.exists?("#{Rails.root}/mints_config.yml")
33
+ config = YAML.load_file("#{Rails.root}/mints_config.yml")
34
+ @host = config["mints"]["host"]
35
+ @api_key = config["mints"]["api_key"]
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/pub.rb CHANGED
@@ -1,107 +1,320 @@
1
+ require 'yaml'
1
2
  require_relative './client.rb'
2
3
  module Mints
4
+ ##
5
+ # == Public context API
6
+ # Pub class contains functions that needs only an API key as authentication
7
+ # == Usage example
8
+ # Initialize
9
+ # pub = Mints::Pub.new(mints_url, api_key)
10
+ # or if host and api_key are provided by mints_config.yml
11
+ # pub = Mints::Pub.new
12
+ # Call any function
13
+ # pub.get_products
14
+ # == Single resource options
15
+ # * +include+ - [String] include a relationship
16
+ # * +attributes+ - [Boolean] attach attributes to response
17
+ # * +categories+ - [Boolean] attach categories to response
18
+ # * +tags+ - [Boolean] attach tags to response
19
+ # == Resource collections options
20
+ # * +search+ - [String] filter by a search word
21
+ # * +scopes+ - [String] filter by a scope
22
+ # * +filters+ - [String] filter by where clauses
23
+ # * +jfilters+ - [String] filter using complex condition objects
24
+ # * +catfilters+ - [String] filter by categories
25
+ # * +fields+ - [String] indicates the columns that will be selected
26
+ # * +sort+ - [String] indicates the columns that will be selected
27
+ # * +include+ - [String] include a relationship
28
+ # * +attributes+ - [Boolean] attach attributes to response
29
+ # * +categories+ - [Boolean] attach categories to response
30
+ # * +taxonomies+ - [Boolean] attach categories to response
31
+ # * +tags+ - [Boolean] attach tags to response
3
32
  class Pub
4
33
  attr_reader :client
5
- def initialize(host, api_key)
6
- @client = Mints::Client.new(host, api_key)
34
+
35
+ ##
36
+ # === Initialize.
37
+ # Class constructor
38
+ #
39
+ # ==== Parameters
40
+ # * +host+ - [String] It's the visitor IP
41
+ # * +api_key+ - [String] Mints instance api key
42
+ # * +contact_token+ - [String] Cookie 'mints_contact_id' value (mints_contact_token)
43
+ # ==== Return
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)
7
47
  end
8
48
 
9
- def register_visit(ip, user_agent, url)
49
+ ##
50
+ # === Register Visit.
51
+ # Register a ghost/contact visit in Mints.Cloud
52
+ #
53
+ # ==== Parameters
54
+ # * +request+ - [ActionDispatch::Request] request
55
+ # * +ip+ - [String] It's the visitor IP
56
+ # * +user_agent+ - The visitor's browser user agent
57
+ # * +url+ - [String] URL visited
58
+ def register_visit(request, ip = nil, user_agent = nil, url = nil)
10
59
  data = {
11
- ip_address: ip,
12
- user_agent: user_agent,
13
- url: url
60
+ ip_address: ip || request.remote_ip,
61
+ user_agent: user_agent || request.user_agent,
62
+ url: url || request.fullpath
14
63
  }
15
- return @client.raw("post", "/register-visit-timer", nil, data)
64
+ response = @client.raw("post", "/register-visit", nil, data)
65
+ return response
16
66
  end
17
67
 
18
- def register_visit_timer
19
- return @client.raw("get", "/register-visit-timer")
68
+ ##
69
+ # === Register Visit timer.
70
+ # Register a page visit time
71
+ #
72
+ # ==== Parameters
73
+ # * +visit+ - [String] It's the visitor IP
74
+ # * +time+ - [Integer] The visitor's browser user agent
75
+ def register_visit_timer(visit, time)
76
+ return @client.raw("get", "/register-visit-timer?visit=#{visit}&time=#{time}")
20
77
  end
21
78
 
79
+ ##
80
+ # === Get Content Page.
81
+ # Get a single content page
82
+ #
83
+ # ==== Parameters
84
+ # * +slug+ - [String] It's the slug
85
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
22
86
  def get_content_page(slug, options = nil)
23
- return @client.raw("get", "/content-pages/#{slug}", options)
87
+ return @client.raw("get", "/content/content-pages/#{slug}", options)
24
88
  end
25
89
 
90
+ ##
91
+ # === Get Content Templates.
92
+ # Get a collection of content templates
93
+ #
94
+ # ==== Parameters
95
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
26
96
  def get_content_templates(options = nil)
27
- return @client.raw("get", "/content/content-templates")
97
+ return @client.raw("get", "/content/content-templates", options)
28
98
  end
29
99
 
100
+ ##
101
+ # === Get Content Template.
102
+ # Get a single content template.
103
+ #
104
+ # ==== Parameters
105
+ # * +slug+ - [String] It's the string identifier generated by Mints
106
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
30
107
  def get_content_template(slug, options = nil)
31
108
  return @client.raw("get", "/content/content-templates/#{slug}", options)
32
109
  end
33
110
 
34
- def content_instances(options)
111
+ ##
112
+ # === Get Content Instances.
113
+ # Get a collection of content instances
114
+ #
115
+ # ==== Parameters
116
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
117
+ def get_content_instances(options)
35
118
  return @client.raw("get", "/content/content-instances", options)
36
119
  end
37
120
 
38
- def content_instance(slug, options = nil)
121
+ ##
122
+ # === Get Content Instance.
123
+ # Get a single content instance.
124
+ #
125
+ # ==== Parameters
126
+ # * +slug+ - [String] It's the string identifier generated by Mints
127
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
128
+ def get_content_instance(slug, options = nil)
39
129
  return @client.raw("get", "/content/content-instances/#{slug}", options)
40
130
  end
41
131
 
132
+ ##
133
+ # === Get Stories.
134
+ # Get a collection of stories
135
+ #
136
+ # ==== Parameters
137
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
42
138
  def get_stories(options = nil)
43
139
  return @client.raw("get", "/content/stories", options)
44
140
  end
45
141
 
142
+ ##
143
+ # === Get Story.
144
+ # Get a single story.
145
+ #
146
+ # ==== Parameters
147
+ # * +slug+ - [String] It's the string identifier generated by Mints
148
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
46
149
  def get_story(slug, options = nil)
47
-
48
150
  return @client.raw("get", "/content/stories/#{slug}", options)
49
151
  end
50
152
 
153
+ ##
154
+ # === Get Forms.
155
+ # Get a collection of forms
156
+ #
157
+ # ==== Parameters
158
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
51
159
  def get_forms(options = nil)
52
- return @client.raw("get", "/content/forms/{slug}", options)
160
+ return @client.raw("get", "/content/forms", options)
53
161
  end
54
162
 
163
+ ##
164
+ # === Get Form.
165
+ # Get a single form.
166
+ #
167
+ # ==== Parameters
168
+ # * +slug+ - [String] It's the string identifier generated by Mints
169
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
55
170
  def get_form(slug, options = nil)
56
- return @client.raw("get", "/content/forms/{slug}", options)
171
+ return @client.raw("get", "/content/forms/#{slug}", options)
57
172
  end
58
173
 
174
+ ##
175
+ # === Submit Form.
176
+ # Submit a form.
177
+ #
178
+ # ==== Parameters
179
+ # * +data+ - [Hash] Data to be submited
59
180
  def submit_form(data)
60
181
  return @client.raw("post", "/forms/store", nil, data)
61
182
  end
62
183
 
184
+ ##
185
+ # === Get Products.
186
+ # Get a collection of products.
187
+ #
188
+ # ==== Parameters
189
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
63
190
  def get_products(options = nil)
64
191
  return @client.raw("get", "/ecommerce/products", options)
65
192
  end
66
193
 
194
+ ##
195
+ # === Get Product.
196
+ # Get a single product.
197
+ #
198
+ # ==== Parameters
199
+ # * +slug+ - [String] It's the string identifier generated by Mints
200
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
67
201
  def get_product(slug, options = nil)
68
202
  return @client.raw("get", "/ecommerce/products/#{slug}", options)
69
203
  end
70
204
 
205
+ ##
206
+ # === Get Product Brands.
207
+ # Get a collection of product brands.
208
+ #
209
+ # ==== Parameters
210
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
71
211
  def get_product_brands(options = nil)
72
212
  return @client.raw("get", "/ecommerce/product-brands", options)
73
213
  end
74
214
 
215
+ ##
216
+ # === Get Product Brand.
217
+ # Get a product brand.
218
+ #
219
+ # ==== Parameters
220
+ # * +slug+ - [String] It's the string identifier generated by Mints
221
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
75
222
  def get_product_brand(slug, options = nil)
76
223
  return @client.raw("get", "/ecommerce/product-brands/#{slug}", options)
77
224
  end
78
225
 
226
+ ##
227
+ # === Get SKUs.
228
+ # Get a collection of SKUs.
229
+ #
230
+ # ==== Parameters
231
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
79
232
  def get_skus(options = nil)
80
233
  return @client.raw("get", "/ecommerce/skus", options)
81
234
  end
82
235
 
236
+ ##
237
+ # === Get SKU.
238
+ # Get a single SKU.
239
+ #
240
+ # ==== Parameters
241
+ # * +slug+ - [String] It's the string identifier generated by Mints
242
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
83
243
  def get_sku(slug, options = nil)
84
244
  return @client.raw("get", "/ecommerce/skus/#{slug}", options)
85
245
  end
86
246
 
247
+ ##
248
+ # === Get categories.
249
+ # Get a collection of categories.
250
+ #
251
+ # ==== Parameters
252
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
87
253
  def get_categories(options = nil)
88
254
  return @client.raw("get", "/config/categories", options)
89
255
  end
90
256
 
257
+ ##
258
+ # === Get Category.
259
+ # Get a single category
260
+ #
261
+ # ==== Parameters
262
+ # * +slug+ - [String] It's the string identifier generated by Mints
263
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
91
264
  def get_category(slug, options = nil)
92
265
  return @client.raw("get", "/config/categories/#{slug}", options)
93
266
  end
94
267
 
95
- def get_tags(options)
268
+ ##
269
+ # === Get Tags.
270
+ # Get a collection of tags.
271
+ #
272
+ # ==== Parameters
273
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
274
+ def get_tags(options = nil)
96
275
  return @client.raw("get", "/config/tags", options)
97
276
  end
98
277
 
278
+ ##
279
+ # === Get Tag.
280
+ # Get a single tag
281
+ #
282
+ # ==== Parameters
283
+ # * +slug+ - [String] It's the string identifier generated by Mints
284
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
99
285
  def get_tag(slug, options = nil)
100
286
  return @client.raw("get", "/config/tags/#{slug}", options)
101
287
  end
102
288
 
289
+ ##
290
+ # === Get Attributes.
291
+ # Get a collection of attributes.
292
+ #
293
+ # ==== Parameters
294
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
103
295
  def get_attributes(options = nil)
104
296
  return @client.raw("get", "/config/attributes", options)
105
297
  end
298
+
299
+ ##
300
+ # === Get Taxonomies.
301
+ # Get a collection of taxonomies.
302
+ #
303
+ # ==== Parameters
304
+ # * +options+ - [Hash] List of {Resource collection Options}[#class-Mints::Pub-label-Resource+collections+options+] shown above can be used as parameter
305
+ def get_taxonomies(options = nil)
306
+ return @client.raw("get", "/config/taxonomies", options)
307
+ end
308
+
309
+ ##
310
+ # === Get Taxonomy.
311
+ # Get a single taxonomy
312
+ #
313
+ # ==== Parameters
314
+ # * +slug+ - [String] It's the string identifier generated by Mints
315
+ # * +options+ - [Hash] List of {Single Resource Options}[#class-Mints::Pub-label-Single+resource+options] shown above can be used as parameter
316
+ def get_taxonomy(slug, options = nil)
317
+ return @client.raw("get", "/config/taxonomies/#{slug}", options)
318
+ end
106
319
  end
107
320
  end