messagebird-rest 1.4.2 → 2.0.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.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +56 -8
  3. data/lib/messagebird.rb +10 -5
  4. data/lib/messagebird/balance.rb +2 -0
  5. data/lib/messagebird/base.rb +12 -7
  6. data/lib/messagebird/call_flow.rb +22 -0
  7. data/lib/messagebird/call_flow/step.rb +9 -0
  8. data/lib/messagebird/callflow.rb +60 -0
  9. data/lib/messagebird/client.rb +290 -85
  10. data/lib/messagebird/contact.rb +10 -8
  11. data/lib/messagebird/contact_reference.rb +3 -1
  12. data/lib/messagebird/conversation.rb +16 -14
  13. data/lib/messagebird/conversation_channel.rb +8 -7
  14. data/lib/messagebird/conversation_client.rb +9 -6
  15. data/lib/messagebird/conversation_message.rb +6 -5
  16. data/lib/messagebird/conversation_webhook.rb +4 -2
  17. data/lib/messagebird/custom_details.rb +2 -0
  18. data/lib/messagebird/error.rb +2 -0
  19. data/lib/messagebird/group.rb +7 -5
  20. data/lib/messagebird/group_reference.rb +3 -1
  21. data/lib/messagebird/hlr.rb +7 -5
  22. data/lib/messagebird/http_client.rb +34 -34
  23. data/lib/messagebird/list.rb +4 -2
  24. data/lib/messagebird/lookup.rb +7 -5
  25. data/lib/messagebird/message.rb +7 -5
  26. data/lib/messagebird/message_reference.rb +3 -1
  27. data/lib/messagebird/number.rb +18 -0
  28. data/lib/messagebird/number_client.rb +22 -0
  29. data/lib/messagebird/recipient.rb +5 -3
  30. data/lib/messagebird/signed_request.rb +24 -21
  31. data/lib/messagebird/verify.rb +7 -5
  32. data/lib/messagebird/version.rb +7 -0
  33. data/lib/messagebird/voice/base.rb +16 -0
  34. data/lib/messagebird/voice/call.rb +22 -0
  35. data/lib/messagebird/voice/call_leg.rb +11 -0
  36. data/lib/messagebird/voice/call_leg_recording.rb +20 -0
  37. data/lib/messagebird/voice/client.rb +22 -0
  38. data/lib/messagebird/voice/list.rb +24 -0
  39. data/lib/messagebird/voice/transcription.rb +15 -0
  40. data/lib/messagebird/voice/webhook.rb +11 -0
  41. data/lib/messagebird/voice_client.rb +25 -0
  42. data/lib/messagebird/voicemessage.rb +8 -6
  43. metadata +52 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d8af99a7a5eb2bb8576cfe1e880ed6443a13a962
4
- data.tar.gz: c86b5b379a2e012af6494de574619ac0b0af5cc6
2
+ SHA256:
3
+ metadata.gz: dd213489a7f5a7e70a7418ab2e3be5c4aa8cedfa61042121edb1c03dddd6d56a
4
+ data.tar.gz: 9eca63a90298b50cc4d07fb0b7ff6abc7d1ae292155e7f456df18256ae7f64e3
5
5
  SHA512:
6
- metadata.gz: a223e47afb796eb24c1cba0ff1cb8d50d3a695f1586f2805703050692631076fd98392a024b30c701ff246c8184a5ad2f19a6c747cffcd3f2d6a1d9ac74fd85d
7
- data.tar.gz: 879e546638600315f1dcb2bf3dbc45cd82f08d4f8ae7fa7949be49d26a50abe956571c711bb696adfc5a0a15ff8ae708350e5c5b5e85a25964aed9242aa8da24
6
+ metadata.gz: 373dae967d6e226cccb6f775666369b1cdc07491593cc1f5ed4fb6e7950c887723475360d4b3d04e50f00b609542190980a99ac3c09bbca5974f636791b86755
7
+ data.tar.gz: 8bc8ca63f00b5b77731ca3082497ea9cf9f8ae4738878e15d49361438adf5aa03baa2a35bc3bebec4e46a5b7c30e42cbc17be109fefee47543e0f3c3ac7410c0
data/README.md CHANGED
@@ -58,7 +58,7 @@ pp client.message_create('FromMe', '31612345678', 'Hello World', :reference => '
58
58
 
59
59
  #<MessageBird::Message:0x007f8d5b883520
60
60
  @body="Hello World",
61
- @createdDatetime=2014-07-07 12:20:30 +0200,
61
+ @created_datetime=2014-07-07 12:20:30 +0200,
62
62
  @datacoding="plain",
63
63
  @direction="mt",
64
64
  @gateway=239,
@@ -68,7 +68,7 @@ pp client.message_create('FromMe', '31612345678', 'Hello World', :reference => '
68
68
  @mclass=1,
69
69
  @originator="FromMe",
70
70
  @recipient=
71
- {"totalCount"=>1,
71
+ {"total_count"=>1,
72
72
  "totalSentCount"=>1,
73
73
  "totalDeliveredCount"=>0,
74
74
  "totalDeliveryFailedCount"=>0,
@@ -78,9 +78,9 @@ pp client.message_create('FromMe', '31612345678', 'Hello World', :reference => '
78
78
  @status="sent",
79
79
  @statusDatetime=2014-07-07 12:20:30 +0200>]},
80
80
  @reference="MyReference",
81
- @scheduledDatetime=nil,
81
+ @scheduled_datetime=nil,
82
82
  @type="sms",
83
- @typeDetails={},
83
+ @type_details={},
84
84
  @validity=nil>
85
85
  ```
86
86
 
@@ -97,7 +97,7 @@ To perform HLR lookups we have created the **hlr_create** method, which takes a
97
97
  pp client.hlr_create('31612345678', 'MyReference')
98
98
 
99
99
  #<MessageBird::HLR:0x007f8d5b8dafc8
100
- @createdDatetime=2014-07-07 12:20:05 +0200,
100
+ @created_datetime=2014-07-07 12:20:05 +0200,
101
101
  @href="https://rest.messagebird.com/hlr/4933bed0453ba7455031712h16830892",
102
102
  @id="4933bed0453ba7455031712h16830892",
103
103
  @msisdn=31612345678,
@@ -126,7 +126,7 @@ client.verify_create(31612345678, {:reference => "YourReference"})
126
126
  @reference="YourReference",
127
127
  @status="sent",
128
128
  @href={"message"=>"https://rest.messagebird.com/messages/67d42f004555213679416f0b13254392"},
129
- @createdDatetime=2015-05-12 16:51:19 +0200,
129
+ @created_datetime=2015-05-12 16:51:19 +0200,
130
130
  @validUntilDatetime=2015-05-12 16:51:49 +0200>
131
131
  ```
132
132
 
@@ -147,14 +147,14 @@ pp client.voice_message_create('31612345678', 'Hello World', :reference => 'MyRe
147
147
 
148
148
  #<MessageBird::VoiceMessage:0x000001030101b8
149
149
  @body="Hello World",
150
- @createdDatetime=2014-07-09 12:17:50 +0200,
150
+ @created_datetime=2014-07-09 12:17:50 +0200,
151
151
  @href=
152
152
  "https://rest.messagebird.com/voicemessages/a08e51a0353bd16cea7f298a37405850",
153
153
  @id="a08e51a0353bd16cea7f298a37405850",
154
154
  @ifMachine="continue",
155
155
  @language="en-gb",
156
156
  @recipients=
157
- {"totalCount"=>1,
157
+ {"total_count"=>1,
158
158
  "totalSentCount"=>1,
159
159
  "totalDeliveredCount"=>0,
160
160
  "totalDeliveryFailedCount"=>0,
@@ -175,6 +175,54 @@ Similar to regular messaging and HLR lookups, there is a method available to fet
175
175
  client.voice_message('a08e51a0353bd16cea7f298a37405850')
176
176
  ```
177
177
 
178
+ ##### Numbers
179
+ There is also a Numbers API that allow you to search for and purchase number subscriptions to use as originator in other services.
180
+
181
+ ```ruby
182
+ pp client.number_search("NL", {:limit=>5})
183
+
184
+ #<List:0x00007fa405130618
185
+ @count=5,
186
+ @items=
187
+ [#<MessageBird::Number:0x00007fa405130528
188
+ @country="NL",
189
+ @features=["voice"],
190
+ @locality="Rotterdam",
191
+ @number="31102005108",
192
+ @region="",
193
+ @type="unknown">,
194
+ #<MessageBird::Number:0x00007fa4051303c0
195
+ @country="NL",
196
+ @features=["voice"],
197
+ @locality="Rotterdam",
198
+ @number="31102005143",
199
+ @region="",
200
+ @type="unknown">,
201
+ #<MessageBird::Number:0x00007fa405130208
202
+ @country="NL",
203
+ @features=["voice"],
204
+ @locality="Rotterdam",
205
+ @number="31102005145",
206
+ @region="",
207
+ @type="unknown">,
208
+ #<MessageBird::Number:0x00007fa4051300c8
209
+ @country="NL",
210
+ @features=["voice"],
211
+ @locality="Rotterdam",
212
+ @number="31102005147",
213
+ @region="",
214
+ @type="unknown">,
215
+ #<MessageBird::Number:0x00007fa405131c48
216
+ @country="NL",
217
+ @features=["voice"],
218
+ @locality="Rotterdam",
219
+ @number="31102005148",
220
+ @region="",
221
+ @type="unknown">],
222
+ @limit=5,
223
+ @type=MessageBird::Number>
224
+ ````
225
+
178
226
  Documentation
179
227
  -------------
180
228
  Complete documentation, instructions, and examples are available at:
data/lib/messagebird.rb CHANGED
@@ -1,10 +1,9 @@
1
- libdir = File.dirname(__FILE__)
2
- $:.unshift(libdir) unless $:.include?(libdir)
1
+ # frozen_string_literal: true
3
2
 
4
- module MessageBird
5
- CLIENT_VERSION = '1.4.2'
6
- end
3
+ libdir = File.dirname(__FILE__)
4
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
7
5
 
6
+ require 'messagebird/version'
8
7
  require 'messagebird/balance'
9
8
  require 'messagebird/client'
10
9
  require 'messagebird/contact'
@@ -17,3 +16,9 @@ require 'messagebird/signed_request'
17
16
  require 'messagebird/verify'
18
17
  require 'messagebird/message'
19
18
  require 'messagebird/voicemessage'
19
+ require 'messagebird/callflow'
20
+ require 'messagebird/voice/call'
21
+ require 'messagebird/voice/call_leg'
22
+ require 'messagebird/voice/call_leg_recording'
23
+ require 'messagebird/voice/transcription'
24
+ require 'messagebird/voice/webhook'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'messagebird/base'
2
4
 
3
5
  module MessageBird
@@ -1,18 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'time'
3
5
 
4
6
  module MessageBird
5
7
  class Base
6
- def initialize(json)
7
- json.each do |k,v|
8
- begin
9
- send("#{k}=", v)
10
- rescue NoMethodError
11
- # Silently ignore parameters that are not supported.
12
- end
8
+ # takes each element from the given hash and apply it to ourselves through an assignment method
9
+ def map_hash_elements_to_self(hash)
10
+ hash.each do |key, value|
11
+ method_name = key.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase # convert came case to snake case
12
+ method_name += '='
13
+ send(method_name, value) if respond_to?(method_name)
13
14
  end
14
15
  end
15
16
 
17
+ def initialize(json)
18
+ map_hash_elements_to_self(json)
19
+ end
20
+
16
21
  def value_to_time(value)
17
22
  value ? Time.parse(value) : nil
18
23
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'messagebird/base'
4
+
5
+ module MessageBird
6
+ class CallFlow < MessageBird::Base
7
+ attr_accessor :id, :title, :record, :default
8
+ attr_reader :steps, :created_at, :updated_at
9
+
10
+ def steps=(json)
11
+ @steps = json.map { |s| MessageBird::CallFlowStep.new(s) }
12
+ end
13
+
14
+ def created_at=(value)
15
+ @created_at = value_to_time(value)
16
+ end
17
+
18
+ def updated_at=(value)
19
+ @updated_at = value_to_time(value)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'messagebird/base'
4
+
5
+ module MessageBird
6
+ class CallFlowStep < MessageBird::Base
7
+ attr_accessor :id, :action, :options
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'messagebird/base'
4
+
5
+ module MessageBird
6
+ class CallFlow < MessageBird::Base
7
+ attr_accessor :id, :title, :record, :default
8
+ attr_reader :steps, :created_at, :updated_at
9
+
10
+ def initialize(json)
11
+ params = json.include?('data') ? json['data'].first : json
12
+ super(params)
13
+ end
14
+
15
+ def steps=(json)
16
+ @steps = json.map { |c| MessageBird::CallFlowStep.new(c) }
17
+ end
18
+
19
+ def created_at=(value)
20
+ @created_at = value_to_time(value)
21
+ end
22
+
23
+ def updated_at=(value)
24
+ @updated_at = value_to_time(value)
25
+ end
26
+ end
27
+
28
+ class CallFlowList < List
29
+ attr_accessor :perPage, :currentPage, :pageCount, :totalCount
30
+ PER_PAGE = 20
31
+ CURRENT_PAGE = 1
32
+
33
+ def data=(value)
34
+ self.items = value
35
+ end
36
+
37
+ def pagination=(value)
38
+ value.each do |k, v|
39
+ send("#{k}=", v)
40
+ rescue NoMethodError
41
+ puts 'An error occurred while listing callflows'
42
+ end
43
+ end
44
+ end
45
+
46
+ class CallFlowStep < MessageBird::Base
47
+ attr_accessor :id, :action
48
+
49
+ def options=(json)
50
+ @options = CallFlowStepOption.new(json)
51
+ end
52
+ end
53
+
54
+ class CallFlowStepOption < MessageBird::Base
55
+ attr_accessor :destination, :payload, :language, :voice, :repeat,
56
+ :media, :length, :maxLength, :timeout, :finishOnKey, :transcribe,
57
+ :transcribeLanguage, :record, :url, :ifMachine, :machineTimeout,
58
+ :onFinish, :mask
59
+ end
60
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'net/https'
3
5
  require 'uri'
@@ -15,8 +17,19 @@ require 'messagebird/http_client'
15
17
  require 'messagebird/list'
16
18
  require 'messagebird/lookup'
17
19
  require 'messagebird/message'
20
+ require 'messagebird/number'
21
+ require 'messagebird/number_client'
18
22
  require 'messagebird/verify'
23
+ require 'messagebird/voice/client'
24
+ require 'messagebird/voice/list'
25
+ require 'messagebird/voice/webhook'
19
26
  require 'messagebird/voicemessage'
27
+ require 'messagebird/voice_client'
28
+ require 'messagebird/voice/call'
29
+ require 'messagebird/voice/call_leg'
30
+ require 'messagebird/voice/call_leg_recording'
31
+ require 'messagebird/voice/transcription'
32
+ require 'messagebird/voice/list'
20
33
 
21
34
  module MessageBird
22
35
  class ErrorException < StandardError
@@ -27,24 +40,45 @@ module MessageBird
27
40
  end
28
41
  end
29
42
 
43
+ class InvalidFeatureException < StandardError
44
+ end
45
+
30
46
  class Client
31
- attr_reader :access_key, :http_client, :conversation_client
47
+ attr_reader :access_key, :http_client, :conversation_client, :voice_client
32
48
 
33
- def initialize(access_key = nil, http_client = nil)
49
+ def initialize(access_key = nil, http_client = nil, conversation_client = nil, voice_client = nil)
34
50
  @access_key = access_key || ENV['MESSAGEBIRD_ACCESS_KEY']
35
51
  @http_client = http_client || HttpClient.new(@access_key)
36
- @conversation_client = http_client || ConversationClient.new(@access_key)
52
+ @conversation_client = conversation_client || ConversationClient.new(@access_key)
53
+ @number_client = http_client || NumberClient.new(@access_key)
54
+ @voice_client = voice_client || VoiceClient.new(@access_key)
37
55
  end
38
56
 
39
- def conversation_request(method, path, params={})
57
+ def conversation_request(method, path, params = {})
40
58
  response_body = @conversation_client.request(method, path, params)
41
59
  return if response_body.nil? || response_body.empty?
60
+
61
+ parse_body(response_body)
62
+ end
63
+
64
+ def number_request(method, path, params = {})
65
+ response_body = @number_client.request(method, path, params)
66
+ return if response_body.nil? || response_body.empty?
67
+
68
+ parse_body(response_body)
69
+ end
70
+
71
+ def voice_request(method, path, params = {})
72
+ response_body = @voice_client.request(method, path, params)
73
+ return if response_body.nil? || response_body.empty?
74
+
42
75
  parse_body(response_body)
43
76
  end
44
77
 
45
- def request(method, path, params={})
78
+ def request(method, path, params = {})
46
79
  response_body = @http_client.request(method, path, params)
47
80
  return if response_body.empty?
81
+
48
82
  parse_body(response_body)
49
83
  end
50
84
 
@@ -52,8 +86,8 @@ module MessageBird
52
86
  json = JSON.parse(body)
53
87
 
54
88
  # If the request returned errors, create Error objects and raise.
55
- if json.has_key?('errors')
56
- raise ErrorException, json['errors'].map { |e| Error.new(e) }
89
+ if json.key?('errors')
90
+ raise(ErrorException, json['errors'].map { |e| Error.new(e) })
57
91
  end
58
92
 
59
93
  json
@@ -61,29 +95,36 @@ module MessageBird
61
95
 
62
96
  ## Conversations
63
97
  # Send a conversation message
64
- def send_conversation_message(from, to, params={})
98
+ def send_conversation_message(from, to, params = {})
65
99
  ConversationMessage.new(conversation_request(
66
- :post,
67
- 'send',
68
- params.merge({
69
- :from => from,
70
- :to => to,
71
- })))
100
+ :post,
101
+ 'send',
102
+ params.merge(from: from,
103
+ to: to)
104
+ ))
72
105
  end
73
106
 
74
107
  # Start a conversation
75
- def start_conversation(to, channelId, params={})
108
+ def start_conversation(to, channel_id, params = {})
76
109
  Conversation.new(conversation_request(
77
- :post,
78
- 'conversations/start',
79
- params.merge({
80
- :to => to,
81
- :channelId => channelId,
82
- })))
110
+ :post,
111
+ 'conversations/start',
112
+ params.merge(to: to,
113
+ channel_id: channel_id)
114
+ ))
83
115
  end
84
116
 
85
- def conversation_list(limit = 0, offset = 0)
86
- List.new(Conversation, conversation_request(:get, "conversations?limit=#{limit}&offset=#{offset}"))
117
+ def conversation_list(limit = -1, offset = -1)
118
+ query = '?'
119
+ if limit != -1
120
+ query += "limit=#{limit}&"
121
+ end
122
+
123
+ if offset != -1
124
+ query += "offset=#{offset}"
125
+ end
126
+
127
+ List.new(Conversation, conversation_request(:get, "conversations#{query}"))
87
128
  end
88
129
 
89
130
  def conversation(id)
@@ -91,45 +132,54 @@ module MessageBird
91
132
  end
92
133
 
93
134
  def conversation_update(id, status)
94
- Conversation.new(conversation_request(:patch, "conversations/#{id}", :status => status))
135
+ Conversation.new(conversation_request(:patch, "conversations/#{id}", status: status))
95
136
  end
96
-
97
- def conversation_reply(id, params={})
137
+
138
+ def conversation_reply(id, params = {})
98
139
  ConversationMessage.new(conversation_request(:post, "conversations/#{id}/messages", params))
99
140
  end
100
-
101
- def conversation_messages_list(id, limit=0, offset=0)
102
- List.new(ConversationMessage, conversation_request(:get, "conversations/#{id}/messages?limit=#{limit}&offset=#{offset}"))
141
+
142
+ def conversation_messages_list(id, limit = -1, offset = -1)
143
+ query = '?'
144
+ if limit != -1
145
+ query += "limit=#{limit}&"
146
+ end
147
+
148
+ if offset != -1
149
+ query += "offset=#{offset}"
150
+ end
151
+
152
+ List.new(ConversationMessage, conversation_request(:get, "conversations/#{id}/messages#{query}"))
103
153
  end
104
-
154
+
105
155
  def conversation_message(id)
106
156
  ConversationMessage.new(conversation_request(:get, "messages/#{id}"))
107
157
  end
108
-
109
- def conversation_webhook_create(channelId, url, events=[])
158
+
159
+ def conversation_webhook_create(channel_id, url, events = [])
110
160
  ConversationWebhook.new(conversation_request(
111
- :post,
112
- "webhooks",
113
- :channelId => channelId,
114
- :url => url,
115
- :events => events
161
+ :post,
162
+ 'webhooks',
163
+ channel_id: channel_id,
164
+ url: url,
165
+ events: events
116
166
  ))
117
167
  end
118
168
 
119
- def conversation_webhooks_list(limit=0, offset=0)
120
- List.new(ConversationWebhook, conversation_request(:get,"webhooks?limit=#{limit}&offset=#{offset}"))
169
+ def conversation_webhooks_list(limit = 0, offset = 0)
170
+ List.new(ConversationWebhook, conversation_request(:get, "webhooks?limit=#{limit}&offset=#{offset}"))
121
171
  end
122
172
 
123
- def conversation_webhook_update(id, params={})
124
- ConversationWebhook.new(conversation_request(:patch,"webhooks/#{id}",params))
173
+ def conversation_webhook_update(id, params = {})
174
+ ConversationWebhook.new(conversation_request(:patch, "webhooks/#{id}", params))
125
175
  end
126
176
 
127
177
  def conversation_webhook(id)
128
- ConversationWebhook.new(conversation_request(:get,"webhooks/#{id}"))
178
+ ConversationWebhook.new(conversation_request(:get, "webhooks/#{id}"))
129
179
  end
130
180
 
131
181
  def conversation_webhook_delete(id)
132
- conversation_request(:delete,"webhooks/#{id}")
182
+ conversation_request(:delete, "webhooks/#{id}")
133
183
  end
134
184
 
135
185
  # Retrieve your balance.
@@ -139,96 +189,186 @@ module MessageBird
139
189
 
140
190
  # Retrieve the information of specific HLR.
141
191
  def hlr(id)
142
- HLR.new(request(:get, "hlr/#{id.to_s}"))
192
+ HLR.new(request(:get, "hlr/#{id}"))
143
193
  end
144
194
 
145
195
  # Create a new HLR.
146
196
  def hlr_create(msisdn, reference)
147
197
  HLR.new(request(
148
- :post,
149
- 'hlr',
150
- :msisdn => msisdn,
151
- :reference => reference))
198
+ :post,
199
+ 'hlr',
200
+ msisdn: msisdn,
201
+ reference: reference
202
+ ))
152
203
  end
153
204
 
154
205
  # Retrieve the information of specific Verify.
155
206
  def verify(id)
156
- Verify.new(request(:get, "verify/#{id.to_s}"))
207
+ Verify.new(request(:get, "verify/#{id}"))
157
208
  end
158
209
 
159
210
  # Generate a new One-Time-Password message.
160
- def verify_create(recipient, params={})
211
+ def verify_create(recipient, params = {})
161
212
  Verify.new(request(
162
- :post,
163
- 'verify',
164
- params.merge({
165
- :recipient => recipient
166
- })
213
+ :post,
214
+ 'verify',
215
+ params.merge(recipient: recipient)
167
216
  ))
168
217
  end
169
218
 
170
219
  # Verify the One-Time-Password.
171
220
  def verify_token(id, token)
172
- Verify.new(request(:get, "verify/#{id.to_s}?token=#{token}"))
221
+ Verify.new(request(:get, "verify/#{id}?token=#{token}"))
173
222
  end
174
223
 
175
224
  # Delete a Verify
176
225
  def verify_delete(id)
177
- Verify.new(request(:delete, "verify/#{id.to_s}"))
226
+ Verify.new(request(:delete, "verify/#{id}"))
178
227
  end
179
228
 
180
229
  # Retrieve the information of specific message.
181
230
  def message(id)
182
- Message.new(request(:get, "messages/#{id.to_s}"))
231
+ Message.new(request(:get, "messages/#{id}"))
232
+ end
233
+
234
+ # Retrieve messages with optional paging and status filter.
235
+ def message_list(filter = {})
236
+ limit = filter[:limit] || 10
237
+ offset = filter[:offset] || 0
238
+ status = filter[:status] || ''
239
+
240
+ params = { limit: limit, offset: offset }
241
+ if status != ''
242
+ params['status'] = status
243
+ end
244
+ query = 'messages?' + URI.encode_www_form(params)
245
+ List.new(Message, request(:get, query))
183
246
  end
184
247
 
185
248
  # Create a new message.
186
- def message_create(originator, recipients, body, params={})
249
+ def message_create(originator, recipients, body, params = {})
187
250
  # Convert an array of recipients to a comma-separated string.
188
- recipients = recipients.join(',') if recipients.kind_of?(Array)
251
+ recipients = recipients.join(',') if recipients.is_a?(Array)
189
252
 
190
253
  Message.new(request(
191
- :post,
192
- 'messages',
193
- params.merge({
194
- :originator => originator.to_s,
195
- :body => body.to_s,
196
- :recipients => recipients })))
254
+ :post,
255
+ 'messages',
256
+ params.merge(originator: originator.to_s,
257
+ body: body.to_s,
258
+ recipients: recipients)
259
+ ))
197
260
  end
198
261
 
199
262
  # Retrieve the information of a specific voice message.
200
263
  def voice_message(id)
201
- VoiceMessage.new(request(:get, "voicemessages/#{id.to_s}"))
264
+ VoiceMessage.new(request(:get, "voicemessages/#{id}"))
202
265
  end
203
266
 
204
267
  # Create a new voice message.
205
- def voice_message_create(recipients, body, params={})
268
+ def voice_message_create(recipients, body, params = {})
206
269
  # Convert an array of recipients to a comma-separated string.
207
- recipients = recipients.join(',') if recipients.kind_of?(Array)
270
+ recipients = recipients.join(',') if recipients.is_a?(Array)
208
271
 
209
272
  VoiceMessage.new(request(
210
- :post,
211
- 'voicemessages',
212
- params.merge({ :recipients => recipients, :body => body.to_s })))
273
+ :post,
274
+ 'voicemessages',
275
+ params.merge(recipients: recipients, body: body.to_s)
276
+ ))
277
+ end
278
+
279
+ def voice_webhook_create(url, params = {})
280
+ Voice::Webhook.new(voice_request(:post, 'webhooks', params.merge(url: url)))
281
+ end
282
+
283
+ def voice_webhooks_list(per_page = VoiceList::PER_PAGE, page = VoiceList::CURRENT_PAGE)
284
+ Voice::List.new(Voice::Webhook, voice_request(:get, "webhooks?perPage=#{per_page}&page=#{page}"))
285
+ end
286
+
287
+ def voice_webhook_update(id, params = {})
288
+ Voice::Webhook.new(voice_request(:put, "webhooks/#{id}", params))
289
+ end
290
+
291
+ def voice_webhook(id)
292
+ Voice::Webhook.new(voice_request(:get, "webhooks/#{id}"))
293
+ end
294
+
295
+ def voice_webhook_delete(id)
296
+ voice_request(:delete, "webhooks/#{id}")
297
+ end
298
+
299
+ def call_create(source, destination, call_flow = {}, webhook = {}, params = {})
300
+ params = params.merge(callFlow: call_flow.to_json) unless call_flow.empty?
301
+ params = params.merge(webhook: webhook.to_json) unless webhook.empty?
302
+
303
+ Voice::Call.new(voice_request(:post, 'calls', params.merge(source: source, destination: destination)))
304
+ end
305
+
306
+ def call_list(per_page = Voice::List::PER_PAGE, page = Voice::List::CURRENT_PAGE)
307
+ Voice::List.new(Voice::Call, voice_request(:get, "calls?perPage=#{per_page}&currentPage=#{page}"))
308
+ end
309
+
310
+ def call_view(id)
311
+ Voice::Call.new(voice_request(:get, "calls/#{id}"))
312
+ end
313
+
314
+ def call_delete(id)
315
+ voice_request(:delete, "calls/#{id}")
316
+ end
317
+
318
+ def call_leg_list(call_id, per_page = Voice::List::PER_PAGE, current_page = Voice::List::CURRENT_PAGE)
319
+ Voice::List.new(Voice::CallLeg, voice_request(:get, "calls/#{call_id}/legs?perPage=#{per_page}&currentPage=#{current_page}"))
320
+ end
321
+
322
+ def call_leg_recording_view(call_id, leg_id, recording_id)
323
+ Voice::CallLegRecording.new(voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}"))
324
+ end
325
+
326
+ def call_leg_recording_list(call_id, leg_id)
327
+ Voice::List.new(Voice::CallLegRecording, voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings"))
328
+ end
329
+
330
+ def call_leg_recording_delete(call_id, leg_id, recording_id)
331
+ voice_request(:delete, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}")
332
+ end
333
+
334
+ def call_leg_recording_download(recording_uri, &block)
335
+ @voice_client.request_block(:get, recording_uri, {}, &block)
336
+ end
337
+
338
+ def voice_transcription_create(call_id, leg_id, recording_id, params = {})
339
+ Voice::Transcription.new(voice_request(:post, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions", params))
340
+ end
341
+
342
+ def voice_transcriptions_list(call_id, leg_id, recording_id)
343
+ Voice::List.new(Voice::Transcription, voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions"))
344
+ end
345
+
346
+ def voice_transcription_download(call_id, leg_id, recording_id, transcription_id, &block)
347
+ @voice_client.request_block(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions/#{transcription_id}.txt", {}, &block)
348
+ end
349
+
350
+ def voice_transcription_view(call_id, leg_id, recording_id, transcription_id)
351
+ Voice::Transcription.new(voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions/#{transcription_id}"))
213
352
  end
214
353
 
215
- def lookup(phoneNumber, params={})
216
- Lookup.new(request(:get, "lookup/#{phoneNumber}", params))
354
+ def lookup(phone_number, params = {})
355
+ Lookup.new(request(:get, "lookup/#{phone_number}", params))
217
356
  end
218
357
 
219
- def lookup_hlr_create(phoneNumber, params={})
220
- HLR.new(request(:post, "lookup/#{phoneNumber}/hlr", params))
358
+ def lookup_hlr_create(phone_number, params = {})
359
+ HLR.new(request(:post, "lookup/#{phone_number}/hlr", params))
221
360
  end
222
361
 
223
- def lookup_hlr(phoneNumber, params={})
224
- HLR.new(request(:get, "lookup/#{phoneNumber}/hlr", params))
362
+ def lookup_hlr(phone_number, params = {})
363
+ HLR.new(request(:get, "lookup/#{phone_number}/hlr", params))
225
364
  end
226
365
 
227
- def contact_create(phoneNumber, params={})
366
+ def contact_create(phone_number, params = {})
228
367
  Contact.new(request(
229
- :post,
230
- 'contacts',
231
- params.merge({ :msisdn => phoneNumber.to_s })))
368
+ :post,
369
+ 'contacts',
370
+ params.merge(msisdn: phone_number.to_s)
371
+ ))
232
372
  end
233
373
 
234
374
  def contact(id)
@@ -239,7 +379,7 @@ module MessageBird
239
379
  request(:delete, "contacts/#{id}")
240
380
  end
241
381
 
242
- def contact_update(id, params={})
382
+ def contact_update(id, params = {})
243
383
  request(:patch, "contacts/#{id}", params)
244
384
  end
245
385
 
@@ -252,7 +392,7 @@ module MessageBird
252
392
  end
253
393
 
254
394
  def group_create(name)
255
- Group.new(request(:post, 'groups', { :name => name }))
395
+ Group.new(request(:post, 'groups', name: name))
256
396
  end
257
397
 
258
398
  def group_delete(id)
@@ -264,7 +404,7 @@ module MessageBird
264
404
  end
265
405
 
266
406
  def group_update(id, name)
267
- request(:patch, "groups/#{id}", { :name => name })
407
+ request(:patch, "groups/#{id}", name: name)
268
408
  end
269
409
 
270
410
  def group_add_contacts(group_id, contact_ids)
@@ -280,8 +420,68 @@ module MessageBird
280
420
  request(:delete, "groups/#{group_id}/contacts/#{contact_id}")
281
421
  end
282
422
 
423
+ ## Numbers API
424
+ # Search for available numbers
425
+ def number_search(country_code, params = {})
426
+ List.new(Number, number_request(:get, add_querystring("available-phone-numbers/#{country_code}", params), params))
427
+ end
428
+
429
+ # Purchase an avaiable number
430
+ def number_purchase(number, country_code, billing_interval_months)
431
+ params = {
432
+ number: number,
433
+ countryCode: country_code,
434
+ billingIntervalMonths: billing_interval_months
435
+ }
436
+ Number.new(number_request(:post, 'phone-numbers', params))
437
+ end
438
+
439
+ # Fetch all purchaed numbers' details
440
+ def number_fetch_all(params = {})
441
+ List.new(Number, number_request(:get, add_querystring('phone-numbers', params), params))
442
+ end
443
+
444
+ # Fetch specific purchased number's details
445
+ def number_fetch(number)
446
+ Number.new(number_request(:get, "phone-numbers/#{number}"))
447
+ end
448
+
449
+ # Update a number
450
+ def number_update(number, tags)
451
+ tags = [tags] if tags.is_a? String
452
+ Number.new(number_request(:patch, "phone-numbers/#{number}", tags: tags))
453
+ end
454
+
455
+ # Cancel a number
456
+ def number_cancel(number)
457
+ number_request(:delete, "phone-numbers/#{number}")
458
+ end
459
+
460
+ def call_flow_create(title, steps, default, record, params = {})
461
+ params = params.merge(
462
+ title: title,
463
+ steps: steps,
464
+ default: default,
465
+ record: record
466
+ )
467
+ CallFlow.new(voice_request(:post, 'call-flows', params))
468
+ end
469
+
470
+ def call_flow_view(id)
471
+ CallFlow.new(voice_request(:get, "call-flows/#{id}"))
472
+ end
473
+
474
+ def call_flow_list(per_page = CallFlowList::PER_PAGE, page = CallFlowList::CURRENT_PAGE)
475
+ CallFlowList.new(CallFlow, voice_request(:get, "call-flows?perPage=#{per_page}&page=#{page}"))
476
+ end
477
+
478
+ def call_flow_delete(id)
479
+ voice_request(:delete, "call-flows/#{id}")
480
+ end
481
+
283
482
  private # Applies to every method below this line
284
483
 
484
+ # Applies to every method below this line
285
485
  def add_contacts_query(contact_ids)
286
486
  # add_contacts_query gets a query string to add contacts to a group.
287
487
  # We're using the alternative "/foo?_method=PUT&key=value" format to send
@@ -293,5 +493,10 @@ module MessageBird
293
493
  '_method=PUT&' + contact_ids.map { |id| "ids[]=#{id}" }.join('&')
294
494
  end
295
495
 
496
+ def add_querystring(path, params)
497
+ return path if params.empty?
498
+
499
+ "#{path}?" + params.collect { |k, v| v.is_a?(Array) ? v.collect { |sv| "#{k}=#{sv}" }.join('&') : "#{k}=#{v}" }.join('&')
500
+ end
296
501
  end
297
502
  end