Skip to content

Commit d7ae2fe

Browse files
author
marcel corso gonzalez
authored
Merge pull request #70 from jalerson/fix-conversations-client
Fix conversations client
2 parents 7c916df + 8e81081 commit d7ae2fe

6 files changed

Lines changed: 190 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/Gemfile.lock
44
.rbenv-gemsets
55
.ruby-version
6+
.byebug_history

lib/messagebird/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module MessageBird
77
class Base
88
# takes each element from the given hash and apply it to ourselves through an assignment method
99
def map_hash_elements_to_self(hash)
10+
return if hash.nil?
11+
1012
hash.each do |key, value|
1113
method_name = key.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase # convert came case to snake case
1214
method_name += '='

lib/messagebird/conversation_client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def prepare_request(request, params = {})
1414
request.body = params.to_json
1515
request
1616
end
17-
end
1817

19-
def endpoint
20-
ENDPOINT
18+
def endpoint
19+
ENDPOINT
20+
end
2121
end
2222
end

spec/base_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
describe '#map_hash_elements_to_self' do
4+
context 'hash is nil' do
5+
it 'doesnt raise errors' do
6+
expect { @contact = MessageBird::Contact.new(nil) }.not_to raise_error
7+
end
8+
end
9+
10+
context 'hash is a contact' do
11+
before(:all) do
12+
@contact = MessageBird::Contact.new(
13+
'id' => '03dfc27855c3475b953d6200a1b7eaf7',
14+
'msisdn' => '+31600000000',
15+
'firstName' => 'John',
16+
'lastName' => 'Doe'
17+
)
18+
end
19+
20+
it 'contains an id' do
21+
expect(@contact.id).to be('03dfc27855c3475b953d6200a1b7eaf7')
22+
end
23+
24+
it 'contains a msisdn' do
25+
expect(@contact.msisdn).to be('+31600000000')
26+
end
27+
28+
it 'contains a first name' do
29+
expect(@contact.first_name).to be('John')
30+
end
31+
32+
it 'contains a last name' do
33+
expect(@contact.last_name).to be('Doe')
34+
end
35+
end
36+
end

spec/conversation_client_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
describe MessageBird::ConversationClient do
4+
before(:all) do
5+
@client = MessageBird::ConversationClient.new('secret-access-key')
6+
end
7+
8+
context 'initialization' do
9+
it 'uses Conversations API base URL' do
10+
expect(@client.endpoint).to eq('https://conversations.messagebird.com/v1/')
11+
end
12+
13+
it 'uses the provided access key' do
14+
expect(@client.access_key).to eq('secret-access-key')
15+
end
16+
end
17+
18+
context 'performing a HTTP request' do
19+
before(:each) do
20+
stub_request(:get, 'https://conversations.messagebird.com/v1/conversations')
21+
.with(
22+
headers: {
23+
'Accept' => 'application/json',
24+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
25+
'Authorization' => "AccessKey #{@client.access_key}",
26+
'User-Agent' => "MessageBird/ApiClient/#{MessageBird::Version::STRING} Ruby/#{RUBY_VERSION}"
27+
}
28+
)
29+
.to_return(status: 200, body: File.new(File.join(File.dirname(__FILE__), './data/conversations/list.json')), headers: {})
30+
31+
@response = JSON.parse(@client.request(:get, 'conversations'), symbolize_names: true)
32+
end
33+
34+
it 'contains an offset in the HTTP response body' do
35+
expect(@response[:offset]).to be(0)
36+
end
37+
38+
it 'contains a limit in the HTTP response body' do
39+
expect(@response[:limit]).to be(20)
40+
end
41+
42+
it 'contains a count in the HTTP response body' do
43+
expect(@response[:count]).to be(2)
44+
end
45+
46+
it 'contains a totalCount in the HTTP response body' do
47+
expect(@response[:totalCount]).to be(2)
48+
end
49+
50+
it 'contains two items' do
51+
expect(@response[:items].length).to be(2)
52+
end
53+
end
54+
end

spec/data/conversations/list.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"offset": 0,
3+
"limit": 20,
4+
"count": 2,
5+
"totalCount": 2,
6+
"items": [
7+
{
8+
"id": "fbbdde79129f45e3a179458a91e2ead6",
9+
"contactId": "03dfc27855c3475b953d6200a1b7eaf7",
10+
"contact": {
11+
"id": "03dfc27855c3475b953d6200a1b7eaf7",
12+
"href": "https://rest.messagebird.com/contacts/03dfc27855c3475b953d6200a1b7eaf7",
13+
"msisdn": 31612345678,
14+
"firstName": "John",
15+
"lastName": "Doe",
16+
"customDetails": {
17+
"custom1": null,
18+
"custom2": null,
19+
"custom3": null,
20+
"custom4": null
21+
},
22+
"createdDatetime": "2018-08-01T09:45:52Z",
23+
"updatedDatetime": "2018-08-28T12:37:35Z"
24+
},
25+
"channels": [
26+
{
27+
"id": "619747f69cf940a98fb443140ce9aed2",
28+
"name": "My WhatsApp",
29+
"platformId": "whatsapp",
30+
"status": "active",
31+
"createdDatetime": "2018-08-28T11:56:57Z",
32+
"updatedDatetime": "2018-08-29T08:16:33Z"
33+
}
34+
],
35+
"status": "active",
36+
"createdDatetime": "2018-08-29T08:52:54Z",
37+
"updatedDatetime": "2018-08-29T08:52:54Z",
38+
"lastReceivedDatetime": "2018-08-29T08:52:54Z",
39+
"lastUsedChannelId": "619747f69cf940a98fb443140ce9aed2",
40+
"lastUsedPlatformId": "whatsapp",
41+
"messages": {
42+
"totalCount": 10,
43+
"href": "https://conversations.messagebird.com/v1/conversations/fbbdde79129f45e3a179458a91e2ead6/messages"
44+
}
45+
},
46+
{
47+
"id": "2e15efafec384e1c82e9842075e87beb",
48+
"contactId": "a621095fa44947a28b441cfdf85cb802",
49+
"contact": {
50+
"id": "a621095fa44947a28b441cfdf85cb802",
51+
"href": "https://rest.messagebird.com/1/contacts/a621095fa44947a28b441cfdf85cb802",
52+
"msisdn": 316123456789,
53+
"firstName": "Jen",
54+
"lastName": "Smith",
55+
"customDetails": {
56+
"custom1": null,
57+
"custom2": null,
58+
"custom3": null,
59+
"custom4": null
60+
},
61+
"createdDatetime": "2018-06-03T20:06:03Z",
62+
"updatedDatetime": null
63+
},
64+
"channels": [
65+
{
66+
"id": "853eeb5348e541a595da93b48c61a1ae",
67+
"name": "SMS",
68+
"platformId": "sms",
69+
"status": "active",
70+
"createdDatetime": "2018-08-28T11:56:57Z",
71+
"updatedDatetime": "2018-08-29T08:16:33Z"
72+
},
73+
{
74+
"id": "619747f69cf940a98fb443140ce9aed2",
75+
"name": "My WhatsApp",
76+
"platformId": "whatsapp",
77+
"status": "active",
78+
"createdDatetime": "2018-08-28T11:56:57Z",
79+
"updatedDatetime": "2018-08-29T08:16:33Z"
80+
}
81+
],
82+
"status": "active",
83+
"createdDatetime": "2018-08-13T09:17:22Z",
84+
"updatedDatetime": "2018-08-29T07:35:48Z",
85+
"lastReceivedDatetime": "2018-08-29T07:35:48Z",
86+
"lastUsedChannelId": "853eeb5348e541a595da93b48c61a1ae",
87+
"lastUsedPlatformId": "sms",
88+
"messages": {
89+
"totalCount": 23,
90+
"href": "https://conversations.messagebird.com/v1/conversations/2e15efafec384e1c82e9842075e87beb/messages"
91+
}
92+
}
93+
]
94+
}

0 commit comments

Comments
 (0)