mints 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 74526a6438b7de0c89f91b1d7c14b8e34a77c139f911783cfe37437eb6c32b20
4
+ data.tar.gz: cef5273897e15545fe8a1f831fbbe1069cb1c4c2a14486221bdea94f3f70e6bb
5
+ SHA512:
6
+ metadata.gz: f7da303e2dd55c10fb990d17a49628a99d95c8a3e06985cecc4e80d23784abe4c131afd6a16ff4780d639813ccbb0c0e5c6f75732b294f5a4a8cc9f607378d44
7
+ data.tar.gz: f521865904561b3c73e82ae099ca39dfdd9af23e424faf9cb4a1e219676e8d5eeb43426a6a1623a7f44e4bac88dbcf9603a786953f9d56c7a0a0527c6f41fc29
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'json'
4
+ gem 'httparty'
5
+ gem 'addressable'
data/Readme.md ADDED
@@ -0,0 +1,32 @@
1
+ # Mints Ruby SDK
2
+
3
+ This is a library to connect apps built on ruby to Mints.Cloud
4
+
5
+ ## Installation
6
+
7
+ Add gem to the Gemfile
8
+ ```bash
9
+ gem 'mints', git: 'https://github.com/rubengomez/mints-ruby-sdk'
10
+ bundle install
11
+ ```
12
+
13
+ ## Usage
14
+ Using Mints Public API
15
+ ```ruby
16
+ pub = Mints::Pub.new(mints_url, api_key)
17
+ pub.get_stories
18
+ ```
19
+
20
+ Using Mints Contact API
21
+ ```ruby
22
+ con = Mints::Contact.new(mints_url, api_key)
23
+ con.login(email, password)
24
+ con.me
25
+ ```
26
+
27
+ Using Mints User API
28
+ ```ruby
29
+ con = Mints::User.new(mints_url, api_key)
30
+ con.login(email, password)
31
+ con.get_contacts
32
+ ```
data/lib/client.rb ADDED
@@ -0,0 +1,224 @@
1
+ require "httparty"
2
+ require "json"
3
+ require "addressable"
4
+ module Mints
5
+ class Client
6
+ attr_reader :host
7
+ attr_reader :api_key
8
+ attr_reader :scope
9
+ attr_reader :base_url
10
+ attr_accessor :session_token
11
+
12
+ def initialize(host, api_key, scope = nil, session_token = nil)
13
+ @host = host
14
+ @api_key = api_key
15
+ @session_token = session_token
16
+ self.set_scope(scope)
17
+ end
18
+
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
23
+ if action === 'get'
24
+ response = self.send("#{@scope}_#{action}", full_url)
25
+ elsif action === 'create' or action === 'post'
26
+ action = 'post'
27
+ response = self.send("#{@scope}_#{action}", full_url, data)
28
+ elsif action === 'put' or action === 'patch' or action ==='update'
29
+ action = 'put'
30
+ response = self.send("#{@scope}_#{action}", full_url, data)
31
+ end
32
+ if (response.response.code == "404")
33
+ raise 'NotFoundError'
34
+ end
35
+ parsed_response = JSON.parse(response.body)
36
+ if parsed_response.key?('data')
37
+ return parsed_response['data']
38
+ end
39
+ return parsed_response
40
+ end
41
+
42
+ def method_missing(name, *args, &block)
43
+ name.to_s.include?("__") ? separator = "__" : separator = "_"
44
+ # split the name to identify their elements
45
+ name_spplited = name.to_s.split(separator)
46
+ # count the elments
47
+ name_len = name_spplited.size
48
+ # the action always be the first element
49
+ action = name_spplited.first
50
+ raise 'NoActionError' unless ['get', 'create', 'post', 'update', 'put'].include?(action)
51
+ # the object always be the last element
52
+ object = separator == "__" ? name_spplited.last.gsub("_","-") : name_spplited.last
53
+ # get intermediate url elements
54
+ route_array = []
55
+ (name_len-1).times do |n|
56
+ if n == 0 or n == name_len-1
57
+ next
58
+ end
59
+ n = name_spplited[n]
60
+ self.replacements().each do |object|
61
+ n = n.gsub(object[:old_value], object[:new_value])
62
+ end
63
+ route_array.push n
64
+ end
65
+ 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'
80
+ response = self.send("#{@scope}_#{action}", url)
81
+ elsif action === 'post' or action === 'create'
82
+ action = 'post'
83
+ data = args[0]
84
+ response = self.send("#{@scope}_#{action}", url, {data: data})
85
+ elsif action === 'put' or action === 'update'
86
+ action = 'put'
87
+ id = args[0]
88
+ data = args[1]
89
+ response = self.send("#{@scope}_#{action}", "#{url}", data)
90
+ end
91
+ p url
92
+ if (response.response.code == "404")
93
+ raise 'NotFoundError'
94
+ end
95
+ return JSON.parse(response.body)
96
+ end
97
+
98
+ def replacements
99
+ return [
100
+ {old_value: '_', new_value: '-'},
101
+ {old_value: 'people', new_value: 'crm'},
102
+ {old_value: 'store', new_value: 'ecommerce'}
103
+ ]
104
+ end
105
+
106
+ def set_scope(scope)
107
+ @scope = scope
108
+ if scope == "public"
109
+ @base_url = "/api/v1"
110
+ elsif scope == "contact"
111
+ @base_url = "/api/v1"
112
+ elsif scope == "user"
113
+ @base_url = "/api/user/v1"
114
+ else
115
+ @scope = "public"
116
+ @base_url = "/api/v1"
117
+ end
118
+ end
119
+
120
+ ##### HTTTP CLIENTS ######
121
+ # Simple HTTP GET
122
+ def http_get(url, headers = nil)
123
+ return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
124
+ end
125
+
126
+ # Simple HTTP POST
127
+ def http_post(url, headers = nil, data = nil)
128
+ return headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data)
129
+ end
130
+
131
+ # Simple HTTP PUT
132
+ def http_put(url, headers = nil, data = nil)
133
+ return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
134
+ end
135
+
136
+ # Start contact context
137
+ def contact_get(url)
138
+ headers = {
139
+ "ApiKey" => @api_key,
140
+ "Accept" => "application/json"
141
+ }
142
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
143
+ return self.http_get(url, headers)
144
+ end
145
+
146
+ def contact_post(url, data)
147
+ headers = {
148
+ "ApiKey" => @api_key,
149
+ "Accept" => "application/json"
150
+ }
151
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
152
+ return self.http_post(url, headers, data)
153
+ end
154
+
155
+ def contact_put(url, data)
156
+ headers = {
157
+ "ApiKey" => @api_key,
158
+ "Accept" => "application/json"
159
+ }
160
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
161
+ return self.http_post(url, headers, data)
162
+ end
163
+
164
+ # Start User context
165
+ def user_get(url)
166
+ headers = {
167
+ "ApiKey" => @api_key,
168
+ "Accept" => "application/json"
169
+ }
170
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
171
+ return self.http_get(url, headers)
172
+ end
173
+
174
+ def user_post(url, data)
175
+ headers = {
176
+ "ApiKey" => @api_key,
177
+ "Accept" => "application/json"
178
+ }
179
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
180
+ return self.http_post(url, headers, data)
181
+ end
182
+
183
+ def user_put(url, data)
184
+ headers = {
185
+ "ApiKey" => @api_key,
186
+ "Accept" => "application/json",
187
+ "Contet-Type" => "application/json"
188
+ }
189
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
190
+ return self.http_put(url, headers, data)
191
+ end
192
+ # End User Context
193
+
194
+ def public_get(url, headers = nil)
195
+ h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
196
+ if headers
197
+ headers.each do |k,v|
198
+ h[k] = v
199
+ end
200
+ end
201
+ self.http_get(url, h)
202
+ end
203
+
204
+ def public_post(url, headers = nil, data)
205
+ h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
206
+ if headers
207
+ headers.each do |k,v|
208
+ h[k] = v
209
+ end
210
+ end
211
+ self.http_post(url, h, data)
212
+ end
213
+
214
+ def public_put(url, headers = nil, data)
215
+ h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
216
+ if headers
217
+ headers.each do |k,v|
218
+ h[k] = v
219
+ end
220
+ end
221
+ self.http_put(url, h, data)
222
+ end
223
+ end
224
+ end
data/lib/contact.rb ADDED
@@ -0,0 +1,78 @@
1
+ require_relative "./client.rb"
2
+ module Mints
3
+ class Contact
4
+ attr_reader :client
5
+ def initialize(host, api_key, session_token = nil)
6
+ @client = Mints::Client.new(host, api_key, "contact", session_token)
7
+ end
8
+
9
+ def register(given_name, last_name, email, password)
10
+ data = {
11
+ given_name: given_name,
12
+ last_name: last_name,
13
+ email: email,
14
+ password: password
15
+ }
16
+ return @client.raw("post", "/contacts/register", {data: data})
17
+ end
18
+
19
+ def login(email, password)
20
+ data = {
21
+ email: email,
22
+ password: password
23
+ }
24
+ response = @client.raw("post", "/contacts/login", {data: data})
25
+ if response.key? "session_token"
26
+ @client.session_token = response["session_token"]
27
+ end
28
+ return response
29
+ end
30
+
31
+ def logout
32
+ response = @client.raw("post", "/contacts/logout", nil) if session_token?
33
+ if response["success"]
34
+ @client.session_token = nil
35
+ end
36
+ return response
37
+ end
38
+
39
+ def change_password
40
+ return @client.raw("post", "/contacts/change-password", data)
41
+ end
42
+
43
+ def recover_password
44
+ return @client.raw("post", "/contacts/recover-password", data)
45
+ end
46
+
47
+ def reset_password
48
+ return @client.raw("post", "/contacts/reset-password", data)
49
+ end
50
+
51
+ def auth_login
52
+ return @client.raw("post", "/contacts/oauth-login", data)
53
+ end
54
+
55
+ def me
56
+ return @client.raw("get", "/contacts/me")
57
+ end
58
+
59
+ def status
60
+ return @client.raw("get", "/contacts/status")
61
+ end
62
+
63
+ def update
64
+ return @client.raw("put", "/contacts/update", data)
65
+ end
66
+
67
+ private
68
+
69
+ def session_token?
70
+ if @client.session_token
71
+ return true
72
+ else
73
+ raise "Unauthenticated"
74
+ return false
75
+ end
76
+ end
77
+ end
78
+ end
data/lib/mints.rb ADDED
@@ -0,0 +1,5 @@
1
+ require_relative "./pub.rb"
2
+ require_relative "./contact.rb"
3
+ require_relative "./user.rb"
4
+ module Mints
5
+ end
data/lib/pub.rb ADDED
@@ -0,0 +1,50 @@
1
+ require_relative './client.rb'
2
+ module Mints
3
+ class Pub
4
+ attr_reader :client
5
+ def initialize(host, api_key)
6
+ @client = Mints::Client.new(host, api_key)
7
+ end
8
+
9
+ def register_visit(ip, user_agent, url)
10
+ data = {
11
+ ip_address: ip,
12
+ user_agent: user_agent,
13
+ url: url
14
+ }
15
+ return @client.raw("post", "/register-visit-timer", data)
16
+ end
17
+
18
+ def register_visit_timer
19
+ return @client.raw("get", "/register-visit-timer")
20
+ end
21
+
22
+ def get_content_page
23
+ return @client.raw("get", "/content-pages/#{slug}")
24
+ end
25
+
26
+ def get_content_template
27
+ return @client.raw("get", "/content/content-templates/#{slug}")
28
+ end
29
+
30
+ def content_instance
31
+ return @client.raw("get", "/content/content-instances/#{slug}")
32
+ end
33
+
34
+ def get_stories
35
+ return @client.raw("get", "/content/stories")
36
+ end
37
+
38
+ def get_story(slug)
39
+ return @client.raw("get", "/content/stories/#{slug}")
40
+ end
41
+
42
+ def get_form
43
+ return @client.raw("get", "/content/forms/{slug}")
44
+ end
45
+
46
+ def submit_form
47
+ return @client.raw("post", "/forms/store", data)
48
+ end
49
+ end
50
+ end
data/lib/user.rb ADDED
@@ -0,0 +1,175 @@
1
+ require_relative './client.rb'
2
+ module Mints
3
+ class User
4
+ attr_reader :client
5
+ def initialize(host, api_key, session_token = nil)
6
+ @client = Mints::Client.new(host, api_key, 'user', session_token)
7
+ end
8
+
9
+ def login(email, password)
10
+ data = {
11
+ email: email,
12
+ password: password,
13
+ }
14
+ response = @client.raw("post", "/users/login", data, '/api/v1')
15
+ if response.key? "api_token"
16
+ @client.session_token = response["api_token"]
17
+ end
18
+ return response
19
+ end
20
+ ######################################### CRM #########################################
21
+ ### Contacts ###
22
+ def get_contacts
23
+ return @client.get__crm__contacts
24
+ end
25
+
26
+ def get_contact(id)
27
+ return @client.get__crm__contacts(id)
28
+ end
29
+
30
+ def create_contact(data)
31
+ return @client.create__crm__contacts(data)
32
+ end
33
+
34
+ def update_contact(id, data)
35
+ return @client.update__crm__contacts(id, data)
36
+ end
37
+ ### Companies ###
38
+ def get_companies
39
+ return @client.get__crm__companies
40
+ end
41
+
42
+ def get_company(id)
43
+ return @client.get__crm__companies(id)
44
+ end
45
+
46
+ def create_company(data)
47
+ return @client.create__crm__companies(data)
48
+ end
49
+
50
+ def update_company(id, data)
51
+ return @client.update__crm__companies(id, data)
52
+ end
53
+
54
+ ### Deals ###
55
+ def get_deals
56
+ return @client.get__crm__deals
57
+ end
58
+
59
+ def get_deal(id)
60
+ return @client.get__crm__deals(id)
61
+ end
62
+
63
+ def create_deal(data)
64
+ return @client.create__crm__deals(data)
65
+ end
66
+
67
+ def update_deal(id, data)
68
+ return @client.update__crm__deals(id, data)
69
+ end
70
+
71
+ ######################################### Content #########################################
72
+ ### Stories ###
73
+ def get_stories
74
+ return @client.get__content__stories
75
+ end
76
+
77
+ def get_story(id)
78
+ return @client.get__content__stories(id)
79
+ end
80
+
81
+ def create_story(data)
82
+ return @client.create__content__stories(data)
83
+ end
84
+
85
+ def update_story(id, data)
86
+ return @client.update__content__stories(id, data)
87
+ end
88
+
89
+ ### Content instances ###
90
+ def get_content_instances
91
+ return @client.get__content__instances
92
+ end
93
+
94
+ def get_content_instance(id)
95
+ return @client.get__content__instances(id)
96
+ end
97
+
98
+ def create_content_instance(data)
99
+ return @client.create__content__instances(data)
100
+ end
101
+
102
+ def update_content_instance(id, data)
103
+ return @client.update__content__instances(id, data)
104
+ end
105
+
106
+ ### Content pages ###
107
+ def get_content_pages
108
+ return @client.get__content__pages
109
+ end
110
+
111
+ def get_content_page(id)
112
+ return @client.get__content__pages(id)
113
+ end
114
+
115
+ def create_content_page(data)
116
+ return @client.create__content__pages(data)
117
+ end
118
+
119
+ def update_content_page(id, data)
120
+ return @client.update__content__pages(id, data)
121
+ end
122
+
123
+ ### Content templates ###
124
+ def get_content_templates
125
+ return @client.get__content__templates
126
+ end
127
+
128
+ def get_content_page(id)
129
+ return @client.get__content__templates(id)
130
+ end
131
+
132
+ def create_content_page(data)
133
+ return @client.create__content__templates(data)
134
+ end
135
+
136
+ def update_content_page(id, data)
137
+ return @client.update__content__templates(id, data)
138
+ end
139
+
140
+ ######################################### Ecommerce #########################################
141
+ ### Products ###
142
+ def get_products
143
+ return @client.get__ecommerce__products
144
+ end
145
+
146
+ def get_product(id)
147
+ return @client.get__ecommerce__products(id)
148
+ end
149
+
150
+ def create_product(data)
151
+ return @client.create__ecommerce__products(data)
152
+ end
153
+
154
+ def update_product(id, data)
155
+ return @client.update__ecommerce__products(id, data)
156
+ end
157
+
158
+ ### Locations ###
159
+ def get_locations
160
+ return @client.get__ecommerce__locations
161
+ end
162
+
163
+ def get_location(id)
164
+ return @client.get__ecommerce__locations(id)
165
+ end
166
+
167
+ def create_location(data)
168
+ return @client.create__ecommerce__locations(data)
169
+ end
170
+
171
+ def update_location(id, data)
172
+ return @client.update__ecommerce__locations(id, data)
173
+ end
174
+ end
175
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mints
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "'Ruben Gomez Garcia'"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.3
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.8.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 1.8.3
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.8.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: httparty
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.18.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.18.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 0.18.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.18.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: addressable
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 2.7.0
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.7.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 2.7.0
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.7.0
73
+ description:
74
+ email:
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - Gemfile
80
+ - Readme.md
81
+ - lib/client.rb
82
+ - lib/contact.rb
83
+ - lib/mints.rb
84
+ - lib/pub.rb
85
+ - lib/user.rb
86
+ homepage: https://github.com/rubengomez/mints-ruby-sdk
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.6.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: MINTS gem allows to connect your Rails App to MINTS.CLOUD
109
+ test_files: []