atompark-sms-api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74d7bbb7fdbbc9c57af816e4fe3148caa52a5a15
4
- data.tar.gz: 2cf1c24001c9175021b3b5433e9e36d1e87b68d9
3
+ metadata.gz: 1fe14b3d81e256c5ee664d9e09a53e5cdf959888
4
+ data.tar.gz: 213c36795e4c1a4f4a226642350c7d10f5979de8
5
5
  SHA512:
6
- metadata.gz: e4538d4846c9ba66034f637d77dd19c4856022af33435288e48bdd2aa8e7a19938680e09b49801d3b2f259a3901655205f66922d9b36648f94d8d8aad864ba99
7
- data.tar.gz: b2d06f8e31c92307ae46acb96b250504da71cb322bd10287dbbfc49cf16830654654f3ed812418ec0e9bcc1b53cdb9c4101de59454297ae95702ec4db93d1a1a
6
+ metadata.gz: 206b6b336d32e994df41b44f53a34f6f1f408d0669bbb15d440a72864295d569a19fab4afdb2b83bc49a41102e0bfd10d6bbd597c0bd2090d0476d8765041472
7
+ data.tar.gz: 637db40716cd253a52ea17031433168fbc6c08031f806d1655968ec1934096b046cd95f439612bba966aac06aa340bd1380a35f34dd0a30683291aa071cdcbf3
@@ -8,12 +8,13 @@ module AtomparkSmsApi
8
8
  end
9
9
 
10
10
  class Configuration
11
- attr_accessor :pubkey, :pvtkey, :base_url, :sender
11
+ attr_accessor :pubkey, :pvtkey, :base_url, :sender, :test
12
12
 
13
13
  def initialize
14
14
  @pubkey = ''
15
15
  @pvtkey = ''
16
16
  @sender = ''
17
+ @test = false
17
18
  @base_url = 'http://api.atompark.com/sms/3.0/'
18
19
  end
19
20
  end
@@ -1,3 +1,5 @@
1
+ require 'faraday'
2
+
1
3
  class AtomparkSmsApi::Gateway
2
4
  def self.perform(*args)
3
5
  fail "Don't call me that way"
@@ -6,10 +8,9 @@ class AtomparkSmsApi::Gateway
6
8
  def initialize(method, params)
7
9
  @method = method
8
10
  @params = params.merge(action: method, key: AtomparkSmsApi.config.pubkey, version: '3.0')
11
+ @params.merge!(test: true) if AtomparkSmsApi.config.test
12
+
9
13
  @conn = Faraday.new(url: AtomparkSmsApi.config.base_url)
10
- # do |faraday|
11
- # faraday.response(:logger)
12
- # end
13
14
  end
14
15
 
15
16
  def perform
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  class AtomparkSmsApi::Response
2
4
  def initialize(data)
3
5
  @response = JSON.parse(data)
@@ -11,11 +13,22 @@ class AtomparkSmsApi::Response
11
13
  response.has_key?('error')
12
14
  end
13
15
 
14
- def message
15
- "<#{response['code']}> #{response['error']}"
16
+ def error
17
+ "<#{response['code']}:#{response['result']}> #{response['error']}"
18
+ end
19
+
20
+ %w(id price currency).each do |key|
21
+ define_method(key) do
22
+ result[key]
23
+ end
16
24
  end
17
25
 
18
26
  private
19
27
 
20
28
  attr_reader :response
29
+
30
+ def result
31
+ fail error if error?
32
+ response['result']
33
+ end
21
34
  end
@@ -1,4 +1,3 @@
1
1
  module AtomparkSmsApi
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
4
-
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe AtomparkSmsApi::Configuration do
4
+ subject { AtomparkSmsApi.config }
5
+
6
+ it { expect(subject.pubkey).to eq('') }
7
+ it { expect(subject.pvtkey).to eq('') }
8
+ it { expect(subject.sender).to eq('') }
9
+ it { expect(subject.base_url).to eq('http://api.atompark.com/sms/3.0/') }
10
+ it { expect(subject.test).to be_falsey }
11
+
12
+ shared_examples_for 'set variable' do |var, val|
13
+ before { AtomparkSmsApi.configure { |c| c.send(:"#{var}=", val) } }
14
+ it { expect(subject.send(var)).to eq(val) }
15
+ end
16
+
17
+ it_behaves_like 'set variable', :pubkey, 'public key'
18
+ it_behaves_like 'set variable', :pvtkey, 'private key'
19
+ it_behaves_like 'set variable', :sender, 'IvanPteroff'
20
+ it_behaves_like 'set variable', :test, true
21
+ it_behaves_like 'set variable', :base_url, '/dev/null'
22
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe AtomparkSmsApi::Gateway do
4
+ it { expect{described_class.perform}.to raise_error(RuntimeError, "Don't call me that way") }
5
+
6
+ describe '#perform' do
7
+ let(:faraday) { double }
8
+ let(:method) { :some_call }
9
+ let(:params) { { } }
10
+
11
+ before do
12
+ expect(Faraday).to receive(:new).with(url: AtomparkSmsApi.config.base_url).and_return(faraday)
13
+ end
14
+
15
+ subject { described_class.new(method, params) }
16
+
17
+ describe '#checksum' do
18
+ let(:params) { {foo: 'bar'} }
19
+ before do
20
+ AtomparkSmsApi.configure do |c|
21
+ c.pubkey = 'X'
22
+ c.pvtkey = 'Y'
23
+ c.test = false
24
+ end
25
+ end
26
+
27
+ # some_callbarX3.0Y - md5 sum, params in order: action, foo, pubkey, version, pvtkey
28
+ it { expect(subject.send(:checksum)).to eq('72c28714005950deb5ed6ed80d20a9ad') }
29
+ end
30
+
31
+ describe '#perform' do
32
+ context 'success' do
33
+ before do
34
+ expect(faraday).to receive(:post).and_return(double(body: %Q{{"result" : {"id" : 10}}}))
35
+ end
36
+
37
+ it { expect{subject.perform}.to_not raise_error }
38
+ it { expect(subject.perform).to be_kind_of(AtomparkSmsApi::Response) }
39
+ it { expect(subject.perform.success?).to be_truthy }
40
+ it { expect(subject.perform.id).to eq(10) }
41
+ end
42
+
43
+ context 'failed' do
44
+ before do
45
+ expect(faraday).to receive(:post).and_return(double(body: %Q{{"result" : 1, "code" : 2, "error" : "some error"}}))
46
+ end
47
+
48
+ it { expect{subject.perform}.to_not raise_error }
49
+ it { expect(subject.perform).to be_kind_of(AtomparkSmsApi::Response) }
50
+ it { expect(subject.perform.success?).to be_falsey }
51
+ it { expect(subject.perform.error).to eq("<2:1> some error") }
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe AtomparkSmsApi::Response do
4
+ subject { described_class.new(response) }
5
+
6
+ describe 'invalid' do
7
+ let(:response) { 'foo' }
8
+
9
+ it { expect{subject}.to raise_error(JSON::ParserError, "776: unexpected token at 'foo'") }
10
+ end
11
+
12
+ describe '#error' do
13
+ let(:response) { %Q{{"error" : "some error", "code" : "-1", "result" : 2}} }
14
+
15
+ it { expect{subject}.to_not raise_error }
16
+ it { expect(subject.error?).to be_truthy }
17
+ it { expect(subject.success?).to be_falsey }
18
+ it { expect(subject.error).to eq('<-1:2> some error') }
19
+
20
+ it { expect{subject.id}.to raise_error(RuntimeError, '<-1:2> some error') }
21
+ it { expect{subject.price}.to raise_error(RuntimeError, '<-1:2> some error') }
22
+ it { expect{subject.currency}.to raise_error(RuntimeError, '<-1:2> some error') }
23
+ end
24
+
25
+ describe '#success' do
26
+ let(:response) { %Q{{"result" : {"id" : 1, "price" : 2.5, "currency" : "USD"}}} }
27
+
28
+ it { expect{subject}.to_not raise_error }
29
+ it { expect(subject.error?).to be_falsey }
30
+ it { expect(subject.success?).to be_truthy }
31
+ it { expect(subject.id).to eq(1) }
32
+ it { expect(subject.price).to eq(2.5) }
33
+ it { expect(subject.currency).to eq("USD") }
34
+ end
35
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe AtomparkSmsApi::Send do
4
+ subject { described_class.perform(params) }
5
+ let(:faraday) { double }
6
+
7
+ before do
8
+ AtomparkSmsApi.configure do |c|
9
+ c.pubkey = 'X'
10
+ c.pvtkey = 'Y'
11
+ c.test = false
12
+ c.sender = 'Pupkin'
13
+ end
14
+ end
15
+
16
+ let(:params) { {phone: '000', text: 'xxx'} }
17
+
18
+ context 'correct gw call' do
19
+ before do
20
+ expect(described_class).to receive(:new)
21
+ .with(:sendSMS, {sender: 'Pupkin', datetime: '', sms_lifetime: 0, phone: '000', text: 'xxx'})
22
+ .and_return(double(perform: 'performed'))
23
+ end
24
+
25
+ it { expect{subject}.to_not raise_error }
26
+ it { expect(subject).to eq("performed") }
27
+ end
28
+
29
+ describe '#perform' do
30
+ before do
31
+ expect(Faraday).to receive(:new).with(url: AtomparkSmsApi.config.base_url).and_return(faraday)
32
+ expect(faraday).to receive(:post).and_return(double(body: response))
33
+ end
34
+
35
+ context 'success' do
36
+ let(:response) { %Q{{"result" : { "id" : 1 }}} }
37
+ it { expect{subject}.to_not raise_error }
38
+ it { expect(subject.success?).to be_truthy }
39
+ it { expect(subject.id).to eq(1) }
40
+ end
41
+
42
+ context 'failed' do
43
+ let(:response) { %Q{{"result" : 1, "code" : 2, "error" : "woops"}} }
44
+ it { expect{subject}.to_not raise_error }
45
+ it { expect(subject.success?).to be_falsey }
46
+ it { expect(subject.error).to eq("<2:1> woops") }
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atompark-sms-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - uno4ki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-12 00:00:00.000000000 Z
11
+ date: 2016-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,10 @@ files:
85
85
  - lib/atompark-sms-api/response.rb
86
86
  - lib/atompark-sms-api/send.rb
87
87
  - lib/atompark-sms-api/version.rb
88
+ - spec/configuration_spec.rb
89
+ - spec/gateway_spec.rb
90
+ - spec/response_spec.rb
91
+ - spec/send_spec.rb
88
92
  - spec/spec_helper.rb
89
93
  homepage: https://github.com/the-furnish/atompark-sms-api
90
94
  licenses:
@@ -111,4 +115,8 @@ signing_key:
111
115
  specification_version: 4
112
116
  summary: Atompark SMS API wrapper. Works only with v3 api
113
117
  test_files:
118
+ - spec/configuration_spec.rb
119
+ - spec/gateway_spec.rb
120
+ - spec/response_spec.rb
121
+ - spec/send_spec.rb
114
122
  - spec/spec_helper.rb