textbelt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/README.md +49 -0
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/textbelt.rb +61 -0
- data/lib/textbelt/errors.rb +48 -0
- data/lib/textbelt/validators/phone_validator.rb +37 -0
- data/lib/textbelt/validators/response_validator.rb +34 -0
- data/lib/textbelt/version.rb +4 -0
- data/textbelt.gemspec +37 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7de6ab396ff78777875207b7315451beaf23f774
|
4
|
+
data.tar.gz: cfc7e29b6a77b7c9034625daf3e7743ddf21f0bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5b2750c761f31eab2b0a73b98f54aa474d9a44a83208ba8d3835aca44869190130808f20c07db8704b9b9de196095d887e510c5d72f2ab55a6e25ad3be6647b
|
7
|
+
data.tar.gz: b14840ec6d2a39932c7b10a1403148c27c30f6bc3ae00e3a7380c68ef821206daccf957f782c2414d2ab729c3cc69964b923142bd786ed057f1fe29d38f38030
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Textbelt
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/djds23/textbelt-gem.svg?branch=master)](https://travis-ci.org/djds23/textbelt-gem)
|
4
|
+
|
5
|
+
A ruby interface for sending texts free of charge through [TextBelt](http://textbelt.com/).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'textbelt'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install textbelt
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
TextBelt.text('01234567', 'Hello, World!') #=> Your favorite cell phone number gets a text message
|
27
|
+
```
|
28
|
+
|
29
|
+
## Development
|
30
|
+
|
31
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/djds23/textbelt-gem.
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The MIT License (MIT)
|
42
|
+
|
43
|
+
Copyright (c) 2015 Dean Silfen
|
44
|
+
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
46
|
+
|
47
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
48
|
+
|
49
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "textbelt"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/textbelt.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# @author Dean Silfen
|
2
|
+
module TextBelt
|
3
|
+
require 'textbelt/validators/response_validator'
|
4
|
+
require 'textbelt/validators/phone_validator'
|
5
|
+
require 'textbelt/version'
|
6
|
+
require 'textbelt/errors'
|
7
|
+
require 'net/http'
|
8
|
+
require 'json'
|
9
|
+
require 'uri'
|
10
|
+
|
11
|
+
|
12
|
+
# Get a list of categories from the service
|
13
|
+
#
|
14
|
+
# @param phone_number [String] number to send the text to
|
15
|
+
# @param message [String] the body of the text message
|
16
|
+
# @param country [String] ISO 3166 Country code for destination country
|
17
|
+
#
|
18
|
+
# @raise [Errors::IntegerPhoneError] phone number passed as an integer
|
19
|
+
# @raise [Errors::InvalidPhoneNumberError] phone number is not valid
|
20
|
+
# @raise [Errors::BlackListedNumberError] http server says number is blacklisted
|
21
|
+
# @raise [Errors::GatewayFailureError] http server had a gateway error communicating with carrier
|
22
|
+
# @raise [Errors::PhoneCouldNotValidateError] http server cannot validate the phone\'s quota
|
23
|
+
# @raise [Errors::IPCouldNotValidateError] http server cannot validate the ip's quota
|
24
|
+
# @raise [Errors::PhoneQuotaExceededError] the phone's quota has been exceeded
|
25
|
+
# @raise [Errors::IPQuotaExceededError] IP's quota has been exceeded
|
26
|
+
#
|
27
|
+
# @return [Boolean] true if TextBelt successfully passed on the message
|
28
|
+
#
|
29
|
+
def text(phone_number, message, country = 'US')
|
30
|
+
PhoneValidator.validate(phone_number, country)
|
31
|
+
url = url_for(country)
|
32
|
+
res = Net::HTTP.post_form(url, number: phone_number, message: message)
|
33
|
+
body = JSON.parse(res.body)
|
34
|
+
ResponseValidator.validate(phone_number, body)
|
35
|
+
body['success'.freeze]
|
36
|
+
end
|
37
|
+
|
38
|
+
# @private
|
39
|
+
def url_for(country)
|
40
|
+
url_string =
|
41
|
+
case country
|
42
|
+
when 'US'
|
43
|
+
base_url + 'text'
|
44
|
+
when 'CA'
|
45
|
+
base_url + 'canada'
|
46
|
+
else
|
47
|
+
base_url + 'intl'
|
48
|
+
end
|
49
|
+
|
50
|
+
URI(url_string)
|
51
|
+
end
|
52
|
+
|
53
|
+
# @private
|
54
|
+
def base_url
|
55
|
+
'http://textbelt.com/'
|
56
|
+
end
|
57
|
+
|
58
|
+
module_function :text
|
59
|
+
module_function :base_url
|
60
|
+
module_function :url_for
|
61
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# File to hold all the errors for TextBelt
|
2
|
+
#
|
3
|
+
# @author Dean Silfen
|
4
|
+
module TextBelt
|
5
|
+
module Errors
|
6
|
+
|
7
|
+
# Error to be raised if phone number is not passed as a string
|
8
|
+
#
|
9
|
+
# @author Dean Silfen
|
10
|
+
class IntegerPhoneError < StandardError; end
|
11
|
+
|
12
|
+
# Error to be raised if phone number is not valid in destination country
|
13
|
+
#
|
14
|
+
# @author Dean Silfen
|
15
|
+
class InvalidPhoneNumberError < StandardError; end
|
16
|
+
|
17
|
+
# Error raised if failure happens at the time of send from http service to carrier
|
18
|
+
#
|
19
|
+
# @author Dean Silfen
|
20
|
+
class GatewayFailureError < StandardError; end
|
21
|
+
|
22
|
+
# Error raised if number provided has been blacklisted
|
23
|
+
#
|
24
|
+
# @author Dean Silfen
|
25
|
+
class BlackListedNumberError < StandardError; end
|
26
|
+
|
27
|
+
# Error raised if phone number could not be validated in the http service
|
28
|
+
#
|
29
|
+
# @author Dean Silfen
|
30
|
+
class PhoneCouldNotValidateError < StandardError; end
|
31
|
+
|
32
|
+
# Error raised if phone number could not be validated in the http service
|
33
|
+
#
|
34
|
+
# @author Dean Silfen
|
35
|
+
class IPCouldNotValidateError < StandardError; end
|
36
|
+
|
37
|
+
# Error raised if service exceeded rate limit for phone number
|
38
|
+
#
|
39
|
+
# @author Dean Silfen
|
40
|
+
class PhoneQuotaExceededError < StandardError; end
|
41
|
+
|
42
|
+
# Error raised if service exceeded rate limit for IP
|
43
|
+
#
|
44
|
+
# @author Dean Silfen
|
45
|
+
class IPQuotaExceededError < StandardError; end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# File to hold all the errors for TextBelt
|
2
|
+
#
|
3
|
+
# @author Dean Silfen
|
4
|
+
module TextBelt
|
5
|
+
require 'phonelib'
|
6
|
+
|
7
|
+
class PhoneValidator
|
8
|
+
class << self
|
9
|
+
|
10
|
+
# Get a list of categories from the service
|
11
|
+
#
|
12
|
+
# @param phone_number [String] number to send the text to
|
13
|
+
# @param country [String] ISO 3166 Country code for destination country
|
14
|
+
#
|
15
|
+
# @return [Boolean] true if TextBelt successfully passed on the message,
|
16
|
+
# false if not
|
17
|
+
def validate(phone_number, country)
|
18
|
+
unless is_a_string?(phone_number)
|
19
|
+
raise TextBelt::Errors::IntegerPhoneError, "Please ensure to pass phone numbers as strings."
|
20
|
+
end
|
21
|
+
|
22
|
+
phone = Phonelib.parse phone_number
|
23
|
+
|
24
|
+
if phone.impossible?
|
25
|
+
raise TextBelt::Errors::InvalidPhoneNumberError, "#{phone_number} is not valid in destination country #{country}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# @private
|
32
|
+
def is_a_string?(phone_number)
|
33
|
+
phone_number.is_a? String
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module TextBelt
|
2
|
+
class ResponseValidator
|
3
|
+
class << self
|
4
|
+
def validate(phone_number, body)
|
5
|
+
return true if body['message'].nil? && body['success']
|
6
|
+
message = body['message']
|
7
|
+
if message == 'Sorry, texts to this number are disabled.'
|
8
|
+
raise TextBelt::Errors::BlackListedNumberError, "#{phone_number} has been blacklisted"
|
9
|
+
end
|
10
|
+
|
11
|
+
if message == 'Communication with SMS gateway failed.'
|
12
|
+
raise TextBelt::Errors::GatewayFailureError, 'A gateway failure occured between the http service and the carrier'
|
13
|
+
end
|
14
|
+
|
15
|
+
if message == 'Could not validate phone# quota.'
|
16
|
+
raise TextBelt::Errors::PhoneCouldNotValidateError, "#{phone_number} could not be validated in http service"
|
17
|
+
end
|
18
|
+
|
19
|
+
if message == 'Could not validate IP quota.'
|
20
|
+
raise TextBelt::Errors::IPCouldNotValidateError, message
|
21
|
+
end
|
22
|
+
|
23
|
+
if message.include? 'Exceeded quota for this phone number.'
|
24
|
+
raise TextBelt::Errors::PhoneQuotaExceededError, message
|
25
|
+
end
|
26
|
+
|
27
|
+
if message.include? 'Exceeded quota for this IP address.'
|
28
|
+
raise TextBelt::Errors::IPQuotaExceededError, message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
data/textbelt.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'textbelt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "textbelt"
|
8
|
+
spec.version = TextBelt::VERSION
|
9
|
+
spec.authors = ["Dean Silfen"]
|
10
|
+
spec.email = ["dean.silfen@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A ruby interface for sending texts with http://textbelt.com/}
|
13
|
+
spec.description = %q{A ruby interface for sending texts with http://textbelt.com/. Complete with configurable textbelt endpoint, in case you are hosting your own instance.}
|
14
|
+
spec.homepage = "http://www.rubydoc.info/github/djds23/textbelt"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
# end
|
23
|
+
# TODO: Add a server to push from, defend against this scenario
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'phonelib', '~> 0.5.4'
|
31
|
+
|
32
|
+
spec.add_development_dependency "webmock", ["~> 1.22.3"]
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec"
|
36
|
+
spec.add_development_dependency "yard"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: textbelt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dean Silfen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: phonelib
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.5.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.5.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: webmock
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.22.3
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.22.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: yard
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: A ruby interface for sending texts with http://textbelt.com/. Complete
|
98
|
+
with configurable textbelt endpoint, in case you are hosting your own instance.
|
99
|
+
email:
|
100
|
+
- dean.silfen@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/textbelt.rb
|
114
|
+
- lib/textbelt/errors.rb
|
115
|
+
- lib/textbelt/validators/phone_validator.rb
|
116
|
+
- lib/textbelt/validators/response_validator.rb
|
117
|
+
- lib/textbelt/version.rb
|
118
|
+
- textbelt.gemspec
|
119
|
+
homepage: http://www.rubydoc.info/github/djds23/textbelt
|
120
|
+
licenses: []
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.4.8
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: A ruby interface for sending texts with http://textbelt.com/
|
142
|
+
test_files: []
|
143
|
+
has_rdoc:
|