sms_broker 1.0.5 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/lib/open_market/client.rb +51 -0
  3. data/lib/open_market/sms/client.rb +25 -0
  4. data/lib/open_market/sms/requests/message_status.rb +25 -0
  5. data/lib/open_market/sms/requests/send_message.rb +56 -0
  6. data/lib/open_market/sms/responses/message_status.rb +80 -0
  7. data/lib/open_market/sms/responses/send_message.rb +75 -0
  8. data/lib/open_market.rb +8 -0
  9. data/lib/sms_broker/client/open_market.rb +53 -0
  10. data/lib/sms_broker/client/response/error.rb +1 -0
  11. data/lib/sms_broker/client/response/message_status_success.rb +43 -0
  12. data/lib/sms_broker/client/response/open_market/error.rb +25 -0
  13. data/lib/sms_broker/client/response/open_market/message_status_success.rb +31 -0
  14. data/lib/sms_broker/client/response/open_market/send_message_success.rb +35 -0
  15. data/lib/sms_broker/client/response/success.rb +2 -0
  16. data/lib/sms_broker/service.rb +8 -1
  17. data/lib/sms_broker/setup.rb +5 -1
  18. data/lib/sms_broker/status_requester.rb +40 -0
  19. data/lib/sms_broker/version.rb +1 -1
  20. data/lib/sms_broker.rb +2 -0
  21. data/sms_broker.gemspec +1 -0
  22. data/spec/open_market/client_spec.rb +105 -0
  23. data/spec/sms_broker/nexmo_voice_spec.rb +0 -2
  24. data/spec/sms_broker/open_market_spec.rb +133 -0
  25. data/spec/support/vcr_cassettes/open_market/message_status_not_found.yml +42 -0
  26. data/spec/support/vcr_cassettes/open_market/message_status_success.yml +42 -0
  27. data/spec/support/vcr_cassettes/open_market/message_status_unauthorized.yml +43 -0
  28. data/spec/support/vcr_cassettes/open_market/message_success.yml +43 -0
  29. data/spec/support/vcr_cassettes/open_market/message_unauthorized.yml +43 -0
  30. metadata +44 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de0fd0c33b437013e02a4ade7f46121eba90e819
4
- data.tar.gz: 12ae1bb6e541bebb950b316102ad90c226513291
3
+ metadata.gz: 0acc9a14c5a0467200ba73c2c3e1cd0f89fec0b4
4
+ data.tar.gz: 10f556f81b8e7cb032f572fc25457f17ac08268d
5
5
  SHA512:
6
- metadata.gz: 2b253d6f2cfaabf8d9eeb227bab1ac8038eabeede40091776fb317c1a7423879d4aa74d172e84a625148e89cf8c82bcad0e62ad440925cc05564d50b6e03e8f3
7
- data.tar.gz: 75c9a6c6e463d2d98b1893a0b9677c536b3cea8869c1859bba3964cc3822a4639a7a2a34e80aa1dfc823e0453749193ac5b98af69f9ff1c60b07408dd24765de
6
+ metadata.gz: '0915db6c9fd498ad8af471050428e9191bd317b642cb6586a23e8014000cc0e809f2db141af8f955ef7f79c264255e6a8708397935bfea47d1fe95b1e1bdb011'
7
+ data.tar.gz: b4a85f44034db87b1b6c685ee5f4221c1a8ac6ccf433c0c773d3a41ad8bc32c860275cb41854270dcdbe19375d3648cdf9a6fefe1dba77c00e0ff4b8c51caf94
@@ -0,0 +1,51 @@
1
+ require 'faraday'
2
+
3
+ module OpenMarket
4
+
5
+ class Client
6
+ attr_reader :client
7
+
8
+ HEADERS = {
9
+ 'Content-Type' => 'application/json',
10
+ 'Accept' => 'application/json'
11
+ }.freeze
12
+
13
+ def initialize(account_id:, account_password:) # For now these ones
14
+ @account_id = account_id
15
+ @account_password = account_password
16
+
17
+ @client = init_client
18
+ end
19
+
20
+ private
21
+
22
+ def init_client
23
+ ::Faraday.new(url: base_url).tap do |conn|
24
+ conn.basic_auth(@account_id, @account_password)
25
+
26
+ HEADERS.each do |name, value|
27
+ conn.headers[name] = value
28
+ end
29
+ end
30
+ end
31
+
32
+ def base_url
33
+ OpenMarket::API_URL
34
+ end
35
+
36
+ def send_request(request)
37
+ params = \
38
+ if !request.body.nil? && !request.body.empty?
39
+ request.body.to_json
40
+ end
41
+
42
+ client.send(
43
+ request.method,
44
+ request.url,
45
+ params
46
+ )
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'requests/send_message'
2
+ require_relative 'responses/send_message'
3
+ require_relative 'requests/message_status'
4
+ require_relative 'responses/message_status'
5
+
6
+ module OpenMarket
7
+ module SMS
8
+ class Client < OpenMarket::Client
9
+ BASE_URL ='sms/v4'.freeze
10
+
11
+ def send_message(from:, to:, text:)
12
+ request = Requests::SendMessage.new(from, to, text)
13
+
14
+ SMS::Responses::SendMessage.new(request, send_request(request))
15
+ end
16
+
17
+ def message_status(ticket_id:)
18
+ request = Requests::MessageStatus.new(ticket_id)
19
+
20
+ SMS::Responses::MessageStatus.new(request, send_request(request))
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module OpenMarket
2
+ module SMS
3
+ module Requests
4
+
5
+ class MessageStatus
6
+ attr_accessor :ticket_id
7
+
8
+ def initialize(ticket_id)
9
+ @ticket_id = ticket_id
10
+ end
11
+
12
+ def body;end
13
+
14
+ def url
15
+ "#{SMS::Client::BASE_URL}/mt/#{@ticket_id}/status"
16
+ end
17
+
18
+ def method
19
+ :get
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,56 @@
1
+ module OpenMarket
2
+ module SMS
3
+ module Requests
4
+
5
+ class SendMessage
6
+ attr_accessor :from, :to, :text
7
+
8
+ ALPHANUMERIC_TYPE_OF_NUMBER = 5
9
+
10
+ def initialize(from, to, text)
11
+ @from = from
12
+ @to = to
13
+ @text = text
14
+ end
15
+
16
+ def body
17
+ {
18
+ mobileTerminate: {}
19
+ .merge(message)
20
+ .merge(source)
21
+ .merge(destination)
22
+ }
23
+ end
24
+
25
+ def url
26
+ "#{SMS::Client::BASE_URL}/mt"
27
+ end
28
+
29
+ def method
30
+ :post
31
+ end
32
+
33
+ private
34
+
35
+ def message
36
+ {
37
+ message: {
38
+ content: @text,
39
+ type: 'text'
40
+ }
41
+ }
42
+ end
43
+
44
+ def destination
45
+ { destination: { address: @to } }
46
+ end
47
+
48
+ def source
49
+ { source: { ton: ALPHANUMERIC_TYPE_OF_NUMBER, address: @from } }
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,80 @@
1
+ module OpenMarket
2
+ module SMS
3
+ module Responses
4
+
5
+ class MessageStatus
6
+ attr_reader :status, :headers, :body
7
+
8
+ def initialize(request, service_response)
9
+ @request = request
10
+ @response = service_response
11
+
12
+ parse_response
13
+ end
14
+
15
+ def success?
16
+ status == 200
17
+ end
18
+
19
+ def ticket_id
20
+ @request.ticket_id
21
+ end
22
+
23
+ def message_status_code
24
+ @delivery_status.dig('code')&.to_i
25
+ end
26
+
27
+ def message_status
28
+ @delivery_status.dig('description')
29
+ end
30
+
31
+ def destination
32
+ @delivery_status.dig('destination', 'address')
33
+ end
34
+
35
+ def delivery_date
36
+ delivery_date = @delivery_status.dig('deliveryDate')
37
+
38
+ return if delivery_date.nil? || delivery_date.empty?
39
+
40
+ DateTime.parse(delivery_date)
41
+ end
42
+
43
+ def to_s
44
+ "Response: #{status}, #{error}"
45
+ end
46
+
47
+ def error_message
48
+ body.dig('error', 'description')
49
+ end
50
+
51
+ def error_code
52
+ body.dig('error', 'code')&.to_i
53
+ end
54
+
55
+ private
56
+
57
+ def parse_response
58
+ @status = @response.status
59
+
60
+ parse_body
61
+ parse_delivery_status
62
+ end
63
+
64
+ def parse_body
65
+ @body = \
66
+ if !@response.body || @response.body.empty?
67
+ {}
68
+ else
69
+ JSON.parse(@response.body)
70
+ end
71
+ end
72
+
73
+ def parse_delivery_status
74
+ @delivery_status = @body.dig('mtStatus')
75
+ end
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,75 @@
1
+ module OpenMarket
2
+ module SMS
3
+ module Responses
4
+
5
+ class SendMessage
6
+ attr_reader :message_id, :status, :headers, :body
7
+
8
+ def initialize(request, service_response)
9
+ @request = request
10
+ @response = service_response
11
+
12
+ parse_response
13
+ end
14
+
15
+ def success?
16
+ status == 202
17
+ end
18
+
19
+ def from
20
+ @request.from
21
+ end
22
+
23
+ def to
24
+ @request.to
25
+ end
26
+
27
+ def location
28
+ @response.headers['location']
29
+ end
30
+
31
+ def request_id
32
+ @response.headers['x-request-id']
33
+ end
34
+
35
+ def error_message
36
+ body.dig('error', 'description')
37
+ end
38
+
39
+ def error_code
40
+ body.dig('error', 'code')&.to_i
41
+ end
42
+
43
+ def to_s
44
+ "Response: #{status}, #{error}"
45
+ end
46
+
47
+ private
48
+
49
+ def parse_response
50
+ @status = @response.status
51
+
52
+ parse_message_id
53
+ parse_body
54
+ end
55
+
56
+ def parse_message_id
57
+ return unless success?
58
+
59
+ location = @response.headers['location']
60
+ *, @message_id = location.split('/')
61
+ end
62
+
63
+ def parse_body
64
+ @body = \
65
+ if !@response.body || @response.body.empty?
66
+ {}
67
+ else
68
+ JSON.parse(@response.body)
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,8 @@
1
+ require 'json'
2
+
3
+ require_relative 'open_market/client'
4
+ require_relative 'open_market/sms/client'
5
+
6
+ module OpenMarket
7
+ API_URL = 'https://smsc.openmarket.com'.freeze
8
+ end
@@ -0,0 +1,53 @@
1
+ module SmsBroker
2
+ module Client
3
+ class OpenMarket < Base
4
+ def initialize(options)
5
+ client = ::OpenMarket::SMS::Client.new(
6
+ account_id: options[:account_id],
7
+ account_password: options[:account_password]
8
+ )
9
+
10
+ super :open_market, client, options
11
+ end
12
+
13
+ def send_message(message)
14
+ response = client.send_message \
15
+ text: message[:text],
16
+ from: message[:from],
17
+ to: message[:to]
18
+
19
+ if success_response?(response)
20
+ Response::OpenMarket::SendMessageSuccess.new(response)
21
+ else
22
+ Response::OpenMarket::Error.new(response)
23
+ end
24
+ end
25
+
26
+ def send_voice_message(_message)
27
+ message = 'openmarket voice message is not implemented'
28
+ exception = \
29
+ Exceptions::NotImplemented.new(message)
30
+
31
+ raise exception
32
+ end
33
+
34
+ def message_status(params)
35
+ response = client.message_status \
36
+ ticket_id: params[:message_id]
37
+
38
+ if success_response?(response)
39
+ Response::OpenMarket::MessageStatusSuccess.new(response)
40
+ else
41
+ Response::OpenMarket::Error.new(response)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def success_response?(response)
48
+ response.success?
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -27,3 +27,4 @@ end
27
27
  require 'sms_broker/client/response/nexmo_error'
28
28
  require 'sms_broker/client/response/twilio_error'
29
29
  require 'sms_broker/client/response/nexmo_voice_error'
30
+ require 'sms_broker/client/response/open_market/error'
@@ -0,0 +1,43 @@
1
+ module SmsBroker
2
+ module Client
3
+ module Response
4
+ class MessageStatusSuccess
5
+ attr_reader :raw,
6
+ :service,
7
+ :serialized
8
+
9
+ def initialize(service, response, serialized)
10
+ @raw = response
11
+ @service = service
12
+ @serialized = serialized
13
+ end
14
+
15
+ def success?
16
+ true
17
+ end
18
+
19
+ def message_id
20
+ serialized[:message_id]
21
+ end
22
+
23
+ def to
24
+ serialized[:to]
25
+ end
26
+
27
+ def delivery_date
28
+ serialized[:delivery_date]
29
+ end
30
+
31
+ def status_code
32
+ serialized[:status_code]
33
+ end
34
+
35
+ def status
36
+ serialized[:status]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ require 'sms_broker/client/response/open_market/message_status_success'
@@ -0,0 +1,25 @@
1
+ module SmsBroker
2
+ module Client
3
+ module Response
4
+ module OpenMarket
5
+
6
+ class Error < Error
7
+ def initialize(open_market_response)
8
+ super :open_market, open_market_response, serialize_error_response(open_market_response)
9
+ end
10
+
11
+ private
12
+
13
+ def serialize_error_response(open_market_response)
14
+ {}.tap do |hash|
15
+ code = open_market_response.error_code
16
+ message = open_market_response.error_message
17
+
18
+ hash[code] = [message]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ module SmsBroker
2
+ module Client
3
+ module Response
4
+ module OpenMarket
5
+
6
+ class MessageStatusSuccess < MessageStatusSuccess
7
+ attr_reader :location, :request_id
8
+
9
+ def initialize(open_market_response)
10
+ super :open_market, open_market_response, serialize(open_market_response)
11
+
12
+ end
13
+
14
+ private
15
+
16
+ def serialize(response)
17
+ {
18
+ message_id: response.ticket_id,
19
+ to: response.destination,
20
+ delivery_date: response.delivery_date,
21
+ status_code: response.message_status_code,
22
+ status: response.message_status
23
+ }
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,35 @@
1
+ module SmsBroker
2
+ module Client
3
+ module Response
4
+ module OpenMarket
5
+
6
+ class SendMessageSuccess < Success
7
+ attr_reader :location, :request_id
8
+
9
+ def initialize(open_market_response)
10
+ super :open_market, open_market_response, serialize(open_market_response)
11
+
12
+ set_attributes(open_market_response)
13
+ end
14
+
15
+ private
16
+
17
+ def serialize(response)
18
+ {
19
+ to: response.to,
20
+ from: response.from,
21
+ message_id: response.message_id
22
+ }
23
+ end
24
+
25
+ def set_attributes(response)
26
+ @location = response.location
27
+ @request_id = response.request_id
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -34,3 +34,5 @@ end
34
34
 
35
35
  require 'sms_broker/client/response/nexmo_success'
36
36
  require 'sms_broker/client/response/twilio_success'
37
+ require 'sms_broker/client/response/open_market/send_message_success'
38
+
@@ -1,15 +1,18 @@
1
1
  require 'sms_broker/message_sender'
2
2
  require 'sms_broker/voice_message_sender'
3
+ require 'sms_broker/status_requester'
3
4
  require 'sms_broker/client/base'
4
5
  require 'sms_broker/client/nexmo'
5
6
  require 'sms_broker/client/twilio'
7
+ require 'sms_broker/client/open_market'
6
8
  require 'sms_broker/exceptions/invalid_service'
7
9
  require 'sms_broker/exceptions/not_implemented'
8
10
 
9
11
  module SmsBroker
10
12
  CLIENTS = {
11
13
  nexmo: Client::Nexmo,
12
- twilio: Client::Twilio
14
+ twilio: Client::Twilio,
15
+ open_market: Client::OpenMarket
13
16
  }.freeze
14
17
 
15
18
  class Service
@@ -46,5 +49,9 @@ module SmsBroker
46
49
  def voice_message(message) #could have another params like lang
47
50
  VoiceMessageSender.new(client).message(message)
48
51
  end
52
+
53
+ def status(message_id)
54
+ StatusRequester.new(client).status(message_id)
55
+ end
49
56
  end
50
57
  end
@@ -16,6 +16,11 @@ module SmsBroker
16
16
  auth_token: Compel.string.required,
17
17
  account_sid: Compel.string.required,
18
18
  phone_number: Compel.string.required
19
+ ),
20
+ open_market: Compel.hash.keys(
21
+ account_id: Compel.string.required,
22
+ account_password: Compel.string.required,
23
+ sender_id: Compel.string
19
24
  )
20
25
  }
21
26
  end
@@ -70,7 +75,6 @@ module SmsBroker
70
75
  @options[:services_setups][service.to_sym] = args
71
76
  @options[:services_setups]
72
77
  else
73
-
74
78
  super
75
79
  end
76
80
  end
@@ -0,0 +1,40 @@
1
+ module SmsBroker
2
+ class StatusRequester
3
+ attr_reader :client,
4
+ :errors
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def status(message_id)
11
+ @message_id = message_id
12
+
13
+ self
14
+ end
15
+
16
+ def request
17
+ unless valid?
18
+ return Client::Response::Error.new(client.name, errors, errors)
19
+ end
20
+
21
+ client.message_status(build_request)
22
+ end
23
+
24
+ def valid?
25
+ schema = { message_id: Compel.string.required }
26
+
27
+ result = Compel.hash.keys(schema).validate(build_request)
28
+
29
+ @errors = result.errors
30
+
31
+ result.valid?
32
+ end
33
+
34
+ private
35
+
36
+ def build_request
37
+ { message_id: @message_id }
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module SmsBroker
2
- VERSION = '1.0.5'.freeze
2
+ VERSION = '1.0.7'.freeze
3
3
  end
data/lib/sms_broker.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'nexmo'
2
2
  require 'compel'
3
3
  require 'twilio-ruby'
4
+ require 'open_market'
4
5
  require 'sms_broker/client/response/error'
5
6
  require 'sms_broker/client/response/success'
6
7
  require 'sms_broker/client/response/voice_success'
8
+ require 'sms_broker/client/response/message_status_success'
7
9
  require 'sms_broker/configuration'
8
10
  require 'sms_broker/service'
9
11
 
data/sms_broker.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
21
21
  gem.add_runtime_dependency 'nexmo', '~> 4.2'
22
22
  gem.add_runtime_dependency 'compel', '~> 0.5'
23
23
  gem.add_runtime_dependency 'twilio-ruby', '~> 4.11'
24
+ gem.add_runtime_dependency 'faraday', '~> 0.13.1'
24
25
 
25
26
  gem.add_development_dependency 'webmock', '~> 2.0'
26
27
  gem.add_development_dependency 'rspec', '~> 3.2'
@@ -0,0 +1,105 @@
1
+ describe OpenMarket do
2
+ context 'Client' do
3
+ let(:id) { '123' }
4
+ let(:password) { '456' }
5
+ let(:text) { 'Hello OpenMarket World!' }
6
+ let(:from) { 'streetbees' }
7
+ let(:to) { '918000001' }
8
+
9
+ let(:client) {
10
+ ::OpenMarket::SMS::Client.new(
11
+ account_id: id,
12
+ account_password: password
13
+ )
14
+ }
15
+
16
+ context 'sending a text message' do
17
+ let(:response) {
18
+ client.send_message(
19
+ from: from,
20
+ to: to,
21
+ text: text
22
+ )
23
+ }
24
+
25
+ context 'with success' do
26
+
27
+ it 'should return success == true' do
28
+ VCR.use_cassette('open_market/message_success') do
29
+ expect(response.success?).to eq(true)
30
+ expect(response.status).to eq(202)
31
+ expect(response.message_id).to eq('2618Z-03021-1603K-03FDL')
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ context 'invalid auth' do
38
+ it 'should return success == false' do
39
+ VCR.use_cassette('open_market/message_unauthorized') do
40
+ expect(response.success?).to eq(false)
41
+ expect(response.status).to eq(401)
42
+
43
+ expect(response.error_code).to eq(420)
44
+ expect(response.error_message).not_to be(nil)
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ context 'requesting a delivery status' do
52
+ let(:ticket_id) { '2218Z-0318E-2225A-4999C' }
53
+
54
+ let(:response) { client.message_status(ticket_id: ticket_id) }
55
+
56
+ context 'with success' do
57
+ let(:expected_delivery_date) {
58
+ DateTime.parse('2018-03-19T14:56:05Z')
59
+ }
60
+
61
+ let(:expected_message_status) {
62
+ 'Message successfully delivered.'
63
+ }
64
+
65
+ it 'should return success == true' do
66
+ VCR.use_cassette('open_market/message_status_success') do
67
+ expect(response.success?).to eq(true)
68
+ expect(response.status).to eq(200)
69
+
70
+ expect(response.destination).to eq('447736067864')
71
+ expect(response.delivery_date).to eq(expected_delivery_date)
72
+ expect(response.message_status_code).to eq(4)
73
+ expect(response.message_status).to eq('Message successfully delivered.')
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'invalid auth' do
79
+ it 'should return success == false' do
80
+ VCR.use_cassette('open_market/message_status_unauthorized') do
81
+ expect(response.success?).to eq(false)
82
+ expect(response.status).to eq(401)
83
+
84
+ expect(response.error_code).to eq(420)
85
+ expect(response.error_message).not_to be(nil)
86
+ end
87
+ end
88
+ end
89
+
90
+ context 'not found' do
91
+ let(:ticket_id) { 'fake' }
92
+
93
+ it 'should return success == false' do
94
+ VCR.use_cassette('open_market/message_status_not_found') do
95
+ expect(response.success?).to eq(false)
96
+ expect(response.status).to eq(404)
97
+
98
+ expect(response.error_code).to eq(711)
99
+ expect(response.error_message).not_to be(nil)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -1,5 +1,3 @@
1
- require 'pry'
2
-
3
1
  describe SmsBroker do
4
2
  context 'Nexmo', :focus do
5
3
  context '#send_message' do
@@ -0,0 +1,133 @@
1
+ describe SmsBroker do
2
+ context 'OpenMarket' do
3
+ context '#send_message' do
4
+ let(:provider) { 'open_market' }
5
+
6
+ let(:account_id) { '132'}
7
+ let(:account_password) { '456' }
8
+ let(:text_message) { 'Hello World' }
9
+ let(:sender_id) { 'SenderID' }
10
+ let(:to_phone) { '44000000000' }
11
+
12
+ before(:each) do
13
+ SmsBroker.clear_setup
14
+
15
+ setup
16
+ end
17
+
18
+ let(:service) do
19
+ SmsBroker.service(provider)
20
+ end
21
+
22
+ let(:setup) {
23
+ SmsBroker.setup do |config|
24
+ config.services [provider]
25
+ config.default_service provider
26
+ config.open_market_setup \
27
+ account_id: account_id,
28
+ account_password: account_password,
29
+ sender_id: sender_id
30
+ end
31
+ }
32
+
33
+ context 'valid' do
34
+ it 'should send message with success' do
35
+ setup
36
+
37
+ VCR.use_cassette('open_market/message_success') do
38
+ response = service.message(text_message).to(to_phone).deliver
39
+
40
+ expect(response.service).to eq(:open_market)
41
+ expect(response.success?).to eq(true)
42
+ expect(response.message_id).not_to eq(nil)
43
+ expect(response.location).not_to eq(nil)
44
+ expect(response.request_id).not_to eq(nil)
45
+
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ context 'invalid' do
52
+ it 'should return the error' do
53
+ setup
54
+
55
+ VCR.use_cassette('open_market/message_unauthorized') do
56
+ response = service.message(text_message).to(to_phone).deliver
57
+
58
+ expect(response.service).to eq(:open_market)
59
+ expect(response.success?).to eq(false)
60
+ expect(response.serialized.length).to be > 0
61
+ end
62
+
63
+ end
64
+ end
65
+ end
66
+
67
+ context '#delivery_status' do
68
+ let(:provider) { 'open_market' }
69
+
70
+ let(:account_id) { '132'}
71
+ let(:account_password) { '456' }
72
+ let(:sender_id) { 'SenderID' }
73
+
74
+ let(:message_id) { '2218Z-0318E-2225A-4999C' }
75
+
76
+ before(:each) do
77
+ SmsBroker.clear_setup
78
+
79
+ setup
80
+ end
81
+
82
+ let(:service) do
83
+ SmsBroker.service(provider)
84
+ end
85
+
86
+ let(:setup) {
87
+ SmsBroker.setup do |config|
88
+ config.services [provider]
89
+ config.default_service provider
90
+ config.open_market_setup \
91
+ account_id: account_id,
92
+ account_password: account_password,
93
+ sender_id: sender_id
94
+ end
95
+ }
96
+
97
+ context 'valid message id' do
98
+ it 'should return the status of the message with success' do
99
+ setup
100
+
101
+ VCR.use_cassette('open_market/message_status_success') do
102
+ response = service.status(message_id).request
103
+
104
+ expect(response.service).to eq(:open_market)
105
+ expect(response.success?).to eq(true)
106
+ expect(response.message_id).not_to eq(nil)
107
+ expect(response.to).not_to eq(nil)
108
+ expect(response.delivery_date).not_to eq(nil)
109
+ expect(response.status_code).not_to eq(nil)
110
+ expect(response.status).not_to eq(nil)
111
+ end
112
+ end
113
+ end
114
+
115
+ context 'invalid message id' do
116
+ let(:message_id) { 'fake' }
117
+
118
+ it 'should return the error' do
119
+ setup
120
+
121
+ VCR.use_cassette('open_market/message_status_not_found') do
122
+ response = service.status(message_id).request
123
+
124
+ expect(response.service).to eq(:open_market)
125
+ expect(response.success?).to eq(false)
126
+ expect(response.serialized.length).to be > 0
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ end
133
+ end
@@ -0,0 +1,42 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://smsc.openmarket.com/sms/v4/mt/fake/status
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.14.0
12
+ Authorization:
13
+ - Basic BOGUS
14
+ Content-Type:
15
+ - application/json
16
+ Accept:
17
+ - application/json
18
+ Accept-Encoding:
19
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
20
+ response:
21
+ status:
22
+ code: 404
23
+ message: Not Found
24
+ headers:
25
+ X-Request-Id:
26
+ - 001-23-C54D743B-8FFF-4800-AD8C-4F24DF11916B
27
+ Content-Type:
28
+ - application/json;charset=UTF-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Date:
32
+ - Mon, 19 Mar 2018 16:56:01 GMT
33
+ Server:
34
+ - OpenMarket Server
35
+ Vary:
36
+ - Accept-encoding
37
+ body:
38
+ encoding: ASCII-8BIT
39
+ string: '{"error":{"code":"711","description":"Ticket ID not found"}}'
40
+ http_version:
41
+ recorded_at: Mon, 19 Mar 2018 16:56:01 GMT
42
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,42 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://smsc.openmarket.com/sms/v4/mt/2218Z-0318E-2225A-4999C/status
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.14.0
12
+ Authorization:
13
+ - Basic BOGUS
14
+ Content-Type:
15
+ - application/json
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ Accept:
19
+ - "*/*"
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ X-Request-Id:
26
+ - 001-75-8C72DDB1-86B9-4C92-8C23-E14652782FDA
27
+ Content-Type:
28
+ - application/json;charset=UTF-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Date:
32
+ - Sun, 18 Mar 2018 22:58:06 GMT
33
+ Server:
34
+ - OpenMarket Server
35
+ Vary:
36
+ - Accept-encoding
37
+ body:
38
+ encoding: ASCII-8BIT
39
+ string: '{"mtStatus":{"destination":{"address":"447736067864","alpha2Code":"GB"},"source":{"ton":5,"address":"streetbee"},"ticketId":"7418Z-0319N-14559-597DT","deliveryDate":"2018-03-19T14:56:05Z","code":"4","description":"Message successfully delivered."}}'
40
+ http_version:
41
+ recorded_at: Sun, 18 Mar 2018 22:58:06 GMT
42
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,43 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://smsc.openmarket.com/sms/v4/mt/2218Z-0318E-2225A-4999C/status
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.14.0
12
+ Authorization:
13
+ - Basic BOGUS
14
+ Content-Type:
15
+ - application/json
16
+ Accept:
17
+ - application/json
18
+ Accept-Encoding:
19
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
20
+ response:
21
+ status:
22
+ code: 401
23
+ message: Unauthorized
24
+ headers:
25
+ X-Request-Id:
26
+ - 001-75-8F7BA133-4D88-43F9-B513-31AEF4DDB58D
27
+ Content-Type:
28
+ - application/json;charset=ISO-8859-1
29
+ Content-Length:
30
+ - '80'
31
+ Date:
32
+ - Mon, 19 Mar 2018 16:31:17 GMT
33
+ Server:
34
+ - OpenMarket Server
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"error":{"code":"420","description":"Invalid account ID or account
38
+ password"}}
39
+
40
+ '
41
+ http_version:
42
+ recorded_at: Mon, 19 Mar 2018 16:31:19 GMT
43
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,43 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://smsc.openmarket.com/sms/v4/mt
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"mobileTerminate":{"message":{"content":"Sorry about that, testing
9
+ OpenMarket Integration","type":"text"},"source":{"ton":5,"address":"streetbee"},"destination":{"address":"447736067864"}}}'
10
+ headers:
11
+ User-Agent:
12
+ - Faraday v0.14.0
13
+ Authorization:
14
+ - Basic BOGUS
15
+ Content-Type:
16
+ - application/json
17
+ Accept-Encoding:
18
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
19
+ Accept:
20
+ - "*/*"
21
+ response:
22
+ status:
23
+ code: 202
24
+ message: Accepted
25
+ headers:
26
+ X-Request-Id:
27
+ - 001-26-F18CEC32-0325-4FA6-B1DD-0BEA1C67D627
28
+ Location:
29
+ - http://smsc.openmarket.com/sms/v4/mt/2618Z-03021-1603K-03FDL
30
+ Content-Type:
31
+ - application/json;charset=UTF-8
32
+ Content-Length:
33
+ - '0'
34
+ Date:
35
+ - Fri, 02 Mar 2018 16:03:03 GMT
36
+ Server:
37
+ - OpenMarket Server
38
+ body:
39
+ encoding: UTF-8
40
+ string: ''
41
+ http_version:
42
+ recorded_at: Fri, 02 Mar 2018 16:03:03 GMT
43
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,43 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://smsc.openmarket.com/sms/v4/mt
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"mobileTerminate":{"message":{"content":"Hello OpenMarket World!","type":"text"},"destination":{"address":"918000001"}}}'
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.14.0
12
+ Authorization:
13
+ - Basic BOGUS
14
+ Content-Type:
15
+ - application/json
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ Accept:
19
+ - "*/*"
20
+ response:
21
+ status:
22
+ code: 401
23
+ message: Unauthorized
24
+ headers:
25
+ X-Request-Id:
26
+ - 001-73-8A621A7C-B7AC-495A-A4F3-3363E01B27C7
27
+ Content-Type:
28
+ - application/json;charset=ISO-8859-1
29
+ Content-Length:
30
+ - '80'
31
+ Date:
32
+ - Wed, 28 Feb 2018 16:27:59 GMT
33
+ Server:
34
+ - OpenMarket Server
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"error":{"code":"420","description":"Invalid account ID or account
38
+ password"}}
39
+
40
+ '
41
+ http_version:
42
+ recorded_at: Wed, 28 Feb 2018 16:27:59 GMT
43
+ recorded_with: VCR 4.0.0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sms_broker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Streetbees Dev Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-03 00:00:00.000000000 Z
11
+ date: 2018-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nexmo
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '4.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.1
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: webmock
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -122,14 +136,26 @@ files:
122
136
  - Gemfile
123
137
  - README.md
124
138
  - Rakefile
139
+ - lib/open_market.rb
140
+ - lib/open_market/client.rb
141
+ - lib/open_market/sms/client.rb
142
+ - lib/open_market/sms/requests/message_status.rb
143
+ - lib/open_market/sms/requests/send_message.rb
144
+ - lib/open_market/sms/responses/message_status.rb
145
+ - lib/open_market/sms/responses/send_message.rb
125
146
  - lib/sms_broker.rb
126
147
  - lib/sms_broker/client/base.rb
127
148
  - lib/sms_broker/client/nexmo.rb
149
+ - lib/sms_broker/client/open_market.rb
128
150
  - lib/sms_broker/client/response/error.rb
151
+ - lib/sms_broker/client/response/message_status_success.rb
129
152
  - lib/sms_broker/client/response/nexmo_error.rb
130
153
  - lib/sms_broker/client/response/nexmo_success.rb
131
154
  - lib/sms_broker/client/response/nexmo_voice_error.rb
132
155
  - lib/sms_broker/client/response/nexmo_voice_success.rb
156
+ - lib/sms_broker/client/response/open_market/error.rb
157
+ - lib/sms_broker/client/response/open_market/message_status_success.rb
158
+ - lib/sms_broker/client/response/open_market/send_message_success.rb
133
159
  - lib/sms_broker/client/response/success.rb
134
160
  - lib/sms_broker/client/response/twilio_error.rb
135
161
  - lib/sms_broker/client/response/twilio_success.rb
@@ -142,17 +168,25 @@ files:
142
168
  - lib/sms_broker/message_sender.rb
143
169
  - lib/sms_broker/service.rb
144
170
  - lib/sms_broker/setup.rb
171
+ - lib/sms_broker/status_requester.rb
145
172
  - lib/sms_broker/version.rb
146
173
  - lib/sms_broker/voice_message_sender.rb
147
174
  - sms_broker.gemspec
175
+ - spec/open_market/client_spec.rb
148
176
  - spec/sms_broker/nexmo_spec.rb
149
177
  - spec/sms_broker/nexmo_voice_spec.rb
178
+ - spec/sms_broker/open_market_spec.rb
150
179
  - spec/sms_broker/real_call_spec.rb
151
180
  - spec/sms_broker/setup_spec.rb
152
181
  - spec/sms_broker/sms_broker_spec.rb
153
182
  - spec/sms_broker/twilio_spec.rb
154
183
  - spec/spec_helper.rb
155
184
  - spec/support/nexmo_helpers.rb
185
+ - spec/support/vcr_cassettes/open_market/message_status_not_found.yml
186
+ - spec/support/vcr_cassettes/open_market/message_status_success.yml
187
+ - spec/support/vcr_cassettes/open_market/message_status_unauthorized.yml
188
+ - spec/support/vcr_cassettes/open_market/message_success.yml
189
+ - spec/support/vcr_cassettes/open_market/message_unauthorized.yml
156
190
  - spec/support/vcr_cassettes/twilio/create_invalid_phone_error.yml
157
191
  - spec/support/vcr_cassettes/twilio/create_invalid_phone_error_21612.yml
158
192
  - spec/support/vcr_cassettes/twilio/create_success.yml
@@ -175,19 +209,26 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
209
  version: '0'
176
210
  requirements: []
177
211
  rubyforge_project:
178
- rubygems_version: 2.6.10
212
+ rubygems_version: 2.6.8
179
213
  signing_key:
180
214
  specification_version: 4
181
215
  summary: Sms Broker
182
216
  test_files:
217
+ - spec/open_market/client_spec.rb
183
218
  - spec/sms_broker/nexmo_spec.rb
184
219
  - spec/sms_broker/nexmo_voice_spec.rb
220
+ - spec/sms_broker/open_market_spec.rb
185
221
  - spec/sms_broker/real_call_spec.rb
186
222
  - spec/sms_broker/setup_spec.rb
187
223
  - spec/sms_broker/sms_broker_spec.rb
188
224
  - spec/sms_broker/twilio_spec.rb
189
225
  - spec/spec_helper.rb
190
226
  - spec/support/nexmo_helpers.rb
227
+ - spec/support/vcr_cassettes/open_market/message_status_not_found.yml
228
+ - spec/support/vcr_cassettes/open_market/message_status_success.yml
229
+ - spec/support/vcr_cassettes/open_market/message_status_unauthorized.yml
230
+ - spec/support/vcr_cassettes/open_market/message_success.yml
231
+ - spec/support/vcr_cassettes/open_market/message_unauthorized.yml
191
232
  - spec/support/vcr_cassettes/twilio/create_invalid_phone_error.yml
192
233
  - spec/support/vcr_cassettes/twilio/create_invalid_phone_error_21612.yml
193
234
  - spec/support/vcr_cassettes/twilio/create_success.yml