smspilot 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -1
- data/lib/smspilot/configuration.rb +7 -3
- data/lib/smspilot/request.rb +14 -6
- data/lib/smspilot/version.rb +1 -1
- data/spec/lib/smspilot_spec.rb +106 -84
- metadata +2 -2
data/README.md
CHANGED
@@ -3,13 +3,15 @@ require 'smspilot/version'
|
|
3
3
|
|
4
4
|
module Smspilot
|
5
5
|
module Configuration
|
6
|
-
VALID_OPTIONS_KEYS = [:endpoint, :user_agent, :adapter, :timeout, :api_key, :logger]
|
6
|
+
VALID_OPTIONS_KEYS = [:endpoint, :user_agent, :adapter, :timeout, :api_key, :logger, :login, :password]
|
7
7
|
|
8
8
|
DEFAULT_ADAPTER = :net_http
|
9
9
|
DEFAULT_ENDPOINT = 'http://smspilot.ru/api2.php'
|
10
10
|
DEFAULT_USER_AGENT = "smspilot gem #{Smspilot::VERSION}".freeze
|
11
11
|
DEFAULT_TIMEOUT = 5
|
12
12
|
DEFAILT_API_KEY = nil
|
13
|
+
DEFAILT_LOGIN = nil
|
14
|
+
DEFAILT_PASSWORD = nil
|
13
15
|
DEFAILT_LOGGER = Logger.new("log/smspilot.log")
|
14
16
|
|
15
17
|
attr_accessor *VALID_OPTIONS_KEYS
|
@@ -33,8 +35,10 @@ module Smspilot
|
|
33
35
|
self.endpoint = DEFAULT_ENDPOINT
|
34
36
|
self.user_agent = DEFAULT_USER_AGENT
|
35
37
|
self.timeout = DEFAULT_TIMEOUT
|
36
|
-
self.api_key
|
37
|
-
self.
|
38
|
+
self.api_key = DEFAILT_API_KEY
|
39
|
+
self.login = DEFAILT_LOGIN
|
40
|
+
self.password = DEFAILT_PASSWORD
|
41
|
+
self.logger = DEFAILT_LOGGER
|
38
42
|
self
|
39
43
|
end
|
40
44
|
|
data/lib/smspilot/request.rb
CHANGED
@@ -7,22 +7,22 @@ module Smspilot
|
|
7
7
|
module Request
|
8
8
|
|
9
9
|
def send_request(body)
|
10
|
-
json_body =
|
10
|
+
json_body = request_json body
|
11
11
|
begin
|
12
12
|
response = connection.post do |req|
|
13
13
|
req.body = json_body
|
14
14
|
logger.info("--------\n" + req.body)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
response_error = (response.status == 200) ? Smspilot::Error::WrongStatusError : nil
|
18
18
|
|
19
|
-
rescue Exception => e
|
19
|
+
rescue Exception => e
|
20
20
|
if e.kind_of? Faraday::Error::TimeoutError
|
21
21
|
response_error = Smspilot::Error::TimeoutError
|
22
22
|
elsif e.kind_of? Faraday::Error::ParsingError
|
23
|
-
response_error = Smspilot::Error::ParsingError
|
24
|
-
end
|
25
|
-
end
|
23
|
+
response_error = Smspilot::Error::ParsingError
|
24
|
+
end
|
25
|
+
end
|
26
26
|
|
27
27
|
unless response.body["error"].nil?
|
28
28
|
response_error = Error::ApiError.get_by_code(response.body["error"]["code"]).new()
|
@@ -34,5 +34,13 @@ module Smspilot
|
|
34
34
|
|
35
35
|
response
|
36
36
|
end
|
37
|
+
|
38
|
+
def request_json body
|
39
|
+
if login.nil?
|
40
|
+
{"apikey" => api_key}.merge(body).to_json
|
41
|
+
else
|
42
|
+
{"apikey" => api_key, "login" => login, "password" => password}.merge(body).to_json
|
43
|
+
end
|
44
|
+
end
|
37
45
|
end
|
38
46
|
end
|
data/lib/smspilot/version.rb
CHANGED
data/spec/lib/smspilot_spec.rb
CHANGED
@@ -14,108 +14,130 @@ describe Smspilot do
|
|
14
14
|
|
15
15
|
#TODO add specs for sms delivery statuses
|
16
16
|
#TODO add specs for parameter validations
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
describe "Request" do
|
18
|
+
let(:test_body) { {test: "test"} }
|
19
|
+
let(:login) {"log"}
|
20
|
+
let(:password) {"pass"}
|
21
|
+
|
22
|
+
describe "#request_json" do
|
23
|
+
|
24
|
+
it "should add login and password to request if present" do
|
25
|
+
@client = Smspilot.new(api_key: "XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ", login: login, password: password)
|
26
|
+
req = JSON.parse(@client.request_json(test_body))
|
27
|
+
req["login"].should == login
|
28
|
+
req["password"].should == password
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not add login and password to request if not present" do
|
32
|
+
@client = Smspilot.new "XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ"
|
33
|
+
req = JSON.parse(@client.request_json(test_body))
|
34
|
+
req["login"].should == nil
|
35
|
+
req["password"].should == nil
|
36
|
+
end
|
37
|
+
end
|
21
38
|
end
|
22
39
|
|
23
|
-
describe "
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
it "should return correct result when succeeded" do
|
28
|
-
stub_normal_request(json_send_response)
|
29
|
-
result = @client.send_sms(sms_id, sms_from, sms_to, message_text)
|
30
|
-
result.body.should == {"send"=> [{"id"=>"12345", "server_id"=>"10005", "from"=>"SMSPILOT", "to"=>"79091112233", "text"=>"Тест", "zone"=>"1", "parts"=>"1", "credits"=>"1", "status"=>"0", "error"=>"0"}], "server_packet_id"=>"1234", "balance"=>"10000"}
|
31
|
-
result.status.should eql(200)
|
40
|
+
describe "Api" do
|
41
|
+
before do
|
42
|
+
@client = Smspilot.new "XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ"
|
32
43
|
end
|
33
44
|
|
34
|
-
|
45
|
+
describe "#send_sms" do
|
35
46
|
|
47
|
+
let(:json_send_response) { {"send"=> [{"id"=>"12345", "server_id"=>"10005", "from"=>"SMSPILOT", "to"=>"79091112233", "text"=>"Тест", "zone"=>"1", "parts"=>"1", "credits"=>"1", "status"=>"0", "error"=>"0"}], "server_packet_id"=>"1234", "balance"=>"10000"}.to_json}
|
36
48
|
|
37
|
-
|
38
|
-
|
39
|
-
|
49
|
+
it "should return correct result when succeeded" do
|
50
|
+
stub_normal_request(json_send_response)
|
51
|
+
result = @client.send_sms(sms_id, sms_from, sms_to, message_text)
|
52
|
+
result.body.should == {"send"=> [{"id"=>"12345", "server_id"=>"10005", "from"=>"SMSPILOT", "to"=>"79091112233", "text"=>"Тест", "zone"=>"1", "parts"=>"1", "credits"=>"1", "status"=>"0", "error"=>"0"}], "server_packet_id"=>"1234", "balance"=>"10000"}
|
53
|
+
result.status.should eql(200)
|
54
|
+
end
|
40
55
|
|
41
|
-
it "should return correct hash when succeeded" do
|
42
|
-
stub_normal_request(check_response_hash.to_json)
|
43
|
-
|
44
|
-
result = @client.check_sms_status(sms_server_id)
|
45
|
-
result.body.should == {"check"=> [{"id"=>"12345", "server_id"=>"10005", "status"=>"1", "modified"=>"2011-08-11 14:35:00"}]}
|
46
|
-
result.status.should eql(200)
|
47
|
-
end
|
48
|
-
it "should return correct sms status when succeeded" do
|
49
|
-
check_response_hash["check"].first["status"] = Smspilot::Api::NOT_FOUND_STATUS
|
50
|
-
stub_normal_request(check_response_hash.to_json)
|
51
|
-
|
52
|
-
result = @client.check_sms_status(sms_server_id)
|
53
|
-
result.not_found?.should be_true
|
54
|
-
end
|
55
|
-
it "should return correct sms status when succeeded" do
|
56
|
-
check_response_hash["check"].first["status"] = Smspilot::Api::NOT_DELIVERED_STATUS
|
57
|
-
stub_normal_request(check_response_hash.to_json)
|
58
|
-
|
59
|
-
result = @client.check_sms_status(sms_server_id)
|
60
|
-
result.not_delivered?.should be_true
|
61
|
-
end
|
62
|
-
it "should return correct sms status when succeeded" do
|
63
|
-
check_response_hash["check"].first["status"] = Smspilot::Api::ACCEPTED_STATUS
|
64
|
-
stub_normal_request(check_response_hash.to_json)
|
65
|
-
|
66
|
-
result = @client.check_sms_status(sms_server_id)
|
67
|
-
result.accepted?.should be_true
|
68
|
-
end
|
69
|
-
it "should return correct sms status when succeeded" do
|
70
|
-
check_response_hash["check"].first["status"] = Smspilot::Api::AT_OPERATOR_STATUS
|
71
|
-
stub_normal_request(check_response_hash.to_json)
|
72
|
-
|
73
|
-
result = @client.check_sms_status(sms_server_id)
|
74
|
-
result.at_operator?.should be_true
|
75
|
-
end
|
76
|
-
it "should return correct sms status when succeeded" do
|
77
|
-
check_response_hash["check"].first["status"] = Smspilot::Api::DELIVERED_STATUS
|
78
|
-
stub_normal_request(check_response_hash.to_json)
|
79
|
-
|
80
|
-
result = @client.check_sms_status(sms_server_id)
|
81
|
-
result.delivered?.should be_true
|
82
56
|
end
|
83
57
|
|
84
|
-
end
|
85
58
|
|
86
|
-
|
87
|
-
|
88
|
-
|
59
|
+
describe "#check_sms_status" do
|
60
|
+
|
61
|
+
let (:check_response_hash) { {"check"=> [{"id"=>"12345", "server_id"=>"10005", "status"=>"1", "modified"=>"2011-08-11 14:35:00"}]} }
|
62
|
+
|
63
|
+
it "should return correct hash when succeeded" do
|
64
|
+
stub_normal_request(check_response_hash.to_json)
|
65
|
+
|
66
|
+
result = @client.check_sms_status(sms_server_id)
|
67
|
+
result.body.should == {"check"=> [{"id"=>"12345", "server_id"=>"10005", "status"=>"1", "modified"=>"2011-08-11 14:35:00"}]}
|
68
|
+
result.status.should eql(200)
|
69
|
+
end
|
70
|
+
it "should return correct sms status when succeeded" do
|
71
|
+
check_response_hash["check"].first["status"] = Smspilot::Api::NOT_FOUND_STATUS
|
72
|
+
stub_normal_request(check_response_hash.to_json)
|
73
|
+
|
74
|
+
result = @client.check_sms_status(sms_server_id)
|
75
|
+
result.not_found?.should be_true
|
76
|
+
end
|
77
|
+
it "should return correct sms status when succeeded" do
|
78
|
+
check_response_hash["check"].first["status"] = Smspilot::Api::NOT_DELIVERED_STATUS
|
79
|
+
stub_normal_request(check_response_hash.to_json)
|
80
|
+
|
81
|
+
result = @client.check_sms_status(sms_server_id)
|
82
|
+
result.not_delivered?.should be_true
|
83
|
+
end
|
84
|
+
it "should return correct sms status when succeeded" do
|
85
|
+
check_response_hash["check"].first["status"] = Smspilot::Api::ACCEPTED_STATUS
|
86
|
+
stub_normal_request(check_response_hash.to_json)
|
87
|
+
|
88
|
+
result = @client.check_sms_status(sms_server_id)
|
89
|
+
result.accepted?.should be_true
|
90
|
+
end
|
91
|
+
it "should return correct sms status when succeeded" do
|
92
|
+
check_response_hash["check"].first["status"] = Smspilot::Api::AT_OPERATOR_STATUS
|
93
|
+
stub_normal_request(check_response_hash.to_json)
|
94
|
+
|
95
|
+
result = @client.check_sms_status(sms_server_id)
|
96
|
+
result.at_operator?.should be_true
|
97
|
+
end
|
98
|
+
it "should return correct sms status when succeeded" do
|
99
|
+
check_response_hash["check"].first["status"] = Smspilot::Api::DELIVERED_STATUS
|
100
|
+
stub_normal_request(check_response_hash.to_json)
|
101
|
+
|
102
|
+
result = @client.check_sms_status(sms_server_id)
|
103
|
+
result.delivered?.should be_true
|
104
|
+
end
|
89
105
|
|
90
|
-
it "should return correct hash when succeeded" do
|
91
|
-
stub_normal_request(json_balance_response)
|
92
|
-
|
93
|
-
result = @client.check_balance
|
94
|
-
result.body.should == {"balance"=> 31337}
|
95
|
-
result.status.should eql(200)
|
96
106
|
end
|
97
107
|
|
98
|
-
|
108
|
+
describe "#check_balance" do
|
99
109
|
|
100
|
-
|
110
|
+
let(:json_balance_response) {'{"balance":31337}'}
|
111
|
+
|
112
|
+
it "should return correct hash when succeeded" do
|
113
|
+
stub_normal_request(json_balance_response)
|
114
|
+
|
115
|
+
result = @client.check_balance
|
116
|
+
result.body.should == {"balance"=> 31337}
|
117
|
+
result.status.should eql(200)
|
118
|
+
end
|
101
119
|
|
102
|
-
before do
|
103
|
-
stub_normal_request(json_failure_response)
|
104
120
|
end
|
105
121
|
|
106
|
-
|
107
|
-
result = @client.send_request({})
|
108
|
-
result.error.should eql(Smspilot::Error::UnknownApiError)
|
109
|
-
end
|
122
|
+
describe "send_request errors" do
|
110
123
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
result = @client.send_request({})
|
116
|
-
result.error.should eql(Smspilot::Error::LeetError)
|
117
|
-
end
|
124
|
+
before do
|
125
|
+
stub_normal_request(json_failure_response)
|
126
|
+
end
|
118
127
|
|
119
|
-
|
128
|
+
it "should be unknown apierror when there is correct error response" do
|
129
|
+
result = @client.send_request({})
|
130
|
+
result.error.should eql(Smspilot::Error::UnknownApiError)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should be correct apierror type when there is correct error response" do
|
134
|
+
Smspilot::Error::API_ERROR_CODES["1337"] = "LeetError"
|
135
|
+
class Smspilot::Error::LeetError < Smspilot::Error::ApiError; end
|
136
|
+
|
137
|
+
result = @client.send_request({})
|
138
|
+
result.error.should eql(Smspilot::Error::LeetError)
|
139
|
+
end
|
120
140
|
|
141
|
+
end
|
142
|
+
end
|
121
143
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smspilot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|