voodoo_sms 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.
- checksums.yaml +7 -0
- data/lib/voodoo_sms.rb +103 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b8906c64f53f73143d167395b8ecb3b632229663
|
4
|
+
data.tar.gz: c74233d2b5f45c0e412e0f0cd99fae4a0653ff4d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bc059e9f8b178904d46ee979a1ad02cb4509765d3f5a04ae737799f5d0d1d2b4484e21fb7f3e887e833a4bad28826a616d30c6e6c0727fcd7b6b597d432d5fe2
|
7
|
+
data.tar.gz: 51651ba9e6aadf0d2c6f5d7a410c970da53482074b2f3813f47260119bb9ed3699467f6525c399db35568b62a4bc382274b2c89474e6bca507bda86b25f7af11
|
data/lib/voodoo_sms.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
# TODO: RDoc
|
4
|
+
|
5
|
+
class VoodooSMS
|
6
|
+
module Error
|
7
|
+
class BadRequest < StandardError; end
|
8
|
+
class Unauthorised < StandardError; end
|
9
|
+
class NotEnoughCredit < StandardError; end
|
10
|
+
class Forbidden < StandardError; end
|
11
|
+
class MessageTooLarge < StandardError; end
|
12
|
+
class Unexpected < StandardError; end
|
13
|
+
class InvalidParameterFormat < StandardError; end
|
14
|
+
end
|
15
|
+
|
16
|
+
include HTTParty
|
17
|
+
base_uri 'voodoosms.com'
|
18
|
+
default_params format: 'json'
|
19
|
+
format :json
|
20
|
+
|
21
|
+
def initialize(username, password)
|
22
|
+
@params = { query: { uid: username, pass: password } }
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_credit
|
26
|
+
make_request('getCredit')['credit']
|
27
|
+
end
|
28
|
+
|
29
|
+
def send_sms(originator, destination, message)
|
30
|
+
merge_params(orig: originator, dest: destination, msg: message, validity: 1)
|
31
|
+
make_request('sendSMS')['resultText'].to_s.include? 'OK'
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_sms(from, to, keyword = '')
|
35
|
+
merge_params(from: format_date(from), to: format_date(to), keyword: keyword)
|
36
|
+
response = make_request('getSMS')['messages']
|
37
|
+
if response.is_a?(Array)
|
38
|
+
response.map { |r| OpenStruct.new(from: r['Originator'],
|
39
|
+
timestamp: DateTime.parse(r['TimeStamp']),
|
40
|
+
message: r['Message']) }
|
41
|
+
else
|
42
|
+
[]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def merge_params(opts)
|
48
|
+
@params[:query].merge!(opts)
|
49
|
+
end
|
50
|
+
|
51
|
+
def make_request(method)
|
52
|
+
validate_parameters_for(method)
|
53
|
+
|
54
|
+
begin
|
55
|
+
response = self.class.get("/vapi/server/#{method}", @params)
|
56
|
+
rescue => e
|
57
|
+
raise Error::Unexpected.new(e.message)
|
58
|
+
end
|
59
|
+
|
60
|
+
case response['result']
|
61
|
+
when 200, '200 OK' # inconsistencies :(
|
62
|
+
return response
|
63
|
+
when 'You dont have any messages'
|
64
|
+
return {} # :(
|
65
|
+
when 400
|
66
|
+
raise Error::BadRequest.new(response.values.join(', '))
|
67
|
+
when 401
|
68
|
+
raise Error::Unauthorised.new(response.values.join(', '))
|
69
|
+
when 402
|
70
|
+
raise Error::NotEnoughCredit.new(response.values.join(', '))
|
71
|
+
when 403
|
72
|
+
raise Error::Forbidden.new(response.values.join(', '))
|
73
|
+
when 513
|
74
|
+
raise Error::MessageTooLarge.new(response.values.join(', '))
|
75
|
+
else
|
76
|
+
raise Error::Unexpected.new(response.values.join(', '))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def validate_parameters_for(method)
|
81
|
+
case method
|
82
|
+
when 'sendSMS'
|
83
|
+
validate_originator @params[:query][:orig]
|
84
|
+
validate_destination @params[:query][:dest]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def validate_originator(input)
|
89
|
+
unless input.match /^[a-zA-Z0-9]{1,11}(\d{4})?$/
|
90
|
+
raise Error::InvalidParameterFormat.new('must be 15 numeric digits or 11 alphanumerics')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def validate_destination(input)
|
95
|
+
unless input.match /^\d{10,15}$/
|
96
|
+
raise Error::InvalidParameterFormat.new('must be valid E.164 format')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def format_date(date)
|
101
|
+
date.respond_to?(:strftime) ? date.strftime("%F %T") : date
|
102
|
+
end
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: voodoo_sms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Sabin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: vcr
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.19'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.19'
|
69
|
+
description: Ruby wrapper for VoodooSMS API
|
70
|
+
email:
|
71
|
+
- tom.sabin@unboxedconsulting.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- lib/voodoo_sms.rb
|
77
|
+
homepage: https://github.com/unboxed/voodoo-sms-ruby
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: VoodooSMS API
|
101
|
+
test_files: []
|