messagebird 0.1.1

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: 60591298cdf44f6e11fcdf5028a36d63cc933e80
4
+ data.tar.gz: 3341e9e7f6b5187b2d427795b64b21cefb57d196
5
+ SHA512:
6
+ metadata.gz: d79edff49e03728c97aa256fa623452d9ebeaed368a74cc11c0e28dc7de3fef9eceacd6e05cd9ca01b2d1cde34f9ac70d07c3ecd669c482d4b4643eda9ed6088
7
+ data.tar.gz: 8e9c3b9ca20e78a155480c756ce872ffdf8cc0c8071486c821ed892d6a25d35e6ae0d8df009aa0f9d7c81d80648b9904aacfa43cb982e08b3bb9cb0f20ec3b33
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ cache: bundler
3
+ bundler_args: --without development
4
+
5
+ rvm:
6
+ - 1.8.7
7
+ - 1.9.3
8
+ - 2.0.0
9
+ - 2.1.0
10
+
11
+ matrix:
12
+ fast_finish: true
13
+ allow_failures:
14
+ - rvm: 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'bundler', '~> 1.0'
5
+ gem 'rake'
6
+ gem 'minitest', :require => false
7
+ gem 'rr', :require => false
8
+ end
9
+
10
+ group :development do
11
+ gem 'jeweler', '~> 2.0.0', :require => false
12
+ gem 'simplecov', :require => false
13
+ end
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Nedap N.V., Bram de Vries
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ [![Code Climate](https://codeclimate.com/github/nedap/messagebird.png)](https://codeclimate.com/github/nedap/messagebird)
2
+ [![Build Status](https://travis-ci.org/nedap/messagebird.png?branch=master)](https://travis-ci.org/nedap/messagebird)
3
+
4
+ # MessageBird API gem
5
+ Implementation of the MessageBird text (sms) service API
6
+
7
+ ## Compatibility
8
+ Development is focussed on MRI 1.9.3 and 2.0.0.
9
+
10
+ Legacy support for MRI 1.8.7, and experimental support for MRI 2.1.0 and JRuby 1.7.x.
11
+
12
+ ## Contributing
13
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
14
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
15
+ * Fork the project.
16
+ * Start a feature/bugfix branch.
17
+ * Commit and push until you are happy with your contribution.
18
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
19
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
20
+
21
+ ## Credits
22
+ Based on the API example on https://www.messagebird.com/nl/sms-api#ruby
23
+
24
+ ## Licence
25
+ See the LICENCE file for licensing information.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ # Bundler setup
7
+ begin
8
+ Bundler.setup(:default, :test, :development)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+ require 'rake'
15
+
16
+ # Optional Jeweler setup
17
+ begin
18
+ require 'jeweler'
19
+ Jeweler::Tasks.new do |gem|
20
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
21
+ gem.name = "messagebird"
22
+ gem.homepage = "http://github.com/nedap/messagebird-sms-api-ruby"
23
+ gem.license = "MIT"
24
+ gem.summary = %Q{MessageBird API for Ruby}
25
+ gem.description = %Q{Implementation of the MessageBird text (sms) service API}
26
+ gem.email = "bram.devries@nedap.com"
27
+ gem.authors = ["Bram de Vries"]
28
+ # dependencies defined in Gemfile
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+ rescue LoadError
32
+ end
33
+
34
+ require 'rake/testtask'
35
+ Rake::TestTask.new(:test) do |test|
36
+ test.libs << 'lib' << 'test'
37
+ test.pattern = 'test/**/*_test.rb'
38
+ test.verbose = true
39
+ end
40
+
41
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+
5
+ require 'pp'
6
+ require 'messagebird'
7
+
8
+ MessageBird.configure do
9
+ username 'nedappep'
10
+ password 'testpass'
11
+ sender_name 'NedapPep'
12
+ end
13
+
14
+ MessageBird.deliver("NedapPep", "31648951497", "test message") do |response|
15
+ pp response
16
+ end
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'uri'
6
+
7
+ require 'messagebird/config'
8
+ require 'messagebird/deliverable'
9
+ require 'messagebird/helpers'
10
+ require 'messagebird/sms'
11
+ require 'messagebird/http/sender'
12
+ require 'messagebird/http/sms'
13
+ require 'messagebird/http/response_code'
14
+ require 'messagebird/http/response'
15
+
16
+ module MessageBird
17
+ class << self
18
+
19
+ def deliver(originator, recipients, message, options={}, &block)
20
+ MessageBird::SMS.deliver(originator, recipients, message, options, &block)
21
+ end
22
+
23
+ def configure(&block)
24
+ MessageBird::Config.configure(&block)
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,211 @@
1
+ # Based on https://github.com/jwkoelewijn/batsir/blob/master/lib/batsir/config.rb
2
+
3
+ module MessageBird
4
+ class Config
5
+ class << self
6
+
7
+ attr_accessor :config
8
+
9
+ def defaults
10
+ @defaults ||= {
11
+ :module => :http,
12
+ :api_url => "https://api.messagebird.com/api/sms",
13
+ :username => 'replace_me',
14
+ :password => 'replace_me',
15
+ :sender_name => 'replace_me',
16
+ :test_mode => true
17
+ }
18
+ end
19
+
20
+ # Returns the current configuration or sets it up
21
+ def configuration
22
+ @config ||= setup
23
+ end
24
+
25
+ # Yields the configuration.
26
+ #
27
+ # ==== Block parameters
28
+ # c<Hash>:: The configuration parameters.
29
+ #
30
+ # ==== Examples
31
+ # MessageBird::Config.use do |config|
32
+ # config[:exception_details] = false
33
+ # config[:log_stream] = STDOUT
34
+ # end
35
+ #
36
+ # ==== Returns
37
+ # nil
38
+ #
39
+ # :api: public
40
+ def use
41
+ yield configuration
42
+ nil
43
+ end
44
+
45
+ # Detects whether the provided key is in the config.
46
+ #
47
+ # ==== Parameters
48
+ # key<Object>:: The key to check.
49
+ #
50
+ # ==== Returns
51
+ # Boolean:: True if the key exists in the config.
52
+ #
53
+ # :api: public
54
+ def key?(key)
55
+ configuration.key?(key)
56
+ end
57
+
58
+ # Retrieve the value of a config entry.
59
+ #
60
+ # ==== Parameters
61
+ # key<Object>:: The key to retrieve the parameter for.
62
+ #
63
+ # ==== Returns
64
+ # Object:: The value of the configuration parameter.
65
+ #
66
+ # :api: public
67
+ def [](key)
68
+ configuration[key]
69
+ end
70
+
71
+ # Set the value of a config entry.
72
+ #
73
+ # ==== Parameters
74
+ # key<Object>:: The key to set the parameter for.
75
+ # val<Object>:: The value of the parameter.
76
+ #
77
+ # :api: public
78
+ def []=(key, val)
79
+ configuration[key] = val
80
+ end
81
+
82
+ # Remove the value of a config entry.
83
+ #
84
+ # ==== Parameters
85
+ # key<Object>:: The key of the parameter to delete.
86
+ #
87
+ # ==== Returns
88
+ # Object:: The value of the removed entry.
89
+ #
90
+ # :api: public
91
+ def delete(key)
92
+ configuration.delete(key)
93
+ end
94
+
95
+ # Resets the configuration to its default state
96
+ #
97
+ def reset
98
+ setup
99
+ end
100
+
101
+ # Retrieve the value of a config entry, returning the provided default if the key is not present
102
+ #
103
+ # ==== Parameters
104
+ # key<Object>:: The key to retrieve the parameter for.
105
+ # default<Object>::
106
+ # The default value to return if the parameter is not set.
107
+ #
108
+ # ==== Returns
109
+ # Object:: The value of the configuration parameter or the default.
110
+ #
111
+ # :api: public
112
+ def fetch(key, default = nil)
113
+ configuration.fetch(key, default)
114
+ end
115
+
116
+ # Returns the configuration as a hash.
117
+ #
118
+ # ==== Returns
119
+ # Hash:: The config as a hash.
120
+ #
121
+ # :api: public
122
+ def to_hash
123
+ configuration
124
+ end
125
+
126
+ # Sets up the configuration by storing the given settings.
127
+ #
128
+ # ==== Parameters
129
+ # settings<Hash>::
130
+ # Configuration settings to use. These are merged with the defaults.
131
+ #
132
+ # ==== Returns
133
+ # The configuration as a hash.
134
+ #
135
+ # :api: private
136
+ def setup(settings = {})
137
+ @config = defaults.merge(settings)
138
+ end
139
+
140
+ # Set configuration parameters from a code block, where each method
141
+ # evaluates to a config parameter.
142
+ #
143
+ # ==== Parameters
144
+ # &block:: Configuration parameter block.
145
+ #
146
+ # ==== Examples
147
+ # # Set environment and log level.
148
+ # MessageBird::Config.configure do
149
+ # environment "development"
150
+ # log_level "debug"
151
+ # end
152
+ #
153
+ # ==== Returns
154
+ # nil
155
+ #
156
+ # :api: public
157
+ def configure(&block)
158
+ ConfigBlock.new(self, &block) if block_given?
159
+ nil
160
+ end
161
+
162
+ # Allows retrieval of single key config values via MessageBird::Config.<key>
163
+ # Allows single key assignment via Merb.config.<key> = ...
164
+ #
165
+ # ==== Parameters
166
+ # method<~to_s>:: Method name as hash key value.
167
+ # *args:: Value to set the configuration parameter to.
168
+ #
169
+ # ==== Returns
170
+ # The value of the entry fetched or assigned to.
171
+ #
172
+ # :api: public
173
+ def method_missing(method, *args)
174
+ if method.to_s[-1,1] == '='
175
+ self[method.to_s.tr('=','').to_sym] = args.first
176
+ else
177
+ self[method]
178
+ end
179
+ end
180
+
181
+ end # class << self
182
+
183
+ class ConfigBlock
184
+
185
+ # Evaluates the provided block, where any call to a method causes
186
+ # #[]= to be called on klass with the method name as the key and the arguments
187
+ # as the value.
188
+ #
189
+ # ==== Parameters
190
+ # klass<Object~[]=>:: The object on which to assign values.
191
+ # &block:: The block which specifies the config values to set.
192
+ #
193
+ # ==== Returns
194
+ # nil
195
+ #
196
+ # :api: private
197
+ def initialize(klass, &block)
198
+ @klass = klass
199
+ instance_eval(&block)
200
+ end
201
+
202
+ # Assign args as the value of the entry keyed by method.
203
+ #
204
+ # :api: private
205
+ def method_missing(method, *args)
206
+ @klass[method] = args.first
207
+ end
208
+
209
+ end # ConfigBlock
210
+ end # Config
211
+ end
@@ -0,0 +1,7 @@
1
+ module MessageBird
2
+ class Deliverable
3
+ def deliver
4
+ raise '#deliver method must be implemented!'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ module MessageBird
2
+ module Helpers
3
+
4
+ def config
5
+ MessageBird::Config
6
+ end
7
+
8
+ def constantize(class_name)
9
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ class_name
10
+ raise NameError, "#{class_name.inspect} is not a valid constant name!"
11
+ end
12
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
13
+ end
14
+
15
+ def escape(str)
16
+ URI.escape(str)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ module MessageBird::HTTP
2
+ class Response
3
+
4
+ attr_reader :response_code
5
+
6
+ def initialize( http_response )
7
+ raise ArgumentError unless http_response.respond_to?(:body)
8
+ @response_code = ResponseCode.decode(http_response.body)
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,47 @@
1
+ module MessageBird::HTTP
2
+ class ResponseCode
3
+
4
+ def initialize(symbol, description)
5
+ @symbol = symbol
6
+ @description = description
7
+ end
8
+
9
+ def to_sym
10
+ @symbol
11
+ end
12
+
13
+ def to_s
14
+ @description
15
+ end
16
+
17
+ def ==(other)
18
+ if other.is_a? Symbol
19
+ self.to_sym == other
20
+ else
21
+ self.to_s == other
22
+ end
23
+ end
24
+
25
+ def self.decode(string)
26
+ RESPONSE_CODES[string] || raise(ResponseCodeNotFound.new(string))
27
+ end
28
+
29
+ RESPONSE_CODES = {
30
+ "01" => ResponseCode.new(:success, "Request processed successfully"),
31
+ "69" => ResponseCode.new(:message_cannot_schedule_on_date, "Message cannot be scheduled on this date"),
32
+ "70" => ResponseCode.new(:timestamp_incorrect, "Timestamp notation invalid"),
33
+ "71" => ResponseCode.new(:timestamp_in_past, "Timestamp is in the past"),
34
+ "72" => ResponseCode.new(:message_too_long, "Message is too long"),
35
+ "89" => ResponseCode.new(:sender_invalid, "Sender invalid"),
36
+ "93" => ResponseCode.new(:receivers_invalid, "One or several receivers invalid"),
37
+ "95" => ResponseCode.new(:message_not_selected, "No message selected"),
38
+ "96" => ResponseCode.new(:credits_insufficient, "Insufficient credits"),
39
+ "97" => ResponseCode.new(:login_invalid, "Invalid username and/or password"),
40
+ "98" => ResponseCode.new(:ip_address_not_authorized, "IP address is not authorized for this account"),
41
+ "99" => ResponseCode.new(:connection_failed, "Cannot connect to server")
42
+ }
43
+ # PREMIUM CODES NOT IMPLEMENTED YET
44
+
45
+ class ResponseCodeNotFound < ArgumentError; end
46
+ end
47
+ end