aftalk 0.1.1 → 0.2.0

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: e93ac6c06f96ef4f6e3127eefdeec2f8e3d55467
4
- data.tar.gz: 3cb611a13074f088a939ff4138de64b8390f2e1d
3
+ metadata.gz: d2ec14f0bf217c0d998f9ac7fe0174b55e791b5d
4
+ data.tar.gz: 214b21204ef61173687c8bf7800d686debdcf10b
5
5
  SHA512:
6
- metadata.gz: b47b57d4fc8467dffab8be0d385a4bf52972f4ced94f6d79406349171792d32e8bd1b7febde92c9b0bfeb2416f99385114986da8d31478ec0f2691e8fa240eb2
7
- data.tar.gz: 50d50f2677eff0b0d78eca81ecb71dc7042e067dd2dd0fd7c8aa4cd72b0ccff6a1c706d1e5550146ae60cf1bc2a23d2254a18cc5a4379d88bfa1a3e73ba79f96
6
+ metadata.gz: d73a014f67d8333641c44ae8a015bb71adab5cc0e15d758f4819401d13ef5769b6f082832c83fcdc79abb7877337d794254c26de31fb2795836e7511cff038ab
7
+ data.tar.gz: 5660ba0779adf7af9c16e655c783cc7f7b4e3b965daf2de158e4f67b92e4b52bc2ee5b544d12f4fd79a59d09ea9fa1e71321c28d668c35fc752ab403b83a261c
data/.gitignore CHANGED
@@ -14,6 +14,9 @@
14
14
  # Used by dotenv library to load environment variables.
15
15
  .env
16
16
 
17
+ # Byebug
18
+ .byebug_history
19
+
17
20
  ## Documentation cache and generated files:
18
21
  /.yardoc/
19
22
  /_yardoc/
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
+ [![Gem](https://badge.fury.io/rb/aftalk.svg)](https://rubygems.org/gems/aftalk)
1
2
  [![Build](https://circleci.com/gh/thriveglobal/aftalk.svg?&style=shield&circle-token=ade67468e07185e650dc6ba100127bce34bd10b5)](https://circleci.com/gh/thriveglobal/aftalk)
2
3
  [![Maintainability](https://api.codeclimate.com/v1/badges/8d84a0d4fc06f1c49cd8/maintainability)](https://codeclimate.com/github/thriveglobal/aftalk/maintainability)
3
4
 
5
+
4
6
  # aftalk
5
7
 
6
- A Ruby wrapper for [Africa's Talking](https://africastalking.com) telephony services.
8
+ This is a Ruby wrapper for [Africa's Talking](https://africastalking.com) telephony services.
7
9
 
8
- Currently the only endpoint supported is the `/messaging` POST endpoint that allows you to send SMS messages. Support for other endpoints is planned for future releases.
10
+ **NOTE:** Currently the gem supports only the `/messaging` endpoint, which is for for sending (POST) and fetching (GET) SMS messages. Support for other endpoints is planned for future releases.
9
11
 
10
12
  ## Installation
11
13
 
@@ -55,7 +57,7 @@ Make sure this code runs before you try to use the API.
55
57
 
56
58
  ## Usage
57
59
 
58
- To send an SMS:
60
+ ### Send an SMS message
59
61
 
60
62
  ```ruby
61
63
  phone_number = "+15555555555"
@@ -66,4 +68,14 @@ AfTalk.send_message(
66
68
  message: message,
67
69
  )
68
70
  ```
69
- The `send_message` method requires `to` and `message` parameters, but also supports all optional parameters supported by the API. For a full list, see [the API docs](http://docs.africastalking.com/sms/sending/).
71
+ The `send_message` method requires `to` and `message` keyword arguments, and also supports all optional parameters supported by the API. For a full list, see [the SMS sending API docs](http://docs.africastalking.com/sms/sending/).
72
+
73
+ ### Fetch SMS messages
74
+
75
+ ```ruby
76
+ AfTalk.fetch_messages(
77
+ last_received_id: 0,
78
+ )
79
+ ```
80
+
81
+ The `fetch_messages` method requires a `last_received_id` keyword argument and ignores any other parameters you pass to it. See [the SMS fetch messages API docs](http://docs.africastalking.com/sms/fetchmessages/) for more details.
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ namespace :irb do
4
+ desc "Open interactive REPL with our application preloaded"
5
+
6
+ task :console do
7
+ system("irb -r ./lib/aftalk.rb")
8
+ end
9
+ end
@@ -17,11 +17,13 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
 
19
19
  spec.add_development_dependency "bundler", "~> 1.15"
20
- spec.add_dependency "dotenv", "~>2.2"
21
- spec.add_dependency "faraday", "~> 0.13"
22
- spec.add_dependency "faraday_middleware", "~> 0.12"
20
+ spec.add_development_dependency "byebug", "~> 9.1"
23
21
  spec.add_development_dependency "rake", "~> 12"
24
22
  spec.add_development_dependency "rspec", "~> 3.7"
25
23
  spec.add_development_dependency "rspec_junit_formatter", "~> 0.3"
26
24
  spec.add_development_dependency "vcr", "~> 3.0"
25
+
26
+ spec.add_dependency "dotenv", "~>2.2"
27
+ spec.add_dependency "faraday", "~> 0.13"
28
+ spec.add_dependency "faraday_middleware", "~> 0.12"
27
29
  end
@@ -1,14 +1,26 @@
1
1
  module AfTalk
2
2
  module Client
3
+ SMS_ENDPOINT = "/messaging".freeze
4
+
3
5
  def send_message(to:, message:, **options)
4
6
  messaging_params = { to: to, message: message }.merge(options)
5
- response = post("/messaging", messaging_params)
7
+ AfTalk::SendMessageResponse.new(
8
+ post(SMS_ENDPOINT, messaging_params),
9
+ )
10
+ end
6
11
 
7
- AfTalk::SendMessageResponse.new(response)
12
+ def fetch_messages(last_received_id:)
13
+ AfTalk::FetchMessagesResponse.new(
14
+ get(SMS_ENDPOINT, lastReceivedId: last_received_id),
15
+ )
8
16
  end
9
17
 
10
18
  private
11
19
 
20
+ def get(endpoint, **options)
21
+ request.get(endpoint, options)
22
+ end
23
+
12
24
  def post(endpoint, **options)
13
25
  request.post(endpoint, options)
14
26
  end
@@ -0,0 +1,20 @@
1
+ module AfTalk
2
+ class FetchMessagesResponse < Response
3
+ attr_reader :messages
4
+
5
+ def initialize(response)
6
+ super(response)
7
+ if success?
8
+ @messages = body_data[:Messages].map do |message_data|
9
+ AfTalk::SmsMessageInfo.new(message_data)
10
+ end
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def body_data
17
+ body[:SMSMessageData]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ module AfTalk
2
+ class SmsMessageInfo
3
+ attr_reader :date, :from, :link_id, :message_id, :to
4
+
5
+ def initialize(data = {})
6
+ @date = data[:date]
7
+ @from = data[:from]
8
+ @link_id = data[:linkId]
9
+ @message_id = data[:id]
10
+ @to = data[:to]
11
+ end
12
+ end
13
+ end
@@ -2,11 +2,11 @@ module AfTalk
2
2
  class SmsMessageStatus
3
3
  attr_reader :cost, :message_id, :number, :status
4
4
 
5
- def initialize(opts = {})
6
- @cost = opts[:cost]
7
- @message_id = opts[:messageId]
8
- @number = opts[:number]
9
- @status = opts[:status]
5
+ def initialize(data = {})
6
+ @cost = data[:cost]
7
+ @message_id = data[:messageId]
8
+ @number = data[:number]
9
+ @status = data[:status]
10
10
  end
11
11
  end
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module AfTalk
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk do
4
+ describe "#fetch_messages" do
5
+ before(:each) { reset_config }
6
+
7
+ let(:options) { { last_received_id: 0 } }
8
+
9
+ it "produces a fetch messages response object" do
10
+ VCR.use_cassette("aftalk/messaging/fetch_messages") do
11
+ response = described_class.fetch_messages(options)
12
+ expect(response.error_message).to be_nil
13
+ expect(response.status).to eq(200)
14
+ expect(response.messages).to eq([])
15
+ end
16
+ end
17
+ end
18
+ end
@@ -2,6 +2,8 @@ require "spec_helper"
2
2
 
3
3
  describe AfTalk do
4
4
  describe "#send_message" do
5
+ before(:each) { reset_config }
6
+
5
7
  let(:options) do
6
8
  { to: "+14125554563", message: "Hello World!", foo: "bar" }
7
9
  end
@@ -1,6 +1,7 @@
1
1
  require "rspec"
2
2
  require "aftalk"
3
3
  require "vcr"
4
+ require "byebug"
4
5
 
5
6
  current_path = File.expand_path("../", __FILE__)
6
7
  Dir["#{current_path}/support/**/*.rb"].each { |file| require file }
@@ -0,0 +1,11 @@
1
+ module ResetConfig
2
+ def reset_config
3
+ config = AfTalk::Configuration
4
+ config.api_key = nil
5
+ config.user_name = nil
6
+ end
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.include ResetConfig
11
+ end
@@ -9,12 +9,12 @@ describe AfTalk::Client do
9
9
  end
10
10
 
11
11
  before(:all) { reset_config }
12
+ let(:response) { double(headers: { "content-type" => "application/json" }) }
12
13
 
13
14
  describe "#send_message" do
14
15
  let(:options) {
15
16
  { to: "+14125554563", message: "Hello World!", foo: "bar" }
16
17
  }
17
- let(:response) { double(headers: { "content-type" => "application/json" }) }
18
18
 
19
19
  context "with all required options" do
20
20
  it "it instantiates a send message response" do
@@ -38,9 +38,20 @@ describe AfTalk::Client do
38
38
  end
39
39
  end
40
40
 
41
- def reset_config
42
- config = AfTalk::Configuration
43
- config.api_key = nil
44
- config.user_name = nil
41
+ describe "#fetch_messages" do
42
+ let(:options) { { last_received_id: "abc123" } }
43
+
44
+ context "with all required options" do
45
+ it "it instantiates a fetch messages response" do
46
+ allow(AfTalk::Request).to receive(:get).
47
+ with("/messaging", lastReceivedId: options[:last_received_id]).
48
+ and_return(response)
49
+ allow(AfTalk::FetchMessagesResponse).to receive(:new).with(response)
50
+
51
+ subject.fetch_messages(options)
52
+
53
+ expect(AfTalk::FetchMessagesResponse).to have_received(:new).with(response)
54
+ end
55
+ end
45
56
  end
46
57
  end
@@ -28,7 +28,7 @@ describe AfTalk::Configuration do
28
28
  end
29
29
 
30
30
  describe ".configure" do
31
- it "allows setting version and api_key values" do
31
+ it "allows setting version, sandbox, and api_key values" do
32
32
  expect(described_class.api_key).to eq("abc123")
33
33
  expect(described_class.user_name).to eq("Thrive Global")
34
34
  expect(described_class.sandbox).to be
@@ -44,7 +44,7 @@ describe AfTalk::Configuration do
44
44
 
45
45
  before(:each) { stub_env_vars("AFRICAS_TALKING_SANDBOX" => "env_sandbox") }
46
46
 
47
- context "when no api key is configured" do
47
+ context "when no sandbox value is configured" do
48
48
  it "uses the value set in dotenv" do
49
49
  expect(described_class.sandbox).to eq("env_sandbox")
50
50
  end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::FetchMessagesResponse do
4
+ describe ".new" do
5
+ subject { described_class.new(response) }
6
+
7
+ context "when the response is a success" do
8
+ let(:response) { double(body: data, status: 200) }
9
+ let(:data) do
10
+ {
11
+ SMSMessageData: {
12
+ Messages: [message_data],
13
+ },
14
+ }
15
+ end
16
+ let(:message_data) { double("MessageData") }
17
+ let(:message_info) { double("MessageInfo") }
18
+
19
+ before(:each) do
20
+ allow(AfTalk::SmsMessageInfo).to receive(:new).
21
+ with(message_data).
22
+ and_return(message_info)
23
+ end
24
+
25
+ it "sets messages" do
26
+ expect(subject).to be_kind_of(AfTalk::Response)
27
+ expect(subject.messages).to eq([message_info])
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::SmsMessageInfo do
4
+ let(:data) {
5
+ {
6
+ date: "2012-07-15 15:40:06",
7
+ from: "+15555554563",
8
+ id: "12345",
9
+ linkId: "foo",
10
+ to: "+15555554564",
11
+ }
12
+ }
13
+
14
+ subject { described_class.new(data) }
15
+
16
+ describe ".new" do
17
+ it "sets the expected attributes" do
18
+ expect(subject.date).to eq(data[:date])
19
+ expect(subject.from).to eq(data[:from])
20
+ expect(subject.link_id).to eq(data[:linkId])
21
+ expect(subject.message_id).to eq(data[:id])
22
+ expect(subject.to).to eq(data[:to])
23
+ end
24
+ end
25
+ end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe AfTalk::SmsMessageStatus do
4
- let(:options) {
4
+ let(:data) {
5
5
  {
6
6
  cost: "USD 0.0200",
7
7
  messageId: "ATXid_39b49d3e64107e6b859cab4we2900a07",
@@ -10,14 +10,14 @@ describe AfTalk::SmsMessageStatus do
10
10
  }
11
11
  }
12
12
 
13
- subject { described_class.new(options) }
13
+ subject { described_class.new(data) }
14
14
 
15
15
  describe ".new" do
16
16
  it "sets the expected attributes" do
17
- expect(subject.cost).to eq(options[:cost])
18
- expect(subject.message_id).to eq(options[:messageId])
19
- expect(subject.number).to eq(options[:number])
20
- expect(subject.status).to eq(options[:status])
17
+ expect(subject.cost).to eq(data[:cost])
18
+ expect(subject.message_id).to eq(data[:messageId])
19
+ expect(subject.number).to eq(data[:number])
20
+ expect(subject.status).to eq(data[:status])
21
21
  end
22
22
  end
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aftalk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Hoffman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-15 00:00:00.000000000 Z
11
+ date: 2017-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -25,103 +25,117 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.15'
27
27
  - !ruby/object:Gem::Dependency
28
- name: dotenv
28
+ name: byebug
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.2'
34
- type: :runtime
33
+ version: '9.1'
34
+ type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.2'
40
+ version: '9.1'
41
41
  - !ruby/object:Gem::Dependency
42
- name: faraday
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.13'
48
- type: :runtime
47
+ version: '12'
48
+ type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.13'
54
+ version: '12'
55
55
  - !ruby/object:Gem::Dependency
56
- name: faraday_middleware
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.12'
62
- type: :runtime
61
+ version: '3.7'
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.12'
68
+ version: '3.7'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake
70
+ name: rspec_junit_formatter
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '12'
75
+ version: '0.3'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '12'
82
+ version: '0.3'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rspec
84
+ name: vcr
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '3.7'
89
+ version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '3.7'
96
+ version: '3.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rspec_junit_formatter
98
+ name: dotenv
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0.3'
104
- type: :development
103
+ version: '2.2'
104
+ type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0.3'
110
+ version: '2.2'
111
111
  - !ruby/object:Gem::Dependency
112
- name: vcr
112
+ name: faraday
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '3.0'
118
- type: :development
117
+ version: '0.13'
118
+ type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '3.0'
124
+ version: '0.13'
125
+ - !ruby/object:Gem::Dependency
126
+ name: faraday_middleware
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.12'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.12'
125
139
  description: " A Ruby wrapper for Africa's Talking telephony services (https://africastalking.com)\n"
126
140
  email:
127
141
  - techadmin@thriveglobal.com
@@ -135,7 +149,7 @@ files:
135
149
  - Gemfile
136
150
  - README.md
137
151
  - Rakefile
138
- - africas_talking_api.gemspec
152
+ - aftalk.gemspec
139
153
  - lib/aftalk.rb
140
154
  - lib/aftalk/client.rb
141
155
  - lib/aftalk/configuration.rb
@@ -143,19 +157,25 @@ files:
143
157
  - lib/aftalk/exceptions.rb
144
158
  - lib/aftalk/request.rb
145
159
  - lib/aftalk/response.rb
160
+ - lib/aftalk/responses/fetch_messages_response.rb
146
161
  - lib/aftalk/responses/send_message_response.rb
162
+ - lib/aftalk/sms_message_info.rb
147
163
  - lib/aftalk/sms_message_status.rb
148
164
  - lib/aftalk/version.rb
149
165
  - spec/aftalk_spec.rb
166
+ - spec/integration/fetch_messages_spec.rb
150
167
  - spec/integration/send_message_spec.rb
151
168
  - spec/spec_helper.rb
152
169
  - spec/support/env_vars.rb
170
+ - spec/support/reset_config.rb
153
171
  - spec/unit/client_spec.rb
154
172
  - spec/unit/configuration_spec.rb
155
173
  - spec/unit/connection_spec.rb
156
174
  - spec/unit/request_spec.rb
157
175
  - spec/unit/response_spec.rb
176
+ - spec/unit/responses/fetch_messages_response_spec.rb
158
177
  - spec/unit/responses/send_message_response_spec.rb
178
+ - spec/unit/sms_message_info_spec.rb
159
179
  - spec/unit/sms_message_status_spec.rb
160
180
  homepage: https://github.com/thriveglobal/aftalk
161
181
  licenses:
@@ -183,13 +203,17 @@ specification_version: 4
183
203
  summary: API wrapper for Africa's Talking services
184
204
  test_files:
185
205
  - spec/aftalk_spec.rb
206
+ - spec/integration/fetch_messages_spec.rb
186
207
  - spec/integration/send_message_spec.rb
187
208
  - spec/spec_helper.rb
188
209
  - spec/support/env_vars.rb
210
+ - spec/support/reset_config.rb
189
211
  - spec/unit/client_spec.rb
190
212
  - spec/unit/configuration_spec.rb
191
213
  - spec/unit/connection_spec.rb
192
214
  - spec/unit/request_spec.rb
193
215
  - spec/unit/response_spec.rb
216
+ - spec/unit/responses/fetch_messages_response_spec.rb
194
217
  - spec/unit/responses/send_message_response_spec.rb
218
+ - spec/unit/sms_message_info_spec.rb
195
219
  - spec/unit/sms_message_status_spec.rb