messagebird-rest 1.3.3 → 1.4.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: 4726ce8ee0e2583a41d1f73a7e6866dda139fb14
4
- data.tar.gz: 484cd5e20ed84a89ea58e969289e58daa2d02bdd
3
+ metadata.gz: ed916adf077ac2e3a03631e2d97fad4a6c9e9c97
4
+ data.tar.gz: 01aab619642825c19ef2b939a284c138e28542b2
5
5
  SHA512:
6
- metadata.gz: 79a490c8dd3d3af74ff5ab0bb45209982f35dae182fc6b83cb90177e5b75a60afd36c56bb4285f0affbc1fff15cc7eacf0542e88a1fe742fc88c5a39eee11985
7
- data.tar.gz: 6869c83755659c52549eee08b411076a42ed4c469d7ba1b8016f1b3b1668e681db46d8439ac32c2fcb32054207db9a51cdbf76d57c76ddf3d352af121c6ef26b
6
+ metadata.gz: bfc589631d306c6cdfa0464ed2da03f88c4a7c88846ed0038e48dd20ab8e5e4ddae03a2bdd34c7371e4ce21f3b72b4c0798d43dfcd43f3d359e8e22ec5c85ac8
7
+ data.tar.gz: 799b7b82c6f6f8839986bdedba3abd47984f6a809b388f6713744b2e2c102a6503ff9cdd02e3baca98832117eaf116fbffa32fddaeea57de02d75c26a2b8a471
data/README.md CHANGED
@@ -2,6 +2,8 @@ MessageBird's REST API for Ruby
2
2
  ===============================
3
3
  This repository contains the open source Ruby client for MessageBird's REST API. Documentation can be found at: https://developers.messagebird.com/
4
4
 
5
+ [![Build Status](https://travis-ci.org/messagebird/ruby-rest-api.svg?branch=master)](https://travis-ci.org/messagebird/ruby-rest-api)
6
+
5
7
  Requirements
6
8
  ------------
7
9
  - [Sign up](https://dashboard.messagebird.com/app/en/sign-up) for a free MessageBird account
@@ -2,14 +2,18 @@ libdir = File.dirname(__FILE__)
2
2
  $:.unshift(libdir) unless $:.include?(libdir)
3
3
 
4
4
  module MessageBird
5
- CLIENT_VERSION = '1.3.3'
5
+ CLIENT_VERSION = '1.4.0'
6
6
  ENDPOINT = 'https://rest.messagebird.com'
7
7
  end
8
8
 
9
9
  require 'messagebird/balance'
10
10
  require 'messagebird/client'
11
+ require 'messagebird/contact'
11
12
  require 'messagebird/error'
13
+ require 'messagebird/group_reference'
12
14
  require 'messagebird/hlr'
15
+ require 'messagebird/http_client'
16
+ require 'messagebird/message_reference'
13
17
  require 'messagebird/verify'
14
18
  require 'messagebird/message'
15
19
  require 'messagebird/voicemessage'
@@ -3,12 +3,16 @@ require 'net/https'
3
3
  require 'uri'
4
4
 
5
5
  require 'messagebird/balance'
6
+ require 'messagebird/contact'
6
7
  require 'messagebird/error'
8
+ require 'messagebird/group'
7
9
  require 'messagebird/hlr'
8
- require 'messagebird/verify'
10
+ require 'messagebird/http_client'
11
+ require 'messagebird/list'
12
+ require 'messagebird/lookup'
9
13
  require 'messagebird/message'
14
+ require 'messagebird/verify'
10
15
  require 'messagebird/voicemessage'
11
- require 'messagebird/lookup'
12
16
 
13
17
  module MessageBird
14
18
  class ErrorException < StandardError
@@ -19,42 +23,21 @@ module MessageBird
19
23
  end
20
24
  end
21
25
 
22
- class InvalidPhoneNumberException < TypeError; end
23
-
24
26
  class Client
27
+
25
28
  attr_reader :access_key
29
+ attr_reader :http_client
26
30
 
27
- def initialize(access_key = nil)
31
+ def initialize(access_key = nil, http_client = nil)
28
32
  @access_key = access_key || ENV['MESSAGEBIRD_ACCESS_KEY']
33
+ @http_client = http_client || HttpClient.new(@access_key)
29
34
  end
30
35
 
31
36
  def request(method, path, params={})
32
- uri = URI.join(ENDPOINT, '/', path)
33
-
34
- # Set up the HTTP object.
35
- http = Net::HTTP.new(uri.host, uri.port)
36
- http.use_ssl = true
37
-
38
- # Construct the HTTP GET or POST request.
39
- request = Net::HTTP::Get.new(uri.request_uri) if method == :get
40
- request = Net::HTTP::Post.new(uri.request_uri) if method == :post
41
- request['Accept'] = 'application/json'
42
- request['Authorization'] = "AccessKey #{@access_key}"
43
- request['User-Agent'] = "MessageBird/ApiClient/#{CLIENT_VERSION} Ruby/#{RUBY_VERSION}"
44
-
45
- # If present, add the HTTP POST parameters.
46
- request.set_form_data(params) if method == :post && !params.empty?
47
-
48
- # Execute the request and fetch the response.
49
- response = http.request(request)
50
-
51
- # Parse the HTTP response.
52
- case response.code.to_i
53
- when 200, 201, 204, 401, 404, 405, 422
54
- json = JSON.parse(response.body)
55
- else
56
- raise InvalidPhoneNumberException, 'Unknown response from server'
57
- end
37
+ response_body = @http_client.request(method, path, params)
38
+ return if response_body.empty?
39
+
40
+ json = JSON.parse(response_body)
58
41
 
59
42
  # If the request returned errors, create Error objects and raise.
60
43
  if json.has_key?('errors')
@@ -156,5 +139,74 @@ module MessageBird
156
139
  HLR.new(request(:get, "lookup/#{phoneNumber}/hlr", params))
157
140
  end
158
141
 
142
+ def contact_create(phoneNumber, params={})
143
+ Contact.new(request(
144
+ :post,
145
+ 'contacts',
146
+ params.merge({ :msisdn => phoneNumber.to_s })))
147
+ end
148
+
149
+ def contact(id)
150
+ Contact.new(request(:get, "contacts/#{id}"))
151
+ end
152
+
153
+ def contact_delete(id)
154
+ request(:delete, "contacts/#{id}")
155
+ end
156
+
157
+ def contact_update(id, params={})
158
+ request(:patch, "contacts/#{id}", params)
159
+ end
160
+
161
+ def contact_list(limit = 0, offset = 0)
162
+ List.new(Contact, request(:get, "contacts?limit=#{limit}&offset=#{offset}"))
163
+ end
164
+
165
+ def group(id)
166
+ Group.new(request(:get, "groups/#{id}"))
167
+ end
168
+
169
+ def group_create(name)
170
+ Group.new(request(:post, 'groups', { :name => name }))
171
+ end
172
+
173
+ def group_delete(id)
174
+ request(:delete, "groups/#{id}")
175
+ end
176
+
177
+ def group_list(limit = 0, offset = 0)
178
+ List.new(Group, request(:get, "groups?limit=#{limit}&offset=#{offset}"))
179
+ end
180
+
181
+ def group_update(id, name)
182
+ request(:patch, "groups/#{id}", { :name => name })
183
+ end
184
+
185
+ def group_add_contacts(group_id, contact_ids)
186
+ # We expect an array, but we can handle a string ID as well...
187
+ contact_ids = [contact_ids] if contact_ids.is_a? String
188
+
189
+ query = add_contacts_query(contact_ids)
190
+
191
+ request(:get, "groups/#{group_id}?#{query}")
192
+ end
193
+
194
+ def group_delete_contact(group_id, contact_id)
195
+ request(:delete, "groups/#{group_id}/contacts/#{contact_id}")
196
+ end
197
+
198
+ private # Applies to every method below this line
199
+
200
+ def add_contacts_query(contact_ids)
201
+ # add_contacts_query gets a query string to add contacts to a group.
202
+ # We're using the alternative "/foo?_method=PUT&key=value" format to send
203
+ # the contact IDs as GET params. Sending these in the request body would
204
+ # require a painful workaround, as the client sends request bodies as
205
+ # JSON by default. See also:
206
+ # https://developers.messagebird.com/docs/alternatives.
207
+
208
+ '_method=PUT&' + contact_ids.map { |id| "ids[]=#{id}" }.join('&')
209
+ end
210
+
159
211
  end
160
212
  end
@@ -0,0 +1,31 @@
1
+ require 'messagebird/base'
2
+ require 'messagebird/custom_details'
3
+ require 'messagebird/group_reference'
4
+ require 'messagebird/message_reference'
5
+
6
+ module MessageBird
7
+ class Contact < MessageBird::Base
8
+ attr_accessor :id, :href, :msisdn, :firstName, :lastName, :customDetails,
9
+ :groups, :messages, :createdDatetime, :updatedDatetime
10
+
11
+ def customDetails=(value)
12
+ @customDetails = MessageBird::CustomDetails.new(value)
13
+ end
14
+
15
+ def groups=(value)
16
+ @groups = MessageBird::GroupReference.new(value)
17
+ end
18
+
19
+ def messages=(value)
20
+ @messages = MessageBird::MessageReference.new(value)
21
+ end
22
+
23
+ def createdDatetime=(value)
24
+ @createdDatetime = value_to_time(value)
25
+ end
26
+
27
+ def updatedDatetime=(value)
28
+ @updatedDatetime = value_to_time(value)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ require 'messagebird/base'
2
+
3
+ module MessageBird
4
+ class ContactReference < MessageBird::Base
5
+ attr_accessor :href, :totalCount
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'messagebird/base'
2
+
3
+ module MessageBird
4
+ class CustomDetails < MessageBird::Base
5
+ # CustomDetails holds free-input fields for the Contact object.
6
+ attr_accessor :custom1, :custom2, :custom3, :custom4
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ require 'messagebird/base'
2
+ require 'messagebird/contact_reference'
3
+
4
+ module MessageBird
5
+ class Group < MessageBird::Base
6
+ attr_accessor :id, :href, :name, :contacts, :createdDatetime,
7
+ :updatedDatetime
8
+
9
+ def contacts=(value)
10
+ @contacts = MessageBird::ContactReference.new(value)
11
+ end
12
+
13
+ def createdDatetime=(value)
14
+ @createdDatetime = value_to_time(value)
15
+ end
16
+
17
+ def updatedDatetime=(value)
18
+ @updatedDatetime = value_to_time(value)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ require 'messagebird/base'
2
+
3
+ module MessageBird
4
+ class GroupReference < MessageBird::Base
5
+ attr_accessor :href, :totalCount
6
+ end
7
+ end
@@ -0,0 +1,78 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+
4
+ module MessageBird
5
+
6
+ class InvalidPhoneNumberException < TypeError; end
7
+ class InvalidResponseException < StandardError; end
8
+ class MethodNotAllowedException < ArgumentError; end
9
+
10
+ class HttpClient
11
+
12
+ attr_reader :access_key
13
+
14
+ def initialize(access_key)
15
+ @access_key = access_key
16
+ end
17
+
18
+ def request(method, path, params={}, check_json=true)
19
+ uri = URI.join(ENDPOINT, '/', path)
20
+
21
+ # Set up the HTTP object.
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ http.use_ssl = true
24
+
25
+ request = prepare_request(method, uri, params)
26
+
27
+ # Execute the request and fetch the response.
28
+ response = http.request(request)
29
+
30
+ assert_valid_response_code(response.code.to_i)
31
+ assert_json_response_type(response['Content-Type']) unless check_json
32
+
33
+ response.body
34
+ end
35
+
36
+ def prepare_request(method, uri, params={})
37
+ # Construct the HTTP request.
38
+ case method
39
+ when :delete
40
+ request = Net::HTTP::Delete.new(uri.request_uri)
41
+ when :get
42
+ request = Net::HTTP::Get.new(uri.request_uri)
43
+ when :post
44
+ request = Net::HTTP::Post.new(uri.request_uri)
45
+ else
46
+ raise MethodNotAllowedException
47
+ end
48
+
49
+ request['Accept'] = 'application/json'
50
+ request['Authorization'] = "AccessKey #{@access_key}"
51
+ request['User-Agent'] = "MessageBird/ApiClient/#{CLIENT_VERSION} Ruby/#{RUBY_VERSION}"
52
+
53
+ request.set_form_data(params) if method == :post && !params.empty?
54
+
55
+ request
56
+ end
57
+
58
+ # Throw an exception if the response code is not one we expect from the
59
+ # MessageBird API.
60
+ def assert_valid_response_code(code)
61
+ # InvalidPhoneNumberException does not make a lot of sense here, but it's
62
+ # needed to maintain backwards compatibility. See issue:
63
+ # https://github.com/messagebird/ruby-rest-api/issues/17
64
+ expected_codes = [200, 201, 204, 401, 404, 405, 422]
65
+ raise InvalidPhoneNumberException, 'Unknown response from server' unless expected_codes.include? code
66
+ end
67
+
68
+ # Throw an exception if the response's content type is not JSON. This only
69
+ # checks the header: it doesn't inspect the actual body.
70
+ def assert_json_response_type(content_type)
71
+ # Check whether the header starts with application/json and don't check
72
+ # for equality: some API's may append the charset to this header.
73
+ raise InvalidResponseException, 'Response is not JSON' unless content_type.start_with? 'application/json'
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,23 @@
1
+ require 'messagebird/base'
2
+
3
+ class List < MessageBird::Base
4
+
5
+ attr_accessor :offset, :limit, :count, :totalCount, :links, :items
6
+
7
+ # type will be used to create objects for the items, e.g.
8
+ # List.new(Contact, {}).
9
+ def initialize(type, json)
10
+ @type = type
11
+
12
+ super(json)
13
+ end
14
+
15
+ def items=(value)
16
+ @items = value.map { |i| @type.new i }
17
+ end
18
+
19
+ def [](index)
20
+ @items[index]
21
+ end
22
+
23
+ end
@@ -0,0 +1,7 @@
1
+ require 'messagebird/base'
2
+
3
+ module MessageBird
4
+ class MessageReference < MessageBird::Base
5
+ attr_accessor :href, :totalCount
6
+ end
7
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: messagebird-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maurice Nonnekes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-30 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2018-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
13
27
  description: A simple REST API for MessageBird in Ruby
14
28
  email: maurice@messagebird.com
15
29
  executables: []
@@ -22,10 +36,18 @@ files:
22
36
  - lib/messagebird/balance.rb
23
37
  - lib/messagebird/base.rb
24
38
  - lib/messagebird/client.rb
39
+ - lib/messagebird/contact.rb
40
+ - lib/messagebird/contact_reference.rb
41
+ - lib/messagebird/custom_details.rb
25
42
  - lib/messagebird/error.rb
43
+ - lib/messagebird/group.rb
44
+ - lib/messagebird/group_reference.rb
26
45
  - lib/messagebird/hlr.rb
46
+ - lib/messagebird/http_client.rb
47
+ - lib/messagebird/list.rb
27
48
  - lib/messagebird/lookup.rb
28
49
  - lib/messagebird/message.rb
50
+ - lib/messagebird/message_reference.rb
29
51
  - lib/messagebird/recipient.rb
30
52
  - lib/messagebird/verify.rb
31
53
  - lib/messagebird/voicemessage.rb
@@ -49,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
71
  version: '0'
50
72
  requirements: []
51
73
  rubyforge_project:
52
- rubygems_version: 2.5.2
74
+ rubygems_version: 2.5.2.3
53
75
  signing_key:
54
76
  specification_version: 4
55
77
  summary: MessageBird's REST API