messagebird-rest 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 65e692932b82b38e07641c5787691f0dddc4330e
4
+ data.tar.gz: ae82199dc7751a50e772ba289fcdc712721fec88
5
+ SHA512:
6
+ metadata.gz: 0ff89ec13b1720dfef137c1e26fca6d105bd6e2538c2e0cbf647b2924791aa81716f62a377100cebc0f4385cfaf953ef224044a853eef0aaa6059ca0d72d145d
7
+ data.tar.gz: 62078521b02b35ffd7a30dfd5c1f4226cc20b920d2cd006b118b1f8188ed868b756ba6ed2c78e0e084a28eb748316bffb80d7f49f26f6e98fe0ae9ba16c0d5d7
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014, MessageBird
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,159 @@
1
+ MessageBird's REST API for Ruby
2
+ ===============================
3
+ This repository contains the open source Ruby client for MessageBird's REST API. Documentation can be found at: https://www.messagebird.com/developers/ruby.
4
+
5
+ Requirements
6
+ ------------
7
+ - [Sign up](https://www.messagebird.com/en/signup) for a free MessageBird account
8
+ - Create a new access_key in the developers sections
9
+ - MessageBird's API client for Ruby requires Ruby >= 1.9
10
+
11
+ Installation
12
+ ------------
13
+ You can either include the following line in your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'messagebird-rest', :require => 'messagebird'
17
+ ```
18
+
19
+ Or you can just manually install it from the command line:
20
+ ```sh
21
+ $ gem install messagebird-rest
22
+ ```
23
+
24
+ Examples
25
+ --------
26
+ We have put some self-explanatory examples in the *examples* directory, but here is a quick breakdown on how it works. First, you need to create an instance of **MessageBird::Client**. Be sure to replace **YOUR_ACCESS_KEY** with something real in the bottom example.
27
+
28
+ ```ruby
29
+ require 'pp' # Only needed for this example
30
+ require 'messagebird'
31
+
32
+ client = MessageBird::Client.new(YOUR_ACCESS_KEY)
33
+ ```
34
+
35
+ That's easy enough. Now we can query the server for information.
36
+
37
+ ##### Balance
38
+ Lets start with out with an overview of our balance.
39
+
40
+ ```ruby
41
+ pp client.balance
42
+
43
+ #<MessageBird::Balance:0x007f8d5c83f478
44
+ @amount=9,
45
+ @payment="prepaid",
46
+ @type="credits">
47
+ ```
48
+
49
+ ##### Messages
50
+ Chances are that the most common use you'll have for this API client is the ability to send out text messages. For that purpose we have created the **message_create** method, which takes the required *originator*, one or more *recipients* and a *body* text for parameters.
51
+
52
+ Optional parameters can be specified as a hash.
53
+
54
+ ```ruby
55
+ pp client.message_create('FromMe', '31612345678', 'Hello World', :reference => 'MyReference')
56
+
57
+ #<MessageBird::Message:0x007f8d5b883520
58
+ @body="Hello World",
59
+ @createdDatetime=2014-07-07 12:20:30 +0200,
60
+ @datacoding="plain",
61
+ @direction="mt",
62
+ @gateway=239,
63
+ @href=
64
+ "https://rest.messagebird.com/messages/211e6280453ba746e8eeff7b12582146",
65
+ @id="211e6280453ba746e8eeff7b12582146",
66
+ @mclass=1,
67
+ @originator="FromMe",
68
+ @recipient=
69
+ {"totalCount"=>1,
70
+ "totalSentCount"=>1,
71
+ "totalDeliveredCount"=>0,
72
+ "totalDeliveryFailedCount"=>0,
73
+ "items"=>
74
+ [#<MessageBird::Recipient:0x007f8d5c058c00
75
+ @recipient=31612345678,
76
+ @status="sent",
77
+ @statusDatetime=2014-07-07 12:20:30 +0200>]},
78
+ @reference="MyReference",
79
+ @scheduledDatetime=nil,
80
+ @type="sms",
81
+ @typeDetails={},
82
+ @validity=nil>
83
+ ```
84
+
85
+ As a possible follow-up, you can use the **message** method with the above mentioned batch-id to query the status of the message that you just created. It will return a similar Message object.
86
+
87
+ ```ruby
88
+ client.message('211e6280453ba746e8eeff7b12582146')
89
+ ```
90
+
91
+ ##### HLR
92
+ To perform HLR lookups we have created the **hlr_create** method, which takes a number and a reference for parameters.
93
+
94
+ ```ruby
95
+ pp client.hlr_create('31612345678', 'MyReference')
96
+
97
+ #<MessageBird::HLR:0x007f8d5b8dafc8
98
+ @createdDatetime=2014-07-07 12:20:05 +0200,
99
+ @href="https://rest.messagebird.com/hlr/4933bed0453ba7455031712h16830892",
100
+ @id="4933bed0453ba7455031712h16830892",
101
+ @msisdn=31612345678,
102
+ @network=nil,
103
+ @reference="MyReference",
104
+ @status="sent",
105
+ @statusDatetime=2014-07-07 12:20:05 +0200>
106
+ ```
107
+
108
+ Similar to the **message_create** and **message** methods, the **hlr_create** method has an accompanying **hlr** method to poll the HLR object.
109
+
110
+ ```ruby
111
+ client.hlr('4933bed0453ba7455031712h16830892')
112
+ ```
113
+
114
+ ##### Voice Message
115
+ MessageBird also offers the ability to send out a text message as a voice message, or text-to-speech. For that purpose we have created the **voice_message_create** method, which takes one or more required *recipients* and a *body* text for parameters.
116
+
117
+ Optional parameters can be specified as a hash.
118
+
119
+ ```ruby
120
+ pp client.voice_message_create('31612345678', 'Hello World', :reference => 'MyReference')
121
+
122
+ #<MessageBird::VoiceMessage:0x000001030101b8
123
+ @body="Hello World",
124
+ @createdDatetime=2014-07-09 12:17:50 +0200,
125
+ @href=
126
+ "https://rest.messagebird.com/voicemessages/a08e51a0353bd16cea7f298a37405850",
127
+ @id="a08e51a0353bd16cea7f298a37405850",
128
+ @ifMachine="continue",
129
+ @language="en-gb",
130
+ @recipients=
131
+ {"totalCount"=>1,
132
+ "totalSentCount"=>1,
133
+ "totalDeliveredCount"=>0,
134
+ "totalDeliveryFailedCount"=>0,
135
+ "items"=>
136
+ [#<MessageBird::Recipient:0x000001011d3178
137
+ @recipient=31612345678,
138
+ @status="calling",
139
+ @statusDatetime=2014-07-09 12:17:50 +0200>]},
140
+ @reference="MyReference",
141
+ @repeat=1,
142
+ @scheduledDatetime=nil,
143
+ @voice="female">
144
+ ```
145
+
146
+ Similar to regular messaging and HLR lookups, there is a method available to fetch the VoiceMessage object by using an *id*.
147
+
148
+ ```ruby
149
+ client.voice_message('a08e51a0353bd16cea7f298a37405850')
150
+ ```
151
+
152
+ Documentation
153
+ -------------
154
+ Complete documentation, instructions, and examples are available at:
155
+ [https://www.messagebird.com/developers/ruby](https://www.messagebird.com/developers/ruby).
156
+
157
+ License
158
+ -------
159
+ The MessageBird REST Client for Ruby is licensed under [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause). Copyright (c) 2014, MessageBird
@@ -0,0 +1,14 @@
1
+ libdir = File.dirname(__FILE__)
2
+ $:.unshift(libdir) unless $:.include?(libdir)
3
+
4
+ module MessageBird
5
+ CLIENT_VERSION = '1.1.0'
6
+ ENDPOINT = 'https://rest.messagebird.com'
7
+ end
8
+
9
+ require 'messagebird/balance'
10
+ require 'messagebird/client'
11
+ require 'messagebird/error'
12
+ require 'messagebird/hlr'
13
+ require 'messagebird/message'
14
+ require 'messagebird/voicemessage'
@@ -0,0 +1,7 @@
1
+ require 'messagebird/base'
2
+
3
+ module MessageBird
4
+ class Balance < MessageBird::Base
5
+ attr_accessor :payment, :type, :amount
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ require 'json'
2
+ require 'time'
3
+
4
+ module MessageBird
5
+ 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
13
+ end
14
+ end
15
+
16
+ def value_to_time(value)
17
+ value ? Time.parse(value) : nil
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,117 @@
1
+ require 'json'
2
+ require 'net/https'
3
+ require 'uri'
4
+
5
+ require 'messagebird/balance'
6
+ require 'messagebird/error'
7
+ require 'messagebird/hlr'
8
+ require 'messagebird/message'
9
+ require 'messagebird/voicemessage'
10
+
11
+ module MessageBird
12
+ class ErrorException < Exception
13
+ attr_reader :errors
14
+
15
+ def initialize(errors)
16
+ @errors = errors
17
+ end
18
+ end
19
+
20
+ class Client
21
+ attr_reader :access_key
22
+
23
+ def initialize(access_key)
24
+ @access_key = access_key
25
+ end
26
+
27
+ def request(method, path, params={})
28
+ uri = URI.join(ENDPOINT, '/', path)
29
+
30
+ # Set up the HTTP object.
31
+ http = Net::HTTP.new(uri.host, uri.port)
32
+ http.use_ssl = true
33
+
34
+ # Construct the HTTP GET or POST request.
35
+ request = Net::HTTP::Get.new(uri.request_uri) if method == :get
36
+ request = Net::HTTP::Post.new(uri.request_uri) if method == :post
37
+ request['Accept'] = 'application/json'
38
+ request['Authorization'] = "AccessKey #{@access_key}"
39
+ request['User-Agent'] = "MessageBird/ApiClient/#{CLIENT_VERSION} Ruby/#{RUBY_VERSION}"
40
+
41
+ # If present, add the HTTP POST parameters.
42
+ request.set_form_data(params) if method == :post && !params.empty?
43
+
44
+ # Execute the request and fetch the response.
45
+ response = http.request(request)
46
+
47
+ # Parse the HTTP response.
48
+ case response.code.to_i
49
+ when 200, 201, 204, 401, 404, 405, 422
50
+ json = JSON.parse(response.body)
51
+ else
52
+ raise Net::HTTPServerError.new 'Unknown response from server', response
53
+ end
54
+
55
+ # If the request returned errors, create Error objects and raise.
56
+ if json.has_key?('errors')
57
+ raise ErrorException, json['errors'].map { |e| Error.new(e) }
58
+ end
59
+
60
+ json
61
+ end
62
+
63
+ # Retrieve your balance.
64
+ def balance
65
+ Balance.new(request(:get, 'balance'))
66
+ end
67
+
68
+ # Retrieve the information of specific HLR.
69
+ def hlr(id)
70
+ HLR.new(request(:get, "hlr/#{id.to_s}"))
71
+ end
72
+
73
+ # Create a new HLR.
74
+ def hlr_create(msisdn, reference)
75
+ HLR.new(request(
76
+ :post,
77
+ 'hlr',
78
+ :msisdn => msisdn,
79
+ :reference => reference))
80
+ end
81
+
82
+ # Retrieve the information of specific message.
83
+ def message(id)
84
+ Message.new(request(:get, "messages/#{id.to_s}"))
85
+ end
86
+
87
+ # Create a new message.
88
+ def message_create(originator, recipients, body, params={})
89
+ # Convert an array of recipients to a comma-separated string.
90
+ recipients = recipients.join(',') if recipients.kind_of?(Array)
91
+
92
+ Message.new(request(
93
+ :post,
94
+ 'messages',
95
+ params.merge({
96
+ :originator => originator.to_s,
97
+ :body => body.to_s,
98
+ :recipients => recipients })))
99
+ end
100
+
101
+ # Retrieve the information of a specific voice message.
102
+ def voice_message(id)
103
+ VoiceMessage.new(request(:get, "voicemessages/#{id.to_s}"))
104
+ end
105
+
106
+ # Create a new voice message.
107
+ def voice_message_create(recipients, body, params={})
108
+ # Convert an array of recipients to a comma-separated string.
109
+ recipients = recipients.join(',') if recipients.kind_of?(Array)
110
+
111
+ VoiceMessage.new(request(
112
+ :post,
113
+ 'voicemessages',
114
+ params.merge({ :recipients => recipients, :body => body.to_s })))
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,7 @@
1
+ require 'messagebird/base'
2
+
3
+ module MessageBird
4
+ class Error < MessageBird::Base
5
+ attr_accessor :code, :description, :parameter
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ require 'time'
2
+
3
+ require 'messagebird/base'
4
+
5
+ module MessageBird
6
+ class HLR < MessageBird::Base
7
+ attr_accessor :id, :href, :msisdn, :network, :reference, :status,
8
+ :createdDatetime, :statusDatetime
9
+
10
+ def createdDatetime=(value)
11
+ @createdDatetime = value_to_time(value)
12
+ end
13
+
14
+ def statusDatetime=(value)
15
+ @statusDatetime = value_to_time(value)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require 'messagebird/base'
2
+ require 'messagebird/recipient'
3
+
4
+ module MessageBird
5
+ class Message < MessageBird::Base
6
+ attr_accessor :id, :href, :direction, :type, :originator, :body,
7
+ :reference, :validity, :gateway, :typeDetails, :datacoding,
8
+ :mclass, :scheduledDatetime, :createdDatetime, :recipients
9
+
10
+ def scheduledDatetime=(value)
11
+ @scheduledDatetime = value_to_time(value)
12
+ end
13
+
14
+ def createdDatetime=(value)
15
+ @createdDatetime = value_to_time(value)
16
+ end
17
+
18
+ def recipients=(json)
19
+ json['items'] = json['items'].map { |r| Recipient.new(r) }
20
+ @recipients = json
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ require 'messagebird/base'
2
+
3
+ module MessageBird
4
+ class Recipient < MessageBird::Base
5
+ attr_accessor :recipient, :status, :statusDatetime
6
+
7
+ def statusDatetime=(value)
8
+ @statusDatetime = value_to_time(value)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ require 'messagebird/base'
2
+ require 'messagebird/recipient'
3
+
4
+ module MessageBird
5
+ class VoiceMessage < MessageBird::Base
6
+ attr_accessor :id, :href, :body, :reference, :language, :voice, :repeat,
7
+ :ifMachine, :scheduledDatetime, :createdDatetime, :recipients
8
+
9
+
10
+ def scheduledDatetime=(value)
11
+ @scheduledDatetime = value_to_time(value)
12
+ end
13
+
14
+ def createdDatetime=(value)
15
+ @createdDatetime = value_to_time(value)
16
+ end
17
+
18
+ def recipients=(json)
19
+ json['items'] = json['items'].map { |r| Recipient.new(r) }
20
+ @recipients = json
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: messagebird-rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maurice Nonnekes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple REST API for MessageBird in Ruby
14
+ email: maurice@messagebird.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/messagebird.rb
22
+ - lib/messagebird/balance.rb
23
+ - lib/messagebird/base.rb
24
+ - lib/messagebird/client.rb
25
+ - lib/messagebird/error.rb
26
+ - lib/messagebird/hlr.rb
27
+ - lib/messagebird/message.rb
28
+ - lib/messagebird/recipient.rb
29
+ - lib/messagebird/voicemessage.rb
30
+ homepage: https://github.com/messagebird/ruby-rest-api
31
+ licenses:
32
+ - BSD-2-Clause
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.2.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: MessageBird's REST API
54
+ test_files: []