infobip-sms-api-ruby 0.0.1 → 0.0.2

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: 7b43de03c663f60a25eb989911f0f548fc26ed4c
4
- data.tar.gz: feacbdb85e2e9d47d7f828f02f3da9de30967c3c
3
+ metadata.gz: cb72709a2d68d2a67e06aef08e15ccd66628162a
4
+ data.tar.gz: 67ffd78696bcd9c9172fe51e75516d2b818d8ecb
5
5
  SHA512:
6
- metadata.gz: 1739314c789b282392845ca7dd4db45101f450e256880f492a0bd4c321ab34a366f795f25aaf2e37f956b83757bb360485f3545b8e4ccc3a4f0d82a08ec44ae7
7
- data.tar.gz: de3e98178f876fd798da37e3872d57e9702f7ee4e1fe36d8cf8030defb8a6222732583ad2182f51d7f64321985a551a7caca3ca12475d450f4247e9734a1d20a
6
+ metadata.gz: a01acaa5d2f787f077bbc13944764b97108e0e397a5686a9f17c37f6d94b7feaac59e3ebd7bfc8c436d8c097bac9fd31c13035f84fd2c0f3c4ff9fd78adae214
7
+ data.tar.gz: 5c0e17e93153427762672df1cb7f052f8f65f88f6f54e3a5ac89cbe918e2f401244016a316e3e1a20c8252e9551a9c9e2965138b1554ccb9f945a1077e5b9f86
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v '~> 1.11'
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # infobip-sms-api-ruby
2
2
 
3
- Simple API wrapper for Infobip SMS Api
3
+ [![Build Status](https://travis-ci.org/dkocic/infobip-sms-api-ruby.png)](https://travis-ci.org/dkocic/infobip-sms-api-ruby)
4
+ [![Coverage Status](https://coveralls.io/repos/github/dkocic/infobip-sms-api-ruby/badge.svg?branch=master)](https://coveralls.io/github/dkocic/infobip-sms-api-ruby?branch=master)
5
+
6
+ Simple API wrapper for Infobip SMS Api (https://dev.infobip.com/)
7
+
8
+ Notes:
9
+ **This is not offical infobip release**, it's a personal project started because there is no other solution ATM.
10
+
4
11
 
5
12
  ## Installation
6
13
 
@@ -41,18 +48,51 @@ To successfully use this gem you need to have Infobip account, and you need to s
41
48
 
42
49
  ## Usage
43
50
 
44
- ### Send single textual message
45
- ```ruby
46
- message = Infobip::SmsApi::Message.new(from: 'Sender', to: '381650000000', text: "Lorem ipsum...")
47
- response = message.send_text
48
- ```
51
+ ```ruby
52
+
53
+ text_message = Infobip::SmsApi::TextMessage.new(
54
+ from: 'Sender',
55
+ to: '31630000000',
56
+ text: "Lorem ipsum...")
57
+
58
+ another_text_message = Infobip::SmsApi::TextMessage.new(
59
+ from: 'Sender',
60
+ to: ['31630000000', '31630000001'],
61
+ text: "Lorem ipsum...")
62
+
63
+ binary_message = Infobip::SmsApi::BinaryMessage.new(
64
+ from:'binary',
65
+ to: '31630000000',
66
+ binary: {
67
+ hex: '54 65 73 74 20 6d 65 73 73 61 67 65 2e',
68
+ data_coding: 0,
69
+ esm_class: 0
70
+ })
71
+
72
+ another_binary_message = Infobip::SmsApi::BinaryMessage.new(
73
+ from:'binary2',
74
+ to: ['31630000000', '31630000001'],
75
+ binary: {
76
+ hex: '54 65 73 74 20 6d 65 73 73 61 67 65 2e',
77
+ data_coding: 0,
78
+ esm_class: 0
79
+ })
80
+
81
+ # Send single text sms message
82
+ response = Infobip::SmsApi.deliver(text_message)
83
+ # Send multiple text sms messages
84
+ response = Infobip::SmsApi.deliver(text_message, another_text_message)
85
+
86
+ # Send single binary sms message
87
+ response = Infobip::SmsApi.deliver(binary_message)
88
+ # Send multiple binary sms messages
89
+ response = Infobip::SmsApi.deliver(binary_message, another_binary_message)
90
+
91
+ success = response.successful?
49
92
 
50
- ### Send single textual message to multiple destinations
51
- ```ruby
52
- message = Infobip::SmsApi::Message.new(from: 'Sender', to: ['381650000000','381650000001'], text: "Lorem ipsum...")
53
- response = message.send_text
54
93
  ```
55
94
 
95
+
56
96
  ## Development
57
97
 
58
98
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "infobip/sms/api/ruby"
4
+ require "infobip/sms_api"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -24,6 +24,9 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "minitest", "~> 5.8"
25
25
  spec.add_development_dependency "vcr", "~> 3.0"
26
26
  spec.add_development_dependency "webmock", "~> 1.24"
27
+ spec.add_development_dependency "simplecov", "~> 0.11"
28
+ spec.add_development_dependency "rails", "~> 5.0.0.beta3"
29
+ spec.add_development_dependency "coveralls", "~> 0"
27
30
 
28
31
  spec.add_dependency "faraday", "~> 0.9"
29
32
  spec.add_dependency "json", "~> 1.8"
@@ -1,8 +1,13 @@
1
1
  require 'infobip/sms_api/version'
2
+ require 'infobip/sms_api/errors'
2
3
  require 'infobip/sms_api/message'
4
+ require 'infobip/sms_api/messages'
3
5
  require 'infobip/sms_api/response'
4
6
  require 'infobip/sms_api/configuration'
5
-
7
+ require 'infobip/sms_api/model/base'
8
+ require 'infobip/sms_api/model/text_message'
9
+ require 'infobip/sms_api/model/binary_message'
10
+ require 'infobip/sms_api/model/binary_data'
6
11
  module Infobip
7
12
  module SmsApi
8
13
 
@@ -19,5 +24,14 @@ module Infobip
19
24
  configuration.connect
20
25
  end
21
26
 
27
+ def self.deliver(*messages)
28
+ raise RequiredArgumentMissingError, 'At least 1 message should be sent' if messages.size == 0
29
+ if messages.size == 1
30
+ Infobip::SmsApi::Messages.send_single(messages.first)
31
+ else
32
+ Infobip::SmsApi::Messages.send_multiple(messages)
33
+ end
34
+ end
35
+
22
36
  end
23
37
  end
@@ -0,0 +1,12 @@
1
+ module Infobip
2
+ module SmsApi
3
+ class InfobipStandardError < StandardError
4
+ end
5
+ class RequiredArgumentMissingError < InfobipStandardError
6
+ end
7
+ class UnknownArgumentError < InfobipStandardError
8
+ end
9
+ class MalformedArgumentError < InfobipStandardError
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,65 @@
1
+ module Infobip
2
+ module SmsApi
3
+ module Messages
4
+
5
+ def self.send_single(message)
6
+ if message.class == Infobip::SmsApi::TextMessage
7
+ return send_single_text_message message
8
+ elsif message.class == Infobip::SmsApi::BinaryMessage
9
+ return send_single_binary_message message
10
+ else
11
+ raise UnknownArgumentError, "Unknown message type: #{message.class}"
12
+ end
13
+ end
14
+
15
+ def self.send_multiple(messages)
16
+ return send_multiple_text_messages messages if valid_messages?(messages, Infobip::SmsApi::TextMessage)
17
+ return send_multiple_binary_messages messages if valid_messages?(messages, Infobip::SmsApi::BinaryMessage)
18
+ raise MalformedArgumentError, 'When sending multiple messages, all messages need to be of the same type'
19
+ end
20
+
21
+ def self.valid_messages?(messages, object_class)
22
+ messages.all? do |message|
23
+ message.class == object_class
24
+ end
25
+ end
26
+
27
+ def self.send_single_text_message(message)
28
+ post_single(Infobip::SmsApi::TextMessage, '/sms/1/text/single', message)
29
+ end
30
+
31
+ def self.send_single_binary_message(message)
32
+ post_single(Infobip::SmsApi::BinaryMessage, '/sms/1/binary/single', message)
33
+ end
34
+
35
+ def self.send_multiple_text_messages(messages)
36
+ post_multi('/sms/1/text/multi', messages)
37
+ end
38
+
39
+ def self.send_multiple_binary_messages(messages)
40
+ post_multi('/sms/1/binary/multi', messages)
41
+ end
42
+
43
+ def self.post_single(class_name, endpoint, message)
44
+ raise UnknownArgumentError, "Unsupported Message Type: #{message.class}" unless message.class == class_name
45
+ response = self.post(endpoint, message)
46
+ Response::Base.new(response.status, JSON.parse(response.body))
47
+ end
48
+
49
+ def self.post_multi(endpoint, messages)
50
+ response = self.post(endpoint, { messages: messages.map(&:to_hash) })
51
+ Response::Base.new(response.status, JSON.parse(response.body))
52
+ end
53
+
54
+ def self.post(url, body)
55
+ Infobip::SmsApi.configuration.connection.post do |req|
56
+ req.url url
57
+ req.body = body.to_json
58
+ req.headers['Accept'] = 'application/json'
59
+ req.headers['Content-Type'] = 'application/json'
60
+ end
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ require 'json'
2
+
3
+ module Infobip
4
+ module SmsApi
5
+ class Base
6
+
7
+ def to_hash
8
+ hash = {}
9
+ self.instance_variables.each do |var|
10
+ value = self.instance_variable_get(var)
11
+ hash[var.to_s.delete('@')] = value.respond_to?(:to_hash) ? value.to_hash : value
12
+ end
13
+ hash
14
+ end
15
+
16
+ def to_json
17
+ self.to_hash.to_json
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'infobip/sms_api/model/base'
2
+ module Infobip
3
+ module SmsApi
4
+ class BinaryData < Base
5
+
6
+ attr_accessor :hex, :data_coding, :esm_class
7
+
8
+ def initialize(attributes)
9
+ @hex = attributes[:hex]
10
+ @data_coding = attributes[:data_coding]
11
+ @esm_class = attributes[:esm_class]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Infobip
2
+ module SmsApi
3
+ class BinaryMessage < Base
4
+
5
+ attr_accessor :from, :to, :binary
6
+
7
+ def initialize(attributes)
8
+ @from = attributes[:from]
9
+ @to = attributes[:to]
10
+ @binary = BinaryData.new(attributes[:binary])
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require 'infobip/sms_api/model/binary_data'
2
+ module Infobip
3
+ module SmsApi
4
+ class TextMessage < Base
5
+
6
+ attr_accessor :from, :to, :text
7
+
8
+ def initialize(attributes)
9
+ @from = attributes[:from]
10
+ @to = attributes[:to]
11
+ @text = attributes[:text]
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -1,6 +1,8 @@
1
1
  require 'infobip/sms_api/response/base'
2
2
  require 'infobip/sms_api/response/message'
3
3
  require 'infobip/sms_api/response/status'
4
+ require 'infobip/sms_api/response/request_error'
5
+ require 'infobip/sms_api/response/service_exception'
4
6
  module Infobip
5
7
  module SmsApi
6
8
  module Response
@@ -14,9 +14,8 @@ module Infobip
14
14
  attributes['messages'].each do |message|
15
15
  @messages << Infobip::SmsApi::Response::Message.new(message)
16
16
  end
17
- when 401
18
- @request_error = attributes['requestError']
19
17
  else
18
+ @request_error = Infobip::SmsApi::Response::RequestError.new(attributes['requestError'])
20
19
  end
21
20
  end
22
21
 
@@ -31,7 +30,7 @@ module Infobip
31
30
  def failed_messages
32
31
  @messages.select { |message|
33
32
  !SUCCESSFUL_STATUSES.include?message.status.group_name
34
- }
33
+ } if @messages
35
34
  end
36
35
 
37
36
  ##
@@ -39,7 +38,7 @@ module Infobip
39
38
  def sent_messages
40
39
  @messages.select { |message|
41
40
  SUCCESSFUL_STATUSES.include?message.status.group_name
42
- }
41
+ } if @messages
43
42
  end
44
43
  end
45
44
  end
@@ -0,0 +1,12 @@
1
+ module Infobip
2
+ module SmsApi
3
+ module Response
4
+ class RequestError
5
+ attr_reader :service_exception
6
+ def initialize(attributes)
7
+ @service_exception = attributes['serviceException']
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Infobip
2
+ module SmsApi
3
+ module Response
4
+ class ServiceException
5
+
6
+ attr_reader :message_id, :text
7
+
8
+ def initialize(attributes)
9
+ @message_id = attributes['messageId']
10
+ @text = attributes['text']
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module Infobip
2
2
  module SmsApi
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infobip-sms-api-ruby
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
  - dkocic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-09 00:00:00.000000000 Z
11
+ date: 2016-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,48 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.24'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.11'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.11'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 5.0.0.beta3
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 5.0.0.beta3
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
83
125
  - !ruby/object:Gem::Dependency
84
126
  name: faraday
85
127
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +158,7 @@ extensions: []
116
158
  extra_rdoc_files: []
117
159
  files:
118
160
  - ".gitignore"
161
+ - ".travis.yml"
119
162
  - CODE_OF_CONDUCT.md
120
163
  - Gemfile
121
164
  - LICENSE.txt
@@ -123,18 +166,23 @@ files:
123
166
  - Rakefile
124
167
  - bin/console
125
168
  - bin/setup
126
- - fixtures/test_single_missing_to.yml
127
- - fixtures/test_single_network_rejected.yml
128
- - fixtures/test_single_pending.yml
129
169
  - infobip-sms-api-ruby.gemspec
130
170
  - lib/generators/infobip/config/config_generator.rb
131
171
  - lib/generators/infobip/config/templates/config.rb
132
172
  - lib/infobip/sms_api.rb
133
173
  - lib/infobip/sms_api/configuration.rb
174
+ - lib/infobip/sms_api/errors.rb
134
175
  - lib/infobip/sms_api/message.rb
176
+ - lib/infobip/sms_api/messages.rb
177
+ - lib/infobip/sms_api/model/base.rb
178
+ - lib/infobip/sms_api/model/binary_data.rb
179
+ - lib/infobip/sms_api/model/binary_message.rb
180
+ - lib/infobip/sms_api/model/text_message.rb
135
181
  - lib/infobip/sms_api/response.rb
136
182
  - lib/infobip/sms_api/response/base.rb
137
183
  - lib/infobip/sms_api/response/message.rb
184
+ - lib/infobip/sms_api/response/request_error.rb
185
+ - lib/infobip/sms_api/response/service_exception.rb
138
186
  - lib/infobip/sms_api/response/status.rb
139
187
  - lib/infobip/sms_api/version.rb
140
188
  homepage: https://github.com/dkocic/infobip-sms-api-ruby
@@ -1,45 +0,0 @@
1
- ---
2
- http_interactions:
3
- - request:
4
- method: post
5
- uri: https://test:test@api.infobip.com/sms/1/text/single
6
- body:
7
- encoding: UTF-8
8
- string: '{"from":"Test","to":"","text":"Testing"}'
9
- headers:
10
- User-Agent:
11
- - Faraday v0.9.2
12
- Accept:
13
- - application/json
14
- Content-Type:
15
- - application/json
16
- Accept-Encoding:
17
- - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
- response:
19
- status:
20
- code: 401
21
- message: Unauthorized
22
- headers:
23
- Access-Control-Allow-Origin:
24
- - "*"
25
- Access-Control-Allow-Methods:
26
- - GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE
27
- Access-Control-Allow-Headers:
28
- - Authorization, Content-Type, X-Enrollment-Token
29
- Content-Type:
30
- - application/json;charset=UTF-8
31
- Content-Length:
32
- - '97'
33
- Date:
34
- - Wed, 09 Mar 2016 19:11:58 GMT
35
- Connection:
36
- - close
37
- Server:
38
- - SMS API
39
- body:
40
- encoding: UTF-8
41
- string: '{"requestError":{"serviceException":{"messageId":"UNAUTHORIZED","text":"Invalid
42
- login details"}}}'
43
- http_version:
44
- recorded_at: Wed, 09 Mar 2016 19:11:58 GMT
45
- recorded_with: VCR 3.0.1
@@ -1,45 +0,0 @@
1
- ---
2
- http_interactions:
3
- - request:
4
- method: post
5
- uri: https://test:test@api.infobip.com/sms/1/text/single
6
- body:
7
- encoding: UTF-8
8
- string: '{"from":"Test","to":"381203136825","text":"Testing"}'
9
- headers:
10
- User-Agent:
11
- - Faraday v0.9.2
12
- Accept:
13
- - application/json
14
- Content-Type:
15
- - application/json
16
- Accept-Encoding:
17
- - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
- response:
19
- status:
20
- code: 401
21
- message: Unauthorized
22
- headers:
23
- Access-Control-Allow-Origin:
24
- - "*"
25
- Access-Control-Allow-Methods:
26
- - GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE
27
- Access-Control-Allow-Headers:
28
- - Authorization, Content-Type, X-Enrollment-Token
29
- Content-Type:
30
- - application/json;charset=UTF-8
31
- Content-Length:
32
- - '97'
33
- Date:
34
- - Wed, 09 Mar 2016 19:11:58 GMT
35
- Connection:
36
- - close
37
- Server:
38
- - SMS API
39
- body:
40
- encoding: UTF-8
41
- string: '{"requestError":{"serviceException":{"messageId":"UNAUTHORIZED","text":"Invalid
42
- login details"}}}'
43
- http_version:
44
- recorded_at: Wed, 09 Mar 2016 19:11:58 GMT
45
- recorded_with: VCR 3.0.1
@@ -1,45 +0,0 @@
1
- ---
2
- http_interactions:
3
- - request:
4
- method: post
5
- uri: https://test:test@api.infobip.com/sms/1/text/single
6
- body:
7
- encoding: UTF-8
8
- string: '{"from":"Test","to":"31633136825","text":"Testing"}'
9
- headers:
10
- User-Agent:
11
- - Faraday v0.9.2
12
- Accept:
13
- - application/json
14
- Content-Type:
15
- - application/json
16
- Accept-Encoding:
17
- - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
- response:
19
- status:
20
- code: 401
21
- message: Unauthorized
22
- headers:
23
- Access-Control-Allow-Origin:
24
- - "*"
25
- Access-Control-Allow-Methods:
26
- - GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE
27
- Access-Control-Allow-Headers:
28
- - Authorization, Content-Type, X-Enrollment-Token
29
- Content-Type:
30
- - application/json;charset=UTF-8
31
- Content-Length:
32
- - '97'
33
- Date:
34
- - Wed, 09 Mar 2016 19:11:58 GMT
35
- Connection:
36
- - close
37
- Server:
38
- - SMS API
39
- body:
40
- encoding: UTF-8
41
- string: '{"requestError":{"serviceException":{"messageId":"UNAUTHORIZED","text":"Invalid
42
- login details"}}}'
43
- http_version:
44
- recorded_at: Wed, 09 Mar 2016 19:11:58 GMT
45
- recorded_with: VCR 3.0.1