fomo 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/fomo.rb +313 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fa77a90b834d57f936318a590dd136649f6b6a70
4
+ data.tar.gz: c7416a9c06ef302d85eeef28b5428329bbb95608
5
+ SHA512:
6
+ metadata.gz: befde76e5fc2ee61086a68f6a713d9802967d1fc1efad93360c666d47f934b785c0b711aa32deed8b97422498b0828ee71cda654301a3492ffba877ed547a892
7
+ data.tar.gz: 591e388f9a4c0aec15ec12aae375774793a6d21fae557effa2852e984c498bdcf1dfbe3b9a74f101bb27dbff37e8301af2698ec798e6f7e0e132f53e27feb802
data/lib/fomo.rb ADDED
@@ -0,0 +1,313 @@
1
+ # Copyright (c) 2016. Fomo. https://www.usefomo.com
2
+ #
3
+ # Author:: Fomo (mailto:hello@usefomo.com)
4
+ # Copyright:: Copyright (c) 2016. Fomo. https://www.usefomo.com
5
+ # License:: MIT
6
+
7
+ require 'net/http'
8
+ require 'json'
9
+
10
+ # Fomo Client is wrapper around official Fomo API (https://www.usefomo.com)
11
+ class Fomo
12
+
13
+ # Fomo Authorization token
14
+ attr_accessor :auth_token
15
+
16
+ # Fomo API Endpoint
17
+ attr_accessor :endpoint
18
+
19
+ # Initializes Fomo Client with authorization token
20
+ #
21
+ # Arguments:
22
+ # auth_token: (String) Fomo Authorization token
23
+ #
24
+ def initialize (auth_token)
25
+ @auth_token = auth_token
26
+ @endpoint = 'https://www.usefomo.com'
27
+ end
28
+
29
+ # Get event
30
+ #
31
+ # Arguments:
32
+ # id: (String) Event ID
33
+ #
34
+ # Returns an FomoEvent object.
35
+ #
36
+ def get_event(id)
37
+ response = make_request('/api/v1/applications/me/events/' + id.to_s, 'GET')
38
+ j = JSON.parse(response)
39
+ FomoEvent.new(j['id'], j['created_at'], j['updated_at'], j['message'], j['link'], j['event_type_id'], j['url'], j['first_name'], j['city'], j['province'], j['country'], j['title'], j['image_url'], j['custom_event_fields_attributes'])
40
+ end
41
+
42
+ # Get events
43
+ #
44
+ # Returns an array of FomoEvent objects.
45
+ #
46
+ def get_events
47
+ response = make_request('/api/v1/applications/me/events', 'GET')
48
+ data = JSON.parse(response)
49
+ list = []
50
+ data.each do |j|
51
+ list.push(FomoEvent.new(j['id'], j['created_at'], j['updated_at'], j['message'], j['link'], j['event_type_id'], j['url'], j['first_name'], j['city'], j['province'], j['country'], j['title'], j['image_url'], j['custom_event_fields_attributes']))
52
+ end
53
+ list
54
+ end
55
+
56
+ # Create event
57
+ #
58
+ # Arguments:
59
+ # event: (FomoEventBasic) Fomo event
60
+ #
61
+ # Returns an FomoEvent object.
62
+ #
63
+ def create_event(event)
64
+ response = make_request('/api/v1/applications/me/events', 'POST', event)
65
+ j = JSON.parse(response)
66
+ FomoEvent.new(j['id'], j['created_at'], j['updated_at'], j['message'], j['link'], j['event_type_id'], j['url'], j['first_name'], j['city'], j['province'], j['country'], j['title'], j['image_url'], j['custom_event_fields_attributes'])
67
+ end
68
+
69
+ # Delete event
70
+ #
71
+ # Arguments:
72
+ # id: (String) Event ID
73
+ #
74
+ # Returns an FomoDeleteMessageResponse object.
75
+ #
76
+ def delete_event(id)
77
+ response = make_request('/api/v1/applications/me/events/' + id.to_s, 'DELETE')
78
+ j = JSON.parse(response)
79
+ FomoDeleteMessageResponse.new(j['message'])
80
+ end
81
+
82
+ # Update event
83
+ #
84
+ # Arguments:
85
+ # event: (FomoEvent) Fomo event
86
+ #
87
+ # Returns an FomoEvent object.
88
+ #
89
+ def update_event(event)
90
+ response = make_request('/api/v1/applications/me/events/' + event.id.to_s, 'PATCH', event)
91
+ j = JSON.parse(response)
92
+ FomoEvent.new(j['id'], j['created_at'], j['updated_at'], j['message'], j['link'], j['event_type_id'], j['url'], j['first_name'], j['city'], j['province'], j['country'], j['title'], j['image_url'], j['custom_event_fields_attributes'])
93
+ end
94
+
95
+ # Make authorized request to Fomo API
96
+ #
97
+ # Arguments:
98
+ # api_path: (String) API path
99
+ # method: (String) HTTP method
100
+ # data: (Object) Object to send, object is JSON serialized before it is sent
101
+ #
102
+ # Returns an JSON string.
103
+ #
104
+ def make_request(api_path, method, data = nil)
105
+ # puts(method + ' ' + @endpoint + api_path)
106
+ uri = URI.parse(@endpoint + api_path)
107
+ http = Net::HTTP.new(uri.host, uri.port)
108
+ http.use_ssl = true
109
+ headers = {
110
+ 'Authorization' => 'Token ' + @auth_token
111
+ }
112
+ case method
113
+ when 'GET'
114
+ request = Net::HTTP::Get.new(uri.path, initheader=headers)
115
+ response = http.request(request)
116
+ return response.body
117
+ when 'POST'
118
+ headers['Content-Type'] = 'application/json'
119
+ request = Net::HTTP::Post.new(uri.path, initheader=headers)
120
+ request.body = data.to_json
121
+ response = http.request(request)
122
+ return response.body
123
+ when 'PATCH'
124
+ headers['Content-Type'] = 'application/json'
125
+ request = Net::HTTP::Patch.new(uri.path, initheader=headers)
126
+ request.body = data.to_json
127
+ response = http.request(request)
128
+ return response.body
129
+ when 'DELETE'
130
+ request = Net::HTTP::Delete.new(uri.path, initheader=headers)
131
+ response = http.request(request)
132
+ return response.body
133
+ else
134
+ puts('Unknown method')
135
+ end
136
+ end
137
+ end
138
+
139
+ # This class holds attributes of basic event, object is needed when creating new event
140
+ class FomoEventBasic
141
+
142
+ # Event type unique ID (required)
143
+ attr_accessor :event_type_id
144
+
145
+ # Url to redirect on the event click. Size range: 0..255 (required)
146
+ attr_accessor :url
147
+
148
+ # First name of the person on the event. Size range: 0..255
149
+ attr_accessor :first_name
150
+
151
+ # City where the event happened. Size range: 0..255
152
+ attr_accessor :city
153
+
154
+ # Province where the event happened. Size range: 0..255
155
+ attr_accessor :province
156
+
157
+ # Country where the event happened ISO-2 standard. Size range: 0..255
158
+ attr_accessor :country
159
+
160
+ # Title of the event. Size range: 0..255
161
+ attr_accessor :title
162
+
163
+ # Url of the image to be displayed. Size range: 0..255
164
+ attr_accessor :image_url
165
+
166
+ # Array to create custom event fields
167
+ attr_accessor :custom_event_fields_attributes
168
+
169
+ # Initializes FomoEventBasic object
170
+ def initialize(event_type_id='', url='', first_name='', city='', province='', country='', title='', image_url='', custom_event_fields_attributes = [])
171
+ @event_type_id = event_type_id
172
+ @url = url
173
+ @first_name = first_name
174
+ @city = city
175
+ @province = province
176
+ @country = country
177
+ @title = title
178
+ @image_url = image_url
179
+ @custom_event_fields_attributes = custom_event_fields_attributes
180
+ end
181
+
182
+ # Add custom event field
183
+ #
184
+ # Arguments:
185
+ # key: Custom attribute key
186
+ # value: Custom attribute value
187
+ # id: Custom attribute ID
188
+ #
189
+ def add_custom_event_field(key, value, id='')
190
+ if id == ''
191
+ @custom_event_fields_attributes.push({'key' => key, 'value' => value})
192
+ else
193
+ @custom_event_fields_attributes.push({'key' => key, 'value' => value, 'id' => id})
194
+ end
195
+ end
196
+
197
+ # Return JSON serialized object
198
+ def to_json
199
+ hash = {}
200
+ self.instance_variables.each do |var|
201
+ hash[var.to_s.sub(/^@/, '')] = self.instance_variable_get var
202
+ end
203
+ '{"event":' + hash.to_json + '}'
204
+ end
205
+ end
206
+
207
+ # This class holds attributes of event, object returned by API
208
+ class FomoEvent
209
+ # Event ID
210
+ attr_accessor :id
211
+
212
+ # Created timestamp
213
+ attr_accessor :created_at
214
+
215
+ # Updated timestamp
216
+ attr_accessor :updated_at
217
+
218
+ # Message template
219
+ attr_accessor :message
220
+
221
+ # Full link
222
+ attr_accessor :link
223
+
224
+ # Event type unique ID (required)
225
+ attr_accessor :event_type_id
226
+
227
+ # Url to redirect on the event click. Size range: 0..255 (required)
228
+ attr_accessor :url
229
+
230
+ # First name of the person on the event. Size range: 0..255
231
+ attr_accessor :first_name
232
+
233
+ # City where the event happened. Size range: 0..255
234
+ attr_accessor :city
235
+
236
+ # Province where the event happened. Size range: 0..255
237
+ attr_accessor :province
238
+
239
+ # Country where the event happened ISO-2 standard. Size range: 0..255
240
+ attr_accessor :country
241
+
242
+ # Title of the event. Size range: 0..255
243
+ attr_accessor :title
244
+
245
+ # Url of the image to be displayed. Size range: 0..255
246
+ attr_accessor :image_url
247
+
248
+ # Array to create custom event fields
249
+ attr_accessor :custom_event_fields_attributes
250
+
251
+ # Initializes FomoEvent object
252
+ def initialize(id='', created_at='', updated_at='', message='', link='', event_type_id='', url='', first_name='', city='', province='', country='', title='', image_url='', custom_event_fields_attributes = [])
253
+ @id = id
254
+ @created_at = created_at
255
+ @updated_at = updated_at
256
+ @message = message
257
+ @link = link
258
+ @event_type_id = event_type_id
259
+ @url = url
260
+ @first_name = first_name
261
+ @city = city
262
+ @province = province
263
+ @country = country
264
+ @title = title
265
+ @image_url = image_url
266
+ @custom_event_fields_attributes = custom_event_fields_attributes
267
+ end
268
+
269
+ # Add custom event field
270
+ #
271
+ # Arguments:
272
+ # key: Custom attribute key
273
+ # value: Custom attribute value
274
+ # id: Custom attribute ID
275
+ #
276
+ def add_custom_event_field(key, value, id='')
277
+ if id == ''
278
+ @custom_event_fields_attributes.push({'key' => key, 'value' => value})
279
+ else
280
+ @custom_event_fields_attributes.push({'key' => key, 'value' => value, 'id' => id})
281
+ end
282
+ end
283
+
284
+ # Return JSON serialized object
285
+ def to_json
286
+ hash = {}
287
+ self.instance_variables.each do |var|
288
+ hash[var.to_s.sub(/^@/, '')] = self.instance_variable_get var
289
+ end
290
+ '{"event":' + hash.to_json + '}'
291
+ end
292
+ end
293
+
294
+ # This class holds attributes of Fomo delete response, object returned by API
295
+ class FomoDeleteMessageResponse
296
+
297
+ # Message
298
+ attr_accessor :message
299
+
300
+ # Initializes FomoDeleteMessageResponse object
301
+ def initialize(message='')
302
+ @message = message
303
+ end
304
+
305
+ # Return JSON serialized object
306
+ def to_json
307
+ hash = {}
308
+ self.instance_variables.each do |var|
309
+ hash[var.to_s.sub(/^@/, '')] = self.instance_variable_get var
310
+ end
311
+ hash.to_json
312
+ end
313
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fomo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fomo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Fomo Ruby SDK is the official SDK wrapper for the Fomo API service
14
+ email:
15
+ - hello@usefomo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/fomo.rb
21
+ homepage: https://github.com/usefomo/fomo-ruby-sdk
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Fomo
45
+ test_files: []