smster 0.0.3 → 0.5.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.
@@ -2,20 +2,26 @@ require 'test_helper'
2
2
 
3
3
  class Sms::ClickatellTest < ActiveSupport::TestCase
4
4
  def setup
5
- @text = "simple text"
6
- @number = (0...7).map { (1..9).to_a.sample }.join
7
5
  @provider = Sms::Clickatell
6
+
7
+ @to = (99_999_999 * rand).to_i.to_s
8
+ @text = 'i am rails smster!'
9
+
10
+ stub_send_request
8
11
  end
9
12
 
10
- test "create" do
11
- sms = @provider.create(text: @text, to: @number)
13
+ test 'create' do
14
+ sms = @provider.create(text: @text, to: @to)
15
+
12
16
  assert_equal false, sms.new_record?
13
17
  end
14
18
 
15
- test "format to" do
16
- to = "+#{@number}"
17
- sms = @provider.create(text: @text, to: to)
19
+ private
18
20
 
19
- assert_equal to, sms.to
20
- end
21
- end
21
+ def stub_send_request
22
+ body = { data: { message: ['apiMessageId' => 15] } }.to_json
23
+
24
+ stub_request(:post, 'https://api.clickatell.com/rest/message')
25
+ .to_return(status: 200, body: body, headers: {})
26
+ end
27
+ end
@@ -2,21 +2,26 @@ require 'test_helper'
2
2
 
3
3
  class Sms::NexmoTest < ActiveSupport::TestCase
4
4
  def setup
5
- @text = "simple text"
6
- @number = (0...7).map { (1..9).to_a.sample }.join
7
5
  @provider = Sms::Nexmo
6
+
7
+ @to = (99_999_999 * rand).to_i.to_s
8
+ @text = 'i am rails smster!'
9
+
10
+ stub_send_request
8
11
  end
9
12
 
10
- test "create" do
11
- sms = @provider.create(text: @text, to: @number)
13
+ test 'create' do
14
+ sms = @provider.create(text: @text, to: @to)
12
15
 
13
16
  assert_equal false, sms.new_record?
14
17
  end
15
18
 
16
- test "format to" do
17
- to = "+#{@number}"
18
- sms = @provider.create(text: @text, to: to)
19
+ private
19
20
 
20
- assert_equal to, sms.to
21
- end
22
- end
21
+ def stub_send_request
22
+ body = { messages: ['message-id' => 15] }.to_json
23
+
24
+ stub_request(:post, 'https://rest.nexmo.com/sms/json')
25
+ .to_return(status: 200, body: body, headers: {})
26
+ end
27
+ end
@@ -2,21 +2,24 @@ require 'test_helper'
2
2
 
3
3
  class Sms::SmsRuTest < ActiveSupport::TestCase
4
4
  def setup
5
- @text = "simple text"
6
- @number = (0...7).map { (1..9).to_a.sample }.join
7
5
  @provider = Sms::Smsru
6
+
7
+ set_sms_params
8
+ stub_send_request
8
9
  end
9
10
 
10
- test "create" do
11
- sms = @provider.create(text: @text, to: @number)
11
+ test 'create' do
12
+ sms = @provider.create(text: @text, to: @to)
12
13
 
13
14
  assert_equal false, sms.new_record?
14
15
  end
15
16
 
16
- test "format to" do
17
- to = "+#{@number}"
18
- sms = @provider.create(text: @text, to: to)
17
+ private
19
18
 
20
- assert_equal to, sms.to
21
- end
22
- end
19
+ def stub_send_request
20
+ body = "100\n201523-1000007\nbalance=52.54"
21
+
22
+ stub_request(:post, 'http://sms.ru/sms/send')
23
+ .to_return(status: 200, body: body, headers: {})
24
+ end
25
+ end
@@ -1,8 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class SmsterTest < ActiveSupport::TestCase
4
- test "confirguration" do
5
- nexmo_key = "123"
4
+ test 'confirguration' do
5
+ nexmo_key = '123'
6
6
 
7
7
  Smster.configure do |config|
8
8
  config.nexmo_key = nexmo_key
@@ -1,19 +1,26 @@
1
1
  # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
2
+ ENV["RAILS_ENV"] = 'test'
3
3
 
4
- require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
- ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
6
- require "rails/test_help"
4
+ require File.expand_path('../../test/dummy/config/environment.rb', __FILE__)
5
+ ActiveRecord::Migrator.migrations_paths = [File.expand_path('../../test/dummy/db/migrate', __FILE__)]
6
+
7
+ require 'rails/test_help'
8
+ require 'webmock/minitest'
7
9
 
8
10
  # Filter out Minitest backtrace while allowing backtrace from other libraries
9
11
  # to be shown.
10
12
  Minitest.backtrace_filter = Minitest::BacktraceFilter.new
11
13
 
12
14
  # Load support files
13
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
+ Dir['#{File.dirname(__FILE__)}/support/**/*.rb'].each { |f| require f }
14
16
 
15
17
  # Load fixtures from the engine
16
18
  if ActiveSupport::TestCase.respond_to?(:fixture_path=)
17
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
19
+ ActiveSupport::TestCase.fixture_path = File.expand_path('../fixtures', __FILE__)
18
20
  ActiveSupport::TestCase.fixtures :all
19
21
  end
22
+
23
+ def set_sms_params
24
+ @to = (99_999_999 * rand).to_i.to_s
25
+ @text = 'i am rails smster!'
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.5.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-04-01 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: smster_ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: sqlite3
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -69,11 +83,7 @@ files:
69
83
  - lib/generators/smster/install/templates/smster.rb
70
84
  - lib/generators/smster/install_generator.rb
71
85
  - lib/smster.rb
72
- - lib/smster/configuration.rb
73
86
  - lib/smster/sms.rb
74
- - lib/smster/sms/clickatell.rb
75
- - lib/smster/sms/nexmo.rb
76
- - lib/smster/sms/smsru.rb
77
87
  - lib/smster/version.rb
78
88
  - lib/tasks/smster_tasks.rake
79
89
  - test/controller/smster/clickatell_controller_test.rb
@@ -1,12 +0,0 @@
1
- module Smster
2
- class Configuration
3
- attr_accessor :nexmo_key, :nexmo_sekret, :clickatell_authorization_code, :smsru_api_id
4
-
5
- def initialize
6
- @nexmo_key = ""
7
- @nexmo_sekret = ""
8
- @clickatell_authorization_code = ""
9
- @smsru_api_id = ""
10
- end
11
- end
12
- end
@@ -1,34 +0,0 @@
1
- class Sms::Clickatell < Sms
2
- def send_sms
3
- config = Smster.configuration
4
- authorization_code = config.clickatell_authorization_code
5
-
6
- text = self.text.tr(" ", "+")
7
- phone = to.gsub(/\D/, '')
8
-
9
- api_message_id = if self.mode == 'test'
10
- logger.debug("Mode: #{mode}. To: #{phone}, text: #{text}")
11
- self.id
12
- else
13
- response = RestClient.post('https://api.clickatell.com/rest/message',
14
- { "text" => text, "to" => [phone.to_s] }.to_json,
15
- :content_type => :json,
16
- :accept => :json,
17
- 'X-Version' => 1,
18
- 'Authorization' => "bearer #{authorization_code}")
19
- response = JSON.parse(response)
20
- response['data']['message'][0]['apiMessageId']
21
- end
22
-
23
- if api_message_id.present?
24
- self.code = api_message_id
25
- self.status = STATUS_CODES[:sent]
26
- self.save
27
- end
28
- rescue => e
29
- logger.debug("Error #{e}")
30
- self.status = STATUS_CODES[:failed]
31
- self.status_message = e.to_s
32
- self.save
33
- end
34
- end
@@ -1,43 +0,0 @@
1
- class Sms::Nexmo < Sms
2
- def send_sms
3
- config = Smster.configuration
4
- api_key = config.nexmo_key
5
- api_secret = config.nexmo_sekret
6
-
7
- text = self.text.tr(" ", "+")
8
- phone = to.gsub(/\D/, '')
9
- current_status = STATUS_CODES[:sent]
10
-
11
- api_message_id = if self.mode == 'test'
12
- logger.debug("Mode: #{mode}. To: #{phone}, text: #{text}")
13
- self.id
14
- else
15
- response = RestClient.post('https://rest.nexmo.com/sms/json',
16
- "text" => text,
17
- "to" => phone.to_s,
18
- :content_type => :json,
19
- :from => 'OnlinePay',
20
- :api_key => api_key,
21
- :api_secret => api_secret)
22
-
23
- json_response = JSON.parse(response)
24
-
25
- error_text = json_response['messages'][0]['error-text']
26
- raise error_text if error_text
27
-
28
- json_response['messages'][0]['message-id']
29
- end
30
-
31
- if api_message_id.present?
32
- self.code = api_message_id
33
- self.status = STATUS_CODES[:sent]
34
- end
35
-
36
- self.save
37
- rescue => e
38
- logger.debug("Error #{e}")
39
- self.status = STATUS_CODES[:failed]
40
- self.status_message = e.to_s
41
- self.save
42
- end
43
- end
@@ -1,40 +0,0 @@
1
- class Sms::Smsru < Sms
2
- def send_sms
3
- config = Smster.configuration
4
- api_id = config.smsru_api_id
5
-
6
- text = self.text.tr(" ", "+")
7
- phone = to.gsub(/\D/, '')
8
-
9
- api_message_id = if self.mode == 'test'
10
- logger.debug("Mode: #{mode}. To: #{phone}, text: #{text}")
11
- self.id
12
- else
13
- response = RestClient.post('http://sms.ru/sms/send',
14
- "api_id" => api_id,
15
- "text" => text,
16
- "to" => phone.to_s,
17
- "from" => name
18
- )
19
-
20
- case response.include?('100')
21
- when true then result = (/\n(.*)\n/).match(response)[1]
22
- else raise response
23
- end
24
-
25
- result
26
- end
27
-
28
- if api_message_id.present?
29
- self.code = api_message_id
30
- self.status = STATUS_CODES[:sent]
31
- end
32
-
33
- self.save
34
- rescue => e
35
- logger.debug("Error #{e}")
36
- self.status = STATUS_CODES[:failed]
37
- self.status_message = e.to_s
38
- self.save
39
- end
40
- end