cpsms 0.1.0
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.
- data/.gitignore +1 -0
- data/.rvmrc_example +4 -0
- data/.travis.yml +8 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +48 -0
- data/Guardfile +5 -0
- data/README.md +64 -0
- data/Rakefile +8 -0
- data/lib/cpsms.rb +4 -0
- data/lib/cpsms/sms.rb +50 -0
- data/lib/cpsms/version.rb +3 -0
- data/spec/fixtures/cpsms_cassettes/sms.yml +78 -0
- data/spec/lib/cpsms/sms_spec.rb +56 -0
- data/spec/spec_helper.rb +34 -0
- metadata +62 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.rvmrc
|
data/.rvmrc_example
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem 'httparty'
|
4
|
+
gem 'libxml-ruby', :platforms => [:ruby]
|
5
|
+
gem 'libxml-jruby', :platforms => [:jruby]
|
6
|
+
gem 'jruby-openssl', :platforms => [:jruby]
|
7
|
+
|
8
|
+
group :test do
|
9
|
+
gem 'minitest'
|
10
|
+
gem 'webmock'
|
11
|
+
gem 'vcr', '~> 2.0.0.rc1'
|
12
|
+
gem 'turn'
|
13
|
+
gem 'rake'
|
14
|
+
gem 'guard-minitest'
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.2.7)
|
5
|
+
ansi (1.4.2)
|
6
|
+
bouncy-castle-java (1.5.0146.1)
|
7
|
+
crack (0.3.1)
|
8
|
+
ffi (1.0.11)
|
9
|
+
ffi (1.0.11-java)
|
10
|
+
guard (0.10.0)
|
11
|
+
ffi (>= 0.5.0)
|
12
|
+
thor (~> 0.14.6)
|
13
|
+
guard-minitest (0.4.0)
|
14
|
+
guard (~> 0.4)
|
15
|
+
httparty (0.8.1)
|
16
|
+
multi_json
|
17
|
+
multi_xml
|
18
|
+
jruby-openssl (0.7.6.1)
|
19
|
+
bouncy-castle-java (>= 1.5.0146.1)
|
20
|
+
libxml-jruby (1.0.0)
|
21
|
+
libxml-ruby (2.2.2)
|
22
|
+
minitest (2.11.2)
|
23
|
+
multi_json (1.0.4)
|
24
|
+
multi_xml (0.4.1)
|
25
|
+
rake (0.9.2.2)
|
26
|
+
thor (0.14.6)
|
27
|
+
turn (0.9.2)
|
28
|
+
ansi
|
29
|
+
vcr (2.0.0.rc1)
|
30
|
+
webmock (1.7.10)
|
31
|
+
addressable (~> 2.2, > 2.2.5)
|
32
|
+
crack (>= 0.1.7)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
java
|
36
|
+
ruby
|
37
|
+
|
38
|
+
DEPENDENCIES
|
39
|
+
guard-minitest
|
40
|
+
httparty
|
41
|
+
jruby-openssl
|
42
|
+
libxml-jruby
|
43
|
+
libxml-ruby
|
44
|
+
minitest
|
45
|
+
rake
|
46
|
+
turn
|
47
|
+
vcr (~> 2.0.0.rc1)
|
48
|
+
webmock
|
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# CPSMS
|
2
|
+
|
3
|
+
A wrapper for the cpsms.dk API.
|
4
|
+
|
5
|
+
It will allow you to easily send SMS messages through the API from your Ruby applications.
|
6
|
+
|
7
|
+
[](http://travis-ci.org/dipth/cpsms)
|
8
|
+
|
9
|
+
# Prerequisites
|
10
|
+
|
11
|
+
To use the API you need to have an account on cpsms.dk
|
12
|
+
|
13
|
+
Once you have the account, you should have the following information:
|
14
|
+
|
15
|
+
* username
|
16
|
+
* password
|
17
|
+
|
18
|
+
You will need this information for all calls to the API.
|
19
|
+
|
20
|
+
# Sending an sms
|
21
|
+
|
22
|
+
To send an sms simply call the ```CPSMS::SMS.send!``` method
|
23
|
+
|
24
|
+
The method takes the following parameters
|
25
|
+
|
26
|
+
* **username**: Your cpsms.dk username (*string*)
|
27
|
+
* **password**: Your spsms.dk password (*string*)
|
28
|
+
* **recipient**: The phone-number of the recipient (*integer*)
|
29
|
+
* **message**: The message to send to the recipient (*string - max 459 chars*)
|
30
|
+
* **options**: A hash of optional options (described below)
|
31
|
+
|
32
|
+
To send a basic sms with the message: "Hello world" to 12345678
|
33
|
+
|
34
|
+
CPSMS::SMS.send! "username", "password", 12345678, "Hello World!"
|
35
|
+
|
36
|
+
You can pass a hash of optional options to the method. The possible options and their defaults are as follows
|
37
|
+
|
38
|
+
* **from**: A number or string that will be displayed as the sender of the message (*string - max 11 chars*)
|
39
|
+
* **url**: A callback url that CPSMS will call with details about the delivery. (See CPSMS's API documentation for more info) (*string - max 100 chars*)
|
40
|
+
* **timestamp**: A timestamp that specifies when the sms should be sent. (*Time*)
|
41
|
+
* **flash**: If set to true, the sms will be sent as a flash-sms (*boolean*)
|
42
|
+
* **group**: Currently not implemented
|
43
|
+
|
44
|
+
# Testing
|
45
|
+
|
46
|
+
To create new tests or edit existing you need to set your cpsms.dk credentials and a valid mobile number as environment variables.
|
47
|
+
|
48
|
+
More specifically you need to set these three environment variables with their respective values:
|
49
|
+
|
50
|
+
* CPSMS_USERNAME
|
51
|
+
* CPSMS_PASSWORD
|
52
|
+
* CPSMS_MOBILE_NUMBER
|
53
|
+
|
54
|
+
If you're using RVM, you can copy the supplied ```.rvmrc_example``` file to ```.rvmrc``` and edit the last two lines with your credentials
|
55
|
+
|
56
|
+
export CPSMS_USERNAME=YourUsername
|
57
|
+
export CPSMS_PASSWORD=YourPassword
|
58
|
+
export CPSMS_MOBILE_NUMBER=YourMobileNumber
|
59
|
+
|
60
|
+
**Please note:** Your mobile number must be prepended with the country-code, for instance: 4512345678 for a danish number (45).
|
61
|
+
|
62
|
+
By doing this, the variables will automatically be set when RVM sets the environment.
|
63
|
+
|
64
|
+
If you just wish to run the existing test suite, you don't need to set these values, as VCR will simply use existing recordings for communication with the API.
|
data/Rakefile
ADDED
data/lib/cpsms.rb
ADDED
data/lib/cpsms/sms.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module CPSMS
|
2
|
+
require 'xml'
|
3
|
+
|
4
|
+
class SMS
|
5
|
+
|
6
|
+
include HTTParty
|
7
|
+
|
8
|
+
base_uri 'https://www.cpsms.dk'
|
9
|
+
|
10
|
+
def self.send!(username, password, recipient)
|
11
|
+
body = { :username => username,
|
12
|
+
:password => password,
|
13
|
+
:recipient => recipient,
|
14
|
+
:message => "test" }
|
15
|
+
parse_response self.post('/sms/', :body => body).body
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def self.parse_response(response)
|
21
|
+
xml = XML::Parser.string(response).parse
|
22
|
+
|
23
|
+
error = xml.find_first('//error')
|
24
|
+
self.handle_errors(error.content) if error
|
25
|
+
|
26
|
+
success = xml.find_first('//succes') # cpsms.dk don't know how to spell
|
27
|
+
self.handle_success(success.content) if success
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.handle_errors(error)
|
31
|
+
case error
|
32
|
+
when 'Invalid username/password'
|
33
|
+
raise InvalidCredentialsError
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.handle_success(success)
|
38
|
+
case success
|
39
|
+
when 'SMS succesfully sent to 0 recipient(s)'
|
40
|
+
raise InvalidRecipientError
|
41
|
+
when 'SMS succesfully sent to 1 recipient(s)'
|
42
|
+
true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
class InvalidCredentialsError < StandardError; end
|
49
|
+
class InvalidRecipientError < StandardError; end
|
50
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: https://www.cpsms.dk/sms/
|
6
|
+
body: username=wrong&password=wrong&recipient=12345678&message=test
|
7
|
+
headers: {}
|
8
|
+
response:
|
9
|
+
status:
|
10
|
+
code: 200
|
11
|
+
message: OK
|
12
|
+
headers:
|
13
|
+
Date:
|
14
|
+
- Sat, 18 Feb 2012 18:23:23 GMT
|
15
|
+
Server:
|
16
|
+
- Apache/2.2.14 (EL)
|
17
|
+
X-Powered-By:
|
18
|
+
- PHP/5.2.11
|
19
|
+
Content-Length:
|
20
|
+
- '40'
|
21
|
+
Connection:
|
22
|
+
- close
|
23
|
+
Content-Type:
|
24
|
+
- text/html
|
25
|
+
body: <error>Invalid username/password</error>
|
26
|
+
http_version:
|
27
|
+
recorded_at: Sat, 18 Feb 2012 18:23:19 GMT
|
28
|
+
- request:
|
29
|
+
method: post
|
30
|
+
uri: https://www.cpsms.dk/sms/
|
31
|
+
body: username=<USERNAME>&password=<PASSWORD>&recipient=<MOBILE_NUMBER>&message=test
|
32
|
+
headers: {}
|
33
|
+
response:
|
34
|
+
status:
|
35
|
+
code: 200
|
36
|
+
message: OK
|
37
|
+
headers:
|
38
|
+
Date:
|
39
|
+
- Sat, 18 Feb 2012 18:23:24 GMT
|
40
|
+
Server:
|
41
|
+
- Apache/2.2.14 (EL)
|
42
|
+
X-Powered-By:
|
43
|
+
- PHP/5.2.11
|
44
|
+
Content-Length:
|
45
|
+
- '55'
|
46
|
+
Connection:
|
47
|
+
- close
|
48
|
+
Content-Type:
|
49
|
+
- text/html
|
50
|
+
body: <succes>SMS succesfully sent to 1 recipient(s)</succes>
|
51
|
+
http_version:
|
52
|
+
recorded_at: Sat, 18 Feb 2012 18:23:20 GMT
|
53
|
+
- request:
|
54
|
+
method: post
|
55
|
+
uri: https://www.cpsms.dk/sms/
|
56
|
+
body: username=<USERNAME>&password=<PASSWORD>&recipient=12345678&message=test
|
57
|
+
headers: {}
|
58
|
+
response:
|
59
|
+
status:
|
60
|
+
code: 200
|
61
|
+
message: OK
|
62
|
+
headers:
|
63
|
+
Date:
|
64
|
+
- Sat, 18 Feb 2012 18:23:25 GMT
|
65
|
+
Server:
|
66
|
+
- Apache/2.2.14 (EL)
|
67
|
+
X-Powered-By:
|
68
|
+
- PHP/5.2.11
|
69
|
+
Content-Length:
|
70
|
+
- '55'
|
71
|
+
Connection:
|
72
|
+
- close
|
73
|
+
Content-Type:
|
74
|
+
- text/html
|
75
|
+
body: <succes>SMS succesfully sent to 0 recipient(s)</succes>
|
76
|
+
http_version:
|
77
|
+
recorded_at: Sat, 18 Feb 2012 18:23:21 GMT
|
78
|
+
recorded_with: VCR 2.0.0.rc1
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require(File.expand_path('./../../../spec_helper', __FILE__))
|
2
|
+
|
3
|
+
describe CPSMS::SMS do
|
4
|
+
|
5
|
+
let(:username) { ENV.fetch('CPSMS_USERNAME', 'fake-cpsms-username') }
|
6
|
+
let(:password) { ENV.fetch('CPSMS_PASSWORD', 'fake-cpsms-password') }
|
7
|
+
let(:recipient) { ENV.fetch('CPSMS_MOBILE_NUMBER', 'fake-cpsms-mobile-number') }
|
8
|
+
|
9
|
+
describe "default attributes" do
|
10
|
+
it "must include httparty methods" do
|
11
|
+
CPSMS::SMS.must_include HTTParty
|
12
|
+
end
|
13
|
+
|
14
|
+
it "must have the base url set to the CPSMS API endpoint" do
|
15
|
+
CPSMS::SMS.base_uri.must_equal 'https://www.cpsms.dk'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".send!" do
|
20
|
+
before do
|
21
|
+
VCR.insert_cassette 'sms',
|
22
|
+
:record => :new_episodes,
|
23
|
+
:match_requests_on => [:method, :body]
|
24
|
+
end
|
25
|
+
after { VCR.eject_cassette }
|
26
|
+
|
27
|
+
it "must have a send! method" do
|
28
|
+
CPSMS::SMS.must_respond_to :send!
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "with invalid credentials" do
|
32
|
+
it "raises a CPSMS::InvalidCredentialsError" do
|
33
|
+
proc do
|
34
|
+
CPSMS::SMS.send!("wrong", "wrong", 12345678)
|
35
|
+
end.must_raise CPSMS::InvalidCredentialsError
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "with valid credentials" do
|
40
|
+
describe "and invalid recipient" do
|
41
|
+
it "raises a CPSMS::InvalidRecipientError" do
|
42
|
+
proc do
|
43
|
+
CPSMS::SMS.send!(username, password, 12345678)
|
44
|
+
end.must_raise CPSMS::InvalidRecipientError
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "and a valid recipient" do
|
49
|
+
it "returns true" do
|
50
|
+
CPSMS::SMS.send!(username, password, recipient).must_equal true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#we need the actual library file
|
2
|
+
require(File.expand_path('../../lib/cpsms', __FILE__))
|
3
|
+
|
4
|
+
#dependencies
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'webmock/minitest'
|
7
|
+
require 'vcr'
|
8
|
+
require 'turn'
|
9
|
+
|
10
|
+
Turn.config do |c|
|
11
|
+
# :outline - turn's original case/test outline mode [default]
|
12
|
+
c.format = :outline
|
13
|
+
# use humanized test names (works only with :outline format)
|
14
|
+
c.natural = true
|
15
|
+
end
|
16
|
+
|
17
|
+
#VCR config
|
18
|
+
VCR.configure do |c|
|
19
|
+
c.cassette_library_dir = 'spec/fixtures/cpsms_cassettes'
|
20
|
+
c.stub_with :webmock
|
21
|
+
c.filter_sensitive_data('username=<USERNAME>') { "username=#{ ENV.fetch('CPSMS_USERNAME', 'fake-cpsms-username') }" }
|
22
|
+
c.filter_sensitive_data('password=<PASSWORD>') { "password=#{ ENV.fetch('CPSMS_PASSWORD', 'fake-cpsms-password') }" }
|
23
|
+
c.filter_sensitive_data('recipient=<MOBILE_NUMBER>') { "recipient=#{ ENV.fetch('CPSMS_MOBILE_NUMBER', 'fake-cpsms-mobile-number') }" }
|
24
|
+
|
25
|
+
# Since HTTParty takes a Hash of body data, we can never be sure that it is
|
26
|
+
# sent in the same order. This can cause VCR to not recognise the request and
|
27
|
+
# therefore not use a previously recorded response.
|
28
|
+
# This overrides VCR's built-in :body matcher to ignore the order of the data
|
29
|
+
c.register_request_matcher :body do |request1, request2|
|
30
|
+
body1 = request1.body.split('&')
|
31
|
+
body2 = request2.body.split('&')
|
32
|
+
(body1 - body2).empty?
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cpsms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- dipth
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Wrapper for the cpsms.dk API
|
15
|
+
email:
|
16
|
+
- thomas@dippel.dk
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rvmrc_example
|
23
|
+
- .travis.yml
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- Guardfile
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- lib/cpsms.rb
|
30
|
+
- lib/cpsms/sms.rb
|
31
|
+
- lib/cpsms/version.rb
|
32
|
+
- spec/fixtures/cpsms_cassettes/sms.yml
|
33
|
+
- spec/lib/cpsms/sms_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
homepage: https://github.com/dipth/cpsms
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project: cpsms
|
55
|
+
rubygems_version: 1.8.15
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Wrapper for the cpsms.dk API
|
59
|
+
test_files:
|
60
|
+
- spec/fixtures/cpsms_cassettes/sms.yml
|
61
|
+
- spec/lib/cpsms/sms_spec.rb
|
62
|
+
- spec/spec_helper.rb
|