aftalk 0.1.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.
@@ -0,0 +1,73 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::Configuration do
4
+ let(:api_key) { "abc123" }
5
+ let(:sandbox) { true }
6
+ let(:user_name) { "Thrive Global" }
7
+
8
+ before(:each) do
9
+ allow(AfTalk::Connection).to receive(:clear)
10
+
11
+ described_class.configure do |config|
12
+ config.api_key = api_key
13
+ config.sandbox = sandbox
14
+ config.user_name = user_name
15
+ end
16
+ end
17
+
18
+ describe ".api_key" do
19
+ let(:api_key) { nil }
20
+
21
+ before(:each) { stub_env_vars("AFRICAS_TALKING_API_KEY" => "env_key") }
22
+
23
+ context "when no api key is configured" do
24
+ it "uses the value set in dotenv" do
25
+ expect(described_class.api_key).to eq("env_key")
26
+ end
27
+ end
28
+ end
29
+
30
+ describe ".configure" do
31
+ it "allows setting version and api_key values" do
32
+ expect(described_class.api_key).to eq("abc123")
33
+ expect(described_class.user_name).to eq("Thrive Global")
34
+ expect(described_class.sandbox).to be
35
+ end
36
+
37
+ it "clears the connection" do
38
+ expect(AfTalk::Connection).to have_received(:clear)
39
+ end
40
+ end
41
+
42
+ describe ".sandbox" do
43
+ let(:sandbox) { nil }
44
+
45
+ before(:each) { stub_env_vars("AFRICAS_TALKING_SANDBOX" => "env_sandbox") }
46
+
47
+ context "when no api key is configured" do
48
+ it "uses the value set in dotenv" do
49
+ expect(described_class.sandbox).to eq("env_sandbox")
50
+ end
51
+ end
52
+ end
53
+
54
+ describe ".user_name" do
55
+ let(:user_name) { nil }
56
+
57
+ before(:each) { stub_env_vars("AFRICAS_TALKING_USER_NAME" => "env_user_name") }
58
+
59
+ context "when no api key is configured" do
60
+ it "uses the value set in dotenv" do
61
+ expect(described_class.user_name).to eq("env_user_name")
62
+ end
63
+ end
64
+ end
65
+
66
+ describe ".version" do
67
+ context "returns the default version" do
68
+ it "returns the default version" do
69
+ expect(described_class.version).to eq("version1")
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::Connection do
4
+ describe ".build" do
5
+ let(:instance) { double }
6
+
7
+ before(:each) do
8
+ allow(described_class).to receive(:new).and_return(instance)
9
+ allow(instance).to receive(:build)
10
+ end
11
+
12
+ it "calls #build on an instance" do
13
+ described_class.build
14
+ expect(instance).to have_received(:build)
15
+ end
16
+
17
+ it "is memoized" do
18
+ first_connection = described_class.build
19
+ second_connection = described_class.build
20
+ expect(first_connection).to be(second_connection)
21
+ end
22
+ end
23
+
24
+ describe ".clear" do
25
+ it "clears the memoization of the connection" do
26
+ first_connection = described_class.build
27
+ described_class.clear
28
+ second_connection = described_class.build
29
+ expect(first_connection).not_to be(second_connection)
30
+ end
31
+ end
32
+
33
+ describe "#build" do
34
+ let(:connection) { described_class.new.build }
35
+ let(:base_url) { connection.url_prefix.to_s }
36
+
37
+ context "in all modes" do
38
+ it "has the expected properties" do
39
+ headers = connection.headers
40
+
41
+ expect(connection.scheme).to eq("https")
42
+ expect(headers["Accept"]).to eq("application/json")
43
+ expect(headers["Apikey"]).to eq(AfTalk::Configuration.api_key)
44
+ end
45
+ end
46
+
47
+ context "in sandbox mode" do
48
+ before(:each) do
49
+ stub_env_vars("SANDBOX" => "true")
50
+ end
51
+
52
+ it "uses the sandbox url" do
53
+ expect(base_url).to match(described_class::SANDBOX_URL)
54
+ end
55
+ end
56
+
57
+ context "in production mode" do
58
+ before(:each) do
59
+ stub_env_vars("AFRICAS_TALKING_SANDBOX" => nil)
60
+ end
61
+
62
+ it "uses the production url" do
63
+ expect(base_url).to match(described_class::URL)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::Request do
4
+ let(:connection) { double("connection") }
5
+ let(:get_response) { double("get_response") }
6
+ let(:post_response) { double("post_response") }
7
+ let(:api_key) { "abc123" }
8
+ let(:user_name) { "Thrive Global" }
9
+ let(:default_params) { { username: user_name } }
10
+ let(:options) { { foo: "bar" } }
11
+
12
+ before(:each) do
13
+ allow(AfTalk::Connection).to receive(:build).and_return(connection)
14
+ end
15
+
16
+ describe ".get" do
17
+ it "creates an instance and sends #get to it" do
18
+ instance = double
19
+ allow(described_class).to receive(:new).and_return(instance)
20
+ allow(instance).to receive(:get).
21
+ with("/path", options).
22
+ and_return(get_response)
23
+
24
+ expect(described_class.get("/path", options)).to eq(get_response)
25
+ end
26
+ end
27
+
28
+ describe ".post" do
29
+ it "creates an instance and sends #post to it" do
30
+ instance = double
31
+ allow(described_class).to receive(:new).and_return(instance)
32
+ allow(instance).to receive(:post).
33
+ with("/path", options).
34
+ and_return(get_response)
35
+
36
+ expect(described_class.post("/path", options)).to eq(get_response)
37
+ end
38
+ end
39
+
40
+ describe "#get" do
41
+ it "returns the response of doing a get to an API endpoint" do
42
+ allow(connection).to receive(:get).
43
+ with("/version1/test", options.merge(default_params)).
44
+ and_return(get_response)
45
+
46
+ expect(described_class.new.get("/test", options)).to eq(get_response)
47
+ end
48
+ end
49
+
50
+ describe "#post" do
51
+ it "returns the response of doing a post to an API endpoint" do
52
+ allow(connection).to receive(:post).
53
+ with("/version1/test", options.merge(default_params)).
54
+ and_return(post_response)
55
+
56
+ expect(described_class.new.post("/test", options)).to eq(post_response)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::Response do
4
+ describe ".new" do
5
+ subject { described_class.new(response) }
6
+
7
+ context "when the response code is a success code" do
8
+ let(:response) { double(status: 200, body: { foo: "bar" }) }
9
+
10
+ it "sets the status and the body" do
11
+ expect(subject.status).to eq(200)
12
+ expect(subject.body).to eq(foo: "bar")
13
+ expect(subject.error_message).to be_nil
14
+ end
15
+ end
16
+
17
+ context "when the response code is a failure code" do
18
+ let(:response) { double(status: 400, body: "Something went wrong.") }
19
+
20
+ it "sets the status, the body, and the error message" do
21
+ expect(subject.status).to eq(400)
22
+ expect(subject.body).to eq(error: "Something went wrong.")
23
+ expect(subject.error_message).to eq("Something went wrong.")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::SendMessageResponse 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: 201) }
9
+ let(:data) do
10
+ {
11
+ SMSMessageData: {
12
+ Message: "The message was delivered.",
13
+ Recipients: [recipient_data],
14
+ },
15
+ }
16
+ end
17
+ let(:recipient_data) { double("RecipientData") }
18
+ let(:message_status) { double("MessageStatus") }
19
+
20
+ before(:each) do
21
+ allow(AfTalk::SmsMessageStatus).to receive(:new).
22
+ with(recipient_data).
23
+ and_return(message_status)
24
+ end
25
+
26
+ it "sets a message and initializes recipients" do
27
+ expect(subject).to be_kind_of(AfTalk::Response)
28
+ expect(subject.message).to eq(response.body[:SMSMessageData][:Message])
29
+ expect(subject.recipients).to eq([message_status])
30
+ end
31
+ end
32
+
33
+ context "when the response is not a success" do
34
+ let(:response) { double(body: "This failed.", status: 400) }
35
+
36
+ it "doesn't set a message or recipients" do
37
+ expect(subject).to be_kind_of(AfTalk::Response)
38
+ expect(subject.message).to be_nil
39
+ expect(subject.recipients).to be_nil
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe AfTalk::SmsMessageStatus do
4
+ let(:options) {
5
+ {
6
+ cost: "USD 0.0200",
7
+ messageId: "ATXid_39b49d3e64107e6b859cab4we2900a07",
8
+ number: "+15555554563",
9
+ status: "Success",
10
+ }
11
+ }
12
+
13
+ subject { described_class.new(options) }
14
+
15
+ describe ".new" do
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])
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aftalk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thrive Global Engineering Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dotenv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec_junit_formatter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ description: " A Ruby wrapper for Africa's Talking telephony services (https://africastalking.com)\n"
126
+ email:
127
+ - techadmin@thriveglobal.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".circleci/config.yml"
133
+ - ".gitignore"
134
+ - ".rubocop.yml"
135
+ - Gemfile
136
+ - README.md
137
+ - Rakefile
138
+ - africas_talking_api.gemspec
139
+ - lib/aftalk.rb
140
+ - lib/aftalk/client.rb
141
+ - lib/aftalk/configuration.rb
142
+ - lib/aftalk/connection.rb
143
+ - lib/aftalk/exceptions.rb
144
+ - lib/aftalk/request.rb
145
+ - lib/aftalk/response.rb
146
+ - lib/aftalk/responses/send_message_response.rb
147
+ - lib/aftalk/sms_message_status.rb
148
+ - lib/aftalk/version.rb
149
+ - spec/aftalk_spec.rb
150
+ - spec/integration/send_message_spec.rb
151
+ - spec/spec_helper.rb
152
+ - spec/support/env_vars.rb
153
+ - spec/unit/client_spec.rb
154
+ - spec/unit/configuration_spec.rb
155
+ - spec/unit/connection_spec.rb
156
+ - spec/unit/request_spec.rb
157
+ - spec/unit/response_spec.rb
158
+ - spec/unit/responses/send_message_response_spec.rb
159
+ - spec/unit/sms_message_status_spec.rb
160
+ homepage: https://github.com/thriveglobal/africas_talking_api
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.6.14
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: API wrapper for Africa's Talking services
184
+ test_files:
185
+ - spec/aftalk_spec.rb
186
+ - spec/integration/send_message_spec.rb
187
+ - spec/spec_helper.rb
188
+ - spec/support/env_vars.rb
189
+ - spec/unit/client_spec.rb
190
+ - spec/unit/configuration_spec.rb
191
+ - spec/unit/connection_spec.rb
192
+ - spec/unit/request_spec.rb
193
+ - spec/unit/response_spec.rb
194
+ - spec/unit/responses/send_message_response_spec.rb
195
+ - spec/unit/sms_message_status_spec.rb