smster_ruby 0.0.3 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4bf257295149476bdbbc89ab56dda3ce9f3698a
4
- data.tar.gz: 058d3a0a585adddff39837fd5112e9ef4f33d85f
3
+ metadata.gz: f7cf21eba6341ca5a5c27688bc9736295b513c99
4
+ data.tar.gz: 6f8f7652396d06f93a8cde59b3a7f2d43cbcc964
5
5
  SHA512:
6
- metadata.gz: 1ae39080af8e389db2c832d5f33293d2a99c7e26de4a66e4eb4d9f5b1840f7a299255de869311540af167576671f6e2f9d0dc4aab5b00842303fcbaa205d99c5
7
- data.tar.gz: 511ea133420cad3a33575e7d655d92c113d802cfa0a0b1472dc8903daa18d1cef76963e688aeead1a5abc349c77c202841efac145e895373d227cf35da8849f1
6
+ metadata.gz: 79993bad5368ffc2e85c5e121be8d34d539dd24e0525219a4d0a4be04c98c3f22c02c321e6dbc353a74d1062001f446c5547346d7fb32a2ea2e38618a11ec816
7
+ data.tar.gz: 9a45848e62e5e6cdfdedb182663450fb01b3f843ffbb0af11c1d5da5f6e8528a7497327c1cdf57d07642be11f7f5c23439721bb0a098d27697343021e323ff29
@@ -0,0 +1,37 @@
1
+ class Sms::Clickatell < Sms
2
+ private
3
+
4
+ def modify_params
5
+ self.to = to.gsub(/\D/, '').to_s
6
+ self.text = text.tr(' ', '+')
7
+ end
8
+
9
+ def send_request
10
+ code = Smster.configuration.clickatell_authorization_code
11
+ msg_params = { 'text' => text, 'to' => [to] }.to_json
12
+
13
+ start_request(msg_params, code)
14
+ end
15
+
16
+ def start_request(params, code)
17
+ RestClient.post(
18
+ 'https://api.clickatell.com/rest/message',
19
+ params,
20
+ content_type: :json,
21
+ accept: :json,
22
+ 'X-Version' => 1,
23
+ 'Authorization' => "bearer #{code}"
24
+ )
25
+ rescue => e
26
+ e.response
27
+ end
28
+
29
+ def assign_attrs_by(response)
30
+ response = JSON.parse(response)
31
+
32
+ info = response['data']['message'][0]
33
+
34
+ self.status_message = info['error']
35
+ self.api_message_id = info['apiMessageId']
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ class Sms::Nexmo < Sms
2
+ private
3
+
4
+ def modify_params
5
+ self.to = to.gsub(/\D/, '').to_s
6
+ self.text = text.tr(' ', '+')
7
+ end
8
+
9
+ def send_request
10
+ config = Smster.configuration
11
+
12
+ RestClient.post(
13
+ 'https://rest.nexmo.com/sms/json',
14
+ text: text,
15
+ to: to,
16
+ content_type: :json,
17
+ from: name,
18
+ api_key: config.nexmo_key,
19
+ api_secret: config.nexmo_sekret
20
+ )
21
+ end
22
+
23
+ def assign_attrs_by(response)
24
+ json_response = JSON.parse(response)
25
+
26
+ self.status_message = json_response['messages'][0]['error-text']
27
+ self.api_message_id = json_response['messages'][0]['message-id']
28
+ end
29
+ end
@@ -1,14 +1,11 @@
1
1
  class Sms::Smsru < Sms
2
2
  private
3
3
 
4
- def send_to_provider
5
- phone = to.gsub(/\D/, '')
6
-
7
- response = send_request(text, phone)
8
- generate_send_resonse(response)
4
+ def modify_params
5
+ self.to = to.gsub(/\D/, '')
9
6
  end
10
7
 
11
- def send_request(text, phone)
8
+ def send_request
12
9
  config = Smster.configuration
13
10
  api_id = config.smsru_api_id
14
11
 
@@ -16,14 +13,14 @@ class Sms::Smsru < Sms
16
13
  'http://sms.ru/sms/send',
17
14
  'api_id' => api_id,
18
15
  'text' => text,
19
- 'to' => phone,
16
+ 'to' => to,
20
17
  'from' => name
21
18
  )
22
19
  end
23
20
 
24
- def generate_send_resonse(response)
21
+ def assign_attrs_by(response)
25
22
  return unless response.include?('100')
26
23
 
27
- (/\n(.*)\n/).match(response)[1]
24
+ self.api_message_id = (/\n(.*)\n/).match(response)[1]
28
25
  end
29
26
  end
data/lib/smster/sms.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  class Sms
2
- attr_accessor :name, :type, :to,
3
- :text, :status, :status_message
2
+ attr_accessor :name, :type, :api_message_id,
3
+ :text, :status, :status_message,
4
+ :to
4
5
 
5
6
  ## Codes
6
7
  STATUSES = { created: 0, sent: 1, delivered: 2, failed: 3 }
@@ -19,11 +20,20 @@ class Sms
19
20
  end
20
21
 
21
22
  def send_sms
22
- api_message_id = send_to_provider
23
+ send_to_provider
23
24
 
24
- self.status = STATUSES[:sent] if api_message_id
25
- self.status ||= STATUSES[:failed]
25
+ status_name = api_message_id ? :sent : :failed
26
+ self.status = STATUSES[status_name]
26
27
 
27
28
  self
28
29
  end
30
+
31
+ private
32
+
33
+ def send_to_provider
34
+ modify_params
35
+
36
+ response = send_request
37
+ assign_attrs_by(response)
38
+ end
29
39
  end
data/lib/smster.rb CHANGED
@@ -2,6 +2,8 @@ require 'rest_client'
2
2
  require 'smster/configuration'
3
3
  require 'smster/sms'
4
4
  require 'smster/sms/smsru'
5
+ require 'smster/sms/nexmo'
6
+ require 'smster/sms/clickatell'
5
7
 
6
8
  module Smster
7
9
  attr_accessor :configuration
@@ -0,0 +1,32 @@
1
+ require './test/test_helper'
2
+
3
+ class ClickatellTest < Minitest::Test
4
+ def test_should_send
5
+ sms = Sms::Clickatell.send_sms(to: @to, text: @text)
6
+
7
+ assert_equal @statuses[:sent], sms.status
8
+ end
9
+
10
+ def test_should_modify_to
11
+ to = '+abc' + @to
12
+
13
+ sms = Sms::Clickatell.send_sms(to: to, text: @text)
14
+
15
+ assert_equal to.gsub(/\D/, ''), sms.to
16
+ end
17
+
18
+ def test_should_modify_text
19
+ sms = Sms::Clickatell.send_sms(to: @to, text: @text)
20
+
21
+ assert_equal @text.tr(' ', '+'), sms.text
22
+ end
23
+
24
+ private
25
+
26
+ def stub_send_request
27
+ body = { data: { message: ['apiMessageId' => 15] } }.to_json
28
+
29
+ stub_request(:post, 'https://api.clickatell.com/rest/message')
30
+ .to_return(status: 200, body: body, headers: {})
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require './test/test_helper'
2
+
3
+ class NexmoTest < Minitest::Test
4
+ def test_should_send
5
+ sms = Sms::Nexmo.send_sms(to: @to, text: @text)
6
+
7
+ assert_equal @statuses[:sent], sms.status
8
+ end
9
+
10
+ def test_should_modify_to
11
+ to = '+abc' + @to
12
+
13
+ sms = Sms::Nexmo.send_sms(to: to, text: @text)
14
+
15
+ assert_equal to.gsub(/\D/, ''), sms.to
16
+ end
17
+
18
+ def test_should_modify_text
19
+ sms = Sms::Nexmo.send_sms(to: @to, text: @text)
20
+
21
+ assert_equal @text.tr(' ', '+'), sms.text
22
+ end
23
+
24
+ private
25
+
26
+ def stub_send_request
27
+ body = { messages: ['message-id' => 15] }.to_json
28
+
29
+ stub_request(:post, 'https://rest.nexmo.com/sms/json')
30
+ .to_return(status: 200, body: body, headers: {})
31
+ end
32
+ end
data/test/smsru_test.rb CHANGED
@@ -2,28 +2,26 @@ require './test/test_helper'
2
2
 
3
3
  class SmsruTest < Minitest::Test
4
4
  def test_should_send
5
- stub_send_request
5
+ sms = Sms::Smsru.send_sms(to: @to, text: @text)
6
6
 
7
- @statuses = Sms::STATUSES
8
- phone = (99_999_999 * rand).to_s
9
- attrs = { to: phone, text: 'i am smster!' }
7
+ assert_equal @statuses[:sent], sms.status
8
+ end
10
9
 
11
- sms = Sms::Smsru.send_sms(attrs)
10
+ def test_should_modify_to
11
+ to = 'a+bc' + @to
12
12
 
13
- assert_equal @statuses[:sent], sms.status
13
+ sms = Sms::Smsru.send_sms(to: to, text: @text)
14
+
15
+ result = to.gsub(/\D/, '')
16
+ assert_equal result, sms.to
14
17
  end
15
18
 
16
19
  private
17
20
 
18
21
  def stub_send_request
22
+ body = "100\n201523-1000007\nbalance=52.54"
23
+
19
24
  stub_request(:post, 'http://sms.ru/sms/send')
20
- .to_return(
21
- status: 200,
22
- body:
23
- '100
24
- 201523-1000007
25
- balance=52.54',
26
- headers: {}
27
- )
25
+ .to_return(status: 200, body: body, headers: {})
28
26
  end
29
27
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,13 @@
1
1
  require 'minitest/autorun'
2
2
  require 'webmock/minitest'
3
3
  require 'smster'
4
+
5
+ class MiniTest::Test
6
+ def setup
7
+ @statuses = Sms::STATUSES
8
+ @to = (99_999_999 * rand).to_i.to_s
9
+ @text = 'i am smster!'
10
+
11
+ stub_send_request
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smster_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - doniv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-01 00:00:00.000000000 Z
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: SMS sending service through different providers with maximum convenience.
14
14
  email:
@@ -21,7 +21,11 @@ files:
21
21
  - lib/smster.rb
22
22
  - lib/smster/configuration.rb
23
23
  - lib/smster/sms.rb
24
+ - lib/smster/sms/clickatell.rb
25
+ - lib/smster/sms/nexmo.rb
24
26
  - lib/smster/sms/smsru.rb
27
+ - test/clickatell_test.rb
28
+ - test/nexmo_test.rb
25
29
  - test/smsru_test.rb
26
30
  - test/test_helper.rb
27
31
  homepage: https://github.com/IlyaDonskikh/smster_ruby
@@ -49,5 +53,7 @@ signing_key:
49
53
  specification_version: 4
50
54
  summary: SMS service
51
55
  test_files:
56
+ - test/clickatell_test.rb
57
+ - test/nexmo_test.rb
52
58
  - test/smsru_test.rb
53
59
  - test/test_helper.rb