smsgateway 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 (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/api.rb +41 -0
  3. data/lib/smsgateway.rb +404 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 530cebff2b9dd33dc014c50b1b9078e69666f7adb8fe076a12f10f0de89a062d
4
+ data.tar.gz: 41787fc5cb6525e4576b16834947d5310f005f0542e35760d1aacdac5a2b4fbe
5
+ SHA512:
6
+ metadata.gz: '097b80fcf075331938cc95059bbca6d96979f8d4cc606d2abdb48d21ad3ce98404dd9cf72d2a647af5a6e4b15a3e7166cb2e7f10ecfb9b23600e77e1c90b6b27'
7
+ data.tar.gz: 7e726d0369babf1fc995f2b8cc126ab6d94817ab9037b1eaf94be814508f2ceed0f68cab3d84ecd3540cb75a8503526c814d9e875d5d5c0fe8d28593a047cde8
data/lib/api.rb ADDED
@@ -0,0 +1,41 @@
1
+ require "uri"
2
+ require "net/http"
3
+ require "net/https"
4
+
5
+ class API
6
+
7
+ API_ENDPOINT = 'https://smsgateway.me/api/v4/'
8
+
9
+ def self.get(endpoint, id)
10
+ uri = URI.parse(API_ENDPOINT + endpoint + "#{id}")
11
+ http = Net::HTTP.new(uri.host, uri.port)
12
+ http.use_ssl = true
13
+ req = Net::HTTP::Get.new(uri.path)
14
+ req['Authorization'] = SMSGateway.configuration.authorization_token
15
+ resp = http.request(req)
16
+ end
17
+
18
+ def self.post(endpoint, params)
19
+ uri = URI.parse(API_ENDPOINT + endpoint)
20
+ http = Net::HTTP.new(uri.host, uri.port)
21
+ http.use_ssl = true
22
+ request = Net::HTTP::Post.new(uri.path)
23
+ request['Authorization'] = SMSGateway.configuration.authorization_token
24
+ if endpoint == 'callback'
25
+ request.body = "#{params}"
26
+ else
27
+ request.body = "[ #{params} ]"
28
+ end
29
+ response = http.request(request)
30
+ end
31
+
32
+ def self.put(endpoint, params)
33
+ uri = URI.parse(API_ENDPOINT + endpoint)
34
+ http = Net::HTTP.new(uri.host, uri.port)
35
+ http.use_ssl = true
36
+ request = Net::HTTP::Put.new(uri.path)
37
+ request['Authorization'] = SMSGateway.configuration.authorization_token
38
+ request.body = "#{params}"
39
+ response = http.request(request)
40
+ end
41
+ end
data/lib/smsgateway.rb ADDED
@@ -0,0 +1,404 @@
1
+ require 'json'
2
+ require 'api'
3
+
4
+ module SMSGateway
5
+ class << self
6
+ attr_accessor :configuration
7
+ end
8
+
9
+ def self.configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def self.configure
14
+ yield(configuration) if block_given?
15
+ end
16
+
17
+ class Configuration
18
+ attr_accessor :authorization_token
19
+
20
+ def initialize
21
+ @authorization_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTUyODgwOTkzNSwiZXhwIjo0MTAyNDQ0ODAwLCJ1aWQiOjUxOTAzLCJyb2xlcyI6WyJST0xFX1VTRVIiXX0.2gZqY9ElCB8d1WxcLgrRmEp7q53dlTiHNrdNEXFDX9s'
22
+ end
23
+ end
24
+
25
+ class Message
26
+
27
+ API_ENDPOINT = 'message/'
28
+
29
+ attr_accessor :device_id, :phone_number, :message, :id, :status, :created_at, :updated_at, :log
30
+
31
+ def initialize(device_id, phone_number, message, id = nil, status = nil, created_at = nil, updated_at = nil, log = nil)
32
+ @id = id
33
+ @device_id = device_id
34
+ @phone_number = phone_number
35
+ @message = message
36
+ @status = status
37
+ @updated_at = updated_at
38
+ @created_at = created_at
39
+ @log = log
40
+ end
41
+
42
+ def send
43
+ result = nil
44
+ params = { device_id: @device_id, phone_number: @phone_number, message: @message }.to_json
45
+
46
+ response = API.post(API_ENDPOINT + 'send', params)
47
+ if response.code == '200'
48
+ res = JSON.parse(response.body)
49
+ @id = res[0]['id']
50
+ @device_id = res[0]['device_id']
51
+ @phone_number = res[0]['phone_number']
52
+ @message = res[0]['message']
53
+ @status = res[0]['status']
54
+ @updated_at = res[0]['updated_at']
55
+ @created_at = res[0]['created_at']
56
+ @log = res[0]['log']
57
+ result = self
58
+ end
59
+
60
+ result
61
+ end
62
+
63
+ def cancel
64
+ result = nil
65
+ params = { id: @id }.to_json
66
+
67
+ response = API.post(API_ENDPOINT + 'cancel', params)
68
+ if response.code == '200'
69
+ res = JSON.parse(response.body)
70
+ @id = res[0]['id']
71
+ @device_id = res[0]['device_id']
72
+ @phone_number = res[0]['phone_number']
73
+ @message = res[0]['message']
74
+ @status = res[0]['status']
75
+ @updated_at = res[0]['updated_at']
76
+ @created_at = res[0]['created_at']
77
+ @log = res[0]['log']
78
+ result = self
79
+ end
80
+
81
+ result
82
+ end
83
+
84
+ def self.where(params)
85
+ result = nil
86
+ search_params = []
87
+ tokens = params.split('AND')
88
+ tokens.each do |tkn|
89
+ tkn = tkn.split('=')
90
+ params = { field: tkn[0].strip!, operator: '=', value: tkn[1].delete("'").strip!}
91
+ search_params << params
92
+ end
93
+
94
+ params = { filters: [ search_params ], order_by: [ { field: 'created_at', direction: 'desc'}, limit: 5, offset: 5] }.to_json
95
+ response = API.post(API_ENDPOINT + 'search', params)
96
+ if response.code == '200'
97
+ res = JSON.parse(response.body)['results']
98
+ converted_objects = []
99
+ res.each do |obj|
100
+ converted_objects << Message.new(obj['device_id'], obj['phone_number'], obj['message'], obj['id'], obj['status'], obj['created_at'], obj['updated_at'], obj['log'])
101
+ end
102
+ result = converted_objects
103
+ end
104
+
105
+ result
106
+ end
107
+
108
+ def self.all
109
+ result = nil
110
+ params = { filters: [] }.to_json
111
+ response = API.post(API_ENDPOINT + 'search', params)
112
+ if response.code == '200'
113
+ res = JSON.parse(response.body)['results']
114
+ converted_objects = []
115
+ res.each do |obj|
116
+ converted_objects << Message.new(obj['device_id'], obj['phone_number'], obj['message'], obj['id'], obj['status'], obj['created_at'], obj['updated_at'], obj['log'])
117
+ end
118
+ result = converted_objects
119
+ end
120
+
121
+ result
122
+ end
123
+
124
+ def self.find(id)
125
+ result = nil
126
+ response = API.get(API_ENDPOINT, id)
127
+
128
+ if response.code == '200'
129
+ res = JSON.parse(response.body)
130
+ result = Message.new(res['device_id'], res['phone_number'], res['message'], res['id'], res['status'], res['created_at'], res['updated_at'], res['log'])
131
+ end
132
+
133
+ result
134
+ end
135
+ end
136
+
137
+ class Device
138
+
139
+ API_ENDPOINT = 'device/'
140
+
141
+ attr_accessor :id, :name, :attributes
142
+
143
+ def initialize(id, name, attributes)
144
+ @id = id
145
+ @name = name
146
+ @attributes = attributes
147
+ end
148
+
149
+ def self.find(id)
150
+ result = nil
151
+ response = API.get(API_ENDPOINT, id)
152
+ if response.code == '200'
153
+ res = JSON.parse(response.body)
154
+ result = Device.new(res['id'], res['name'], res['attributes'])
155
+ end
156
+
157
+ result
158
+ end
159
+
160
+ def self.all
161
+ result = nil
162
+ params = { filters: [] }.to_json
163
+ response = API.post(API_ENDPOINT + 'search', params)
164
+ if response.code == '200'
165
+ res = JSON.parse(response.body)['results']
166
+ converted_objects = []
167
+ res.each do |obj|
168
+ converted_objects << Device.new(obj['id'], obj['name'], obj['attributes'])
169
+ end
170
+ result = converted_objects
171
+ end
172
+
173
+ result
174
+ end
175
+
176
+ def self.where(params)
177
+ result = nil
178
+ search_params = []
179
+ tokens = params.split('AND')
180
+ tokens.each do |tkn|
181
+ tkn = tkn.split('=')
182
+ params = { field: tkn[0].strip!, operator: '=', value: tkn[1].delete("'").strip!}
183
+ search_params << params
184
+ end
185
+
186
+ params = { filters: [ search_params ], order_by: [ { field: 'type', direction: 'desc'}, limit: 5, offset: 5] }.to_json
187
+ response = API.post(API_ENDPOINT + 'search', params)
188
+ if response.code == '200'
189
+ res = JSON.parse(response.body)['results']
190
+ converted_objects = []
191
+ res.each do |obj|
192
+ converted_objects << Device.new(obj['id'], obj['name'], obj['attributes'])
193
+ end
194
+ result = converted_objects
195
+ end
196
+
197
+ result
198
+ end
199
+ end
200
+
201
+ class Contact
202
+
203
+ API_ENDPOINT = 'contact'
204
+
205
+ attr_accessor :id, :name, :phone_numbers
206
+
207
+ def initialize(name, phone_numbers, id = nil)
208
+ @id = id
209
+ @name = name
210
+ @phone_numbers = phone_numbers
211
+ end
212
+
213
+ def save
214
+ params = { name: @name, phone_numbers: @phone_numbers }.to_json
215
+ response = API.post(API_ENDPOINT, params)
216
+ if response.code == '200'
217
+ res = JSON.parse(response.body)
218
+ @id = res[0]['id']
219
+ @name = res[0]['name']
220
+ @phone_numbers = res[0]['phone_numbers']
221
+ result = self
222
+ end
223
+
224
+ result
225
+ end
226
+
227
+ def self.find(id)
228
+ result = nil
229
+ response = API.get(API_ENDPOINT + '/', id)
230
+ if response.code == '200'
231
+ res = JSON.parse(response.body)
232
+ result = Contact.new(res['name'], res['phone_numbers'], res['id'])
233
+ end
234
+
235
+ result
236
+ end
237
+
238
+ def self.where(params)
239
+ result = nil
240
+ search_params = []
241
+ tokens = params.split('AND')
242
+ tokens.each do |tkn|
243
+ tkn = tkn.split('=')
244
+ params = { field: tkn[0].strip!, operator: '=', value: tkn[1].delete("'").strip!}
245
+ search_params << params
246
+ end
247
+
248
+ params = { filters: [ search_params ], order_by: [ { field: 'name', direction: 'desc'}, limit: 5, offset: 5] }.to_json
249
+ response = API.post(API_ENDPOINT + '/search', params)
250
+ if response.code == '200'
251
+ res = JSON.parse(response.body)['results']
252
+ converted_objects = []
253
+ res.each do |obj|
254
+ converted_objects << Contact.new(obj['name'], obj['phone_numbers'], obj['id'])
255
+ end
256
+ result = converted_objects
257
+ end
258
+
259
+ result
260
+ end
261
+
262
+ def self.all
263
+ result = nil
264
+ params = { filters: [] }.to_json
265
+ response = API.post(API_ENDPOINT + '/search', params)
266
+ if response.code == '200'
267
+ res = JSON.parse(response.body)['results']
268
+ converted_objects = []
269
+ res.each do |obj|
270
+ converted_objects << Contact.new(obj['name'], obj['phone_numbers'], obj['id'])
271
+ end
272
+ result = converted_objects
273
+ end
274
+
275
+ result
276
+ end
277
+
278
+ def update
279
+ params = { name: @name, phone_numbers: @phone_numbers }.to_json
280
+ response = API.put(API_ENDPOINT + "/#{@id}", params)
281
+ if response.code == '200'
282
+ res = JSON.parse(response.body)
283
+ @id = res['id']
284
+ @name = res['name']
285
+ @phone_numbers = res['phone_numbers']
286
+ result = self
287
+ end
288
+
289
+ result
290
+ end
291
+ end
292
+
293
+ class Callback
294
+
295
+ API_ENDPOINT = 'callback/'
296
+
297
+ attr_accessor :name, :event, :device_id, :filter_type, :filter, :method, :action, :secret
298
+
299
+ def initialize(name=nil, event=nil, device_id=nil, filter_type=nil, filter=nil, method=nil, action=nil, secret=nil, id=nil)
300
+ @name = name
301
+ @event = event
302
+ @device_id = device_id
303
+ @filter_type = filter_type
304
+ @filter = filter
305
+ @method = method
306
+ @action = action
307
+ @secret = secret
308
+ @id = id
309
+ end
310
+
311
+ def save
312
+ result = nil
313
+ params = { name: @name, event: @event, device_id: @device_id, filter_type: @filter_type, filter: @filter, method: @method, action: @action, secret: @secret }.to_json
314
+
315
+ response = API.post(API_ENDPOINT.chomp('/'), params)
316
+ if response.code == '200'
317
+ res = JSON.parse(response.body)
318
+ @name = res['name']
319
+ @device_id = res['device_id']
320
+ @event = res['event']
321
+ @filter_type = res['filter_type']
322
+ @filter = res['filter']
323
+ @method = res['method']
324
+ @action = res['action']
325
+ @secret = res['secret']
326
+ result = self
327
+ end
328
+
329
+ result
330
+ end
331
+
332
+ def self.find(id)
333
+ result = nil
334
+ response = API.get(API_ENDPOINT, id)
335
+ if response.code == '200'
336
+ res = JSON.parse(response.body)
337
+ result = Callback.new(res['name'], res['event'], res['device_id'], res['filter_type'], res['filter'], res['method'], res['action'], res['secret'], res['id'])
338
+ end
339
+
340
+ result
341
+ end
342
+
343
+ def self.all
344
+ result = nil
345
+ params = { filters: [] }.to_json
346
+ response = API.post(API_ENDPOINT + 'search', params)
347
+ if response.code == '200'
348
+ res = JSON.parse(response.body)['results']
349
+ converted_objects = []
350
+ res.each do |obj|
351
+ converted_objects << Callback.new(obj['name'], obj['event'], obj['device_id'], obj['filter_type'], obj['filter'], obj['method'], obj['action'], obj['secret'], obj['id'])
352
+ end
353
+ result = converted_objects
354
+ end
355
+
356
+ result
357
+ end
358
+
359
+ def self.where(params)
360
+ result = nil
361
+ search_params = []
362
+ tokens = params.split('AND')
363
+ tokens.each do |tkn|
364
+ tkn = tkn.split('=')
365
+ params = { field: tkn[0].strip!, operator: '=', value: tkn[1].delete("'").strip!}
366
+ search_params << params
367
+ end
368
+
369
+ params = { filters: [ search_params ], order_by: [ { field: 'created_at', direction: 'desc'}, limit: 5, offset: 5] }.to_json
370
+ response = API.post(API_ENDPOINT + 'search', params)
371
+ if response.code == '200'
372
+ res = JSON.parse(response.body)['results']
373
+ converted_objects = []
374
+ res.each do |obj|
375
+ converted_objects << Callback.new(obj['name'], obj['event'], obj['device_id'], obj['filter_type'], obj['filter'], obj['method'], obj['action'], obj['secret'], obj['id'])
376
+ end
377
+ result = converted_objects
378
+ end
379
+
380
+ result
381
+ end
382
+
383
+ def update
384
+ result = nil
385
+ params = { name: @name, event: @event, device_id: @device_id, filter_type: @filter_type, filter: @filter, method: @method, action: @action, secret: @secret }.to_json
386
+
387
+ response = API.put(API_ENDPOINT + "#{@id}", params)
388
+ if response.code == '200'
389
+ res = JSON.parse(response.body)
390
+ @name = res['name']
391
+ @device_id = res['device_id']
392
+ @event = res['event']
393
+ @filter_type = res['filter_type']
394
+ @filter = res['filter']
395
+ @method = res['method']
396
+ @action = res['action']
397
+ @secret = res['secret']
398
+ result = self
399
+ end
400
+
401
+ result
402
+ end
403
+ end
404
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smsgateway
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ashhad Sheikh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A ruby gem to interact with smsgateway.me API
14
+ email: ashhadsheikh@hotmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/api.rb
20
+ - lib/smsgateway.rb
21
+ homepage:
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: '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.7.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A ruby gem to interact with smsgateway.me API!
45
+ test_files: []