twilito 0.2.0 → 0.4.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.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +20 -0
- data/.gitignore +3 -2
- data/Gemfile.lock +37 -15
- data/README.md +48 -9
- data/Rakefile +6 -0
- data/lib/twilito.rb +4 -8
- data/lib/twilito/api.rb +13 -8
- data/lib/twilito/result.rb +14 -9
- data/lib/twilito/version.rb +1 -1
- data/twilito.gemspec +5 -3
- metadata +40 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac55859f2c24ea8da46a22d84d2b34473c586280bd4f2541d70fdbe5de044229
|
4
|
+
data.tar.gz: 9301076c59a7372e51870b2b65c33fe793e3163333f6925e67646d8dd1f0c497
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8528c27bcf15dffa8a1e844fd80cd5e648102ca6c7e20afb3af00edbbe7193ba23ff86f367771f901f4e92198ba1304afa730f757cb27964178b68cb3dd524b
|
7
|
+
data.tar.gz: 9edd9bc5e5c52a872db0a4e45e0eba6b04a5fba0de98ef615ca2c1a5c8dbb5abf01f772cb6760a7c3e363cbfa98a6e9129a13f67a366bad746a0f57e7f09135c
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: ["2.4", "2.5", "2.6", "2.7", "3.0"]
|
11
|
+
name: Run Tests (Ruby ${{ matrix.ruby }})
|
12
|
+
steps:
|
13
|
+
- uses: actions/checkout@master
|
14
|
+
- name: Setup ruby
|
15
|
+
uses: ruby/setup-ruby@v1
|
16
|
+
with:
|
17
|
+
ruby-version: ${{ matrix.ruby }}
|
18
|
+
bundler-cache: true
|
19
|
+
- name: Run tests
|
20
|
+
run: bundle exec rake
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,29 +1,49 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
twilito (0.
|
4
|
+
twilito (0.3.3)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
addressable (2.
|
10
|
-
public_suffix (>= 2.0.2, <
|
9
|
+
addressable (2.7.0)
|
10
|
+
public_suffix (>= 2.0.2, < 5.0)
|
11
|
+
ast (2.4.1)
|
11
12
|
coderay (1.1.2)
|
12
|
-
crack (0.4.
|
13
|
-
|
14
|
-
hashdiff (0.
|
13
|
+
crack (0.4.5)
|
14
|
+
rexml
|
15
|
+
hashdiff (1.0.1)
|
15
16
|
method_source (0.9.2)
|
16
17
|
minitest (5.11.3)
|
18
|
+
parallel (1.19.2)
|
19
|
+
parser (2.7.1.3)
|
20
|
+
ast (~> 2.4.0)
|
17
21
|
pry (0.12.2)
|
18
22
|
coderay (~> 1.1.0)
|
19
23
|
method_source (~> 0.9.0)
|
20
|
-
public_suffix (
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
+
public_suffix (4.0.6)
|
25
|
+
rainbow (3.0.0)
|
26
|
+
rake (13.0.1)
|
27
|
+
regexp_parser (1.7.1)
|
28
|
+
rexml (3.2.4)
|
29
|
+
rubocop (0.85.1)
|
30
|
+
parallel (~> 1.10)
|
31
|
+
parser (>= 2.7.0.1)
|
32
|
+
rainbow (>= 2.2.2, < 4.0)
|
33
|
+
regexp_parser (>= 1.7)
|
34
|
+
rexml
|
35
|
+
rubocop-ast (>= 0.0.3)
|
36
|
+
ruby-progressbar (~> 1.7)
|
37
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
38
|
+
rubocop-ast (0.0.3)
|
39
|
+
parser (>= 2.7.0.1)
|
40
|
+
ruby-progressbar (1.10.1)
|
41
|
+
unicode-display_width (1.7.0)
|
42
|
+
webmock (3.12.2)
|
24
43
|
addressable (>= 2.3.6)
|
25
44
|
crack (>= 0.3.2)
|
26
|
-
hashdiff
|
45
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
46
|
+
yard (0.9.25)
|
27
47
|
|
28
48
|
PLATFORMS
|
29
49
|
ruby
|
@@ -32,12 +52,14 @@ DEPENDENCIES
|
|
32
52
|
bundler (~> 2.0)
|
33
53
|
minitest (~> 5.11)
|
34
54
|
pry
|
35
|
-
rake (~>
|
55
|
+
rake (~> 13.0)
|
56
|
+
rubocop
|
36
57
|
twilito!
|
37
|
-
webmock (~> 3)
|
58
|
+
webmock (~> 3.12)
|
59
|
+
yard (~> 0.9)
|
38
60
|
|
39
61
|
RUBY VERSION
|
40
|
-
ruby 2.
|
62
|
+
ruby 2.7.2p137
|
41
63
|
|
42
64
|
BUNDLED WITH
|
43
|
-
2.
|
65
|
+
2.2.15
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Twilito
|
2
2
|
|
3
|
-
|
3
|
+
A tiny, zero dependency helper for sending text messages with Twilio. _Just enough_ of a wrapper to abstract away Twilio's REST API for sending messages, without _anything_ else.
|
4
4
|
|
5
|
-
|
5
|
+
[](https://badge.fury.io/rb/twilito) [](https://github.com/alexford/twilito/actions)
|
6
6
|
|
7
7
|
## Why
|
8
8
|
|
9
|
-
Twilio's [full Ruby library](https://github.com/twilio/twilio-ruby) does a
|
9
|
+
Twilio's [full Ruby library](https://github.com/twilio/twilio-ruby) does a _lot_, and has a large memory footprint to go with it—too large for just sending a message. It's also more difficult to mock and verify in tests than I'd like.
|
10
10
|
|
11
11
|
Using [Twilio's REST API](https://www.twilio.com/docs/usage/api) directly is fine, but can be cumbersome.
|
12
12
|
|
@@ -16,6 +16,8 @@ If you use more of Twilio, consider [twilio-ruby](https://github.com/twilio/twil
|
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
+
Twilito should work on Ruby 2.4 and up. Unit tests [run in CI](https://github.com/alexford/twilito/actions) on 2.4, 2.5, 2.6, 2.7, and 3.0.
|
20
|
+
|
19
21
|
#### Install the gem
|
20
22
|
|
21
23
|
```
|
@@ -25,16 +27,17 @@ gem 'twilito'
|
|
25
27
|
#### Simplest case
|
26
28
|
|
27
29
|
```ruby
|
28
|
-
# All
|
30
|
+
# All of these arguments are required, but can be configured as defaults (see below)
|
29
31
|
result = Twilito.send_sms(
|
30
32
|
to: '+15555555555',
|
31
33
|
from: '+15554444444',
|
32
|
-
|
34
|
+
body: 'This is my content',
|
33
35
|
account_sid: '...', # Twilio Credentials
|
34
36
|
auth_token: '...'
|
35
37
|
)
|
36
38
|
|
37
|
-
|
39
|
+
|
40
|
+
# Returns instance of Twilito::Result
|
38
41
|
|
39
42
|
result.success? # => boolean
|
40
43
|
result.errors # => [] or error messages
|
@@ -60,7 +63,9 @@ rescue Twilito::SendError => e
|
|
60
63
|
end
|
61
64
|
```
|
62
65
|
|
63
|
-
|
66
|
+
### Configuring Defaults For Required Arguments
|
67
|
+
|
68
|
+
The five required arguments (`to`, `from`, `body`, `account_sid`, and `auth_token`) can be configured as defaults with `Twilito.configure`.
|
64
69
|
|
65
70
|
```ruby
|
66
71
|
# In an initializer or something like that:
|
@@ -80,7 +85,22 @@ end
|
|
80
85
|
Twilito.send_sms!(to: '+15555555555', body: 'Foo')
|
81
86
|
```
|
82
87
|
|
83
|
-
|
88
|
+
### Using Other, Optional (Arbitrary) Arguments
|
89
|
+
|
90
|
+
There are a number of optional parameters defined by Twilio for sending a message (see [the API documentation](https://www.twilio.com/docs/sms/api/message-resource#create-a-message-resource)). Any of these can be sent with Twilito using the "Ruby-style" snake cased equivalent of the parameter.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
# Twilito sends arbitrary arguments to Twilio's API after CamelCasing keys to match Twilio's style.
|
94
|
+
# NOTE: This example assumes auth_token, account_sid, and from have already been configured.
|
95
|
+
|
96
|
+
result = Twilito.send_sms(
|
97
|
+
to: '+15555555555',
|
98
|
+
body: 'This is my content',
|
99
|
+
media_url: 'https://example.com/image.png', # MediaUrl
|
100
|
+
status_callback: 'https://your.app.io/sms/callback', # StatusCallback
|
101
|
+
smart_encoded: true # SmartEncoded
|
102
|
+
)
|
103
|
+
```
|
84
104
|
|
85
105
|
## Testing your code
|
86
106
|
|
@@ -88,4 +108,23 @@ _TODO: Add examples of mocking and/or test helpers for asserting your code sends
|
|
88
108
|
|
89
109
|
## Contributing
|
90
110
|
|
91
|
-
|
111
|
+
### Contribute Feedback, Ideas, and Bug Reports
|
112
|
+
|
113
|
+
- Create or comment on an issue
|
114
|
+
|
115
|
+
### Contribute Code
|
116
|
+
|
117
|
+
- Find an open issue or create one
|
118
|
+
- Fork this repo and open a PR
|
119
|
+
- Write unit tests for your change
|
120
|
+
|
121
|
+
### Contribute Docs
|
122
|
+
|
123
|
+
- Open a PR to make the README better, or help with contribution guidelines, etc.
|
124
|
+
- There is currently an open issue for adding RDoc/YARD documentation
|
125
|
+
|
126
|
+
### Contribute Beer
|
127
|
+
|
128
|
+
Did Twilito save you some RAM?
|
129
|
+
|
130
|
+
<a href="https://www.buymeacoffee.com/alexford" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/lato-white.png" alt="Buy Me A Beer" height="51" width="217" style="height: 51px !important;width: 217px !important;" ></a>
|
data/Rakefile
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "bundler/gem_tasks"
|
4
4
|
require "rake/testtask"
|
5
|
+
require 'yard'
|
5
6
|
|
6
7
|
Rake::TestTask.new(:test) do |t|
|
7
8
|
t.libs << "test"
|
@@ -10,3 +11,8 @@ Rake::TestTask.new(:test) do |t|
|
|
10
11
|
end
|
11
12
|
|
12
13
|
task default: :test
|
14
|
+
|
15
|
+
YARD::Rake::YardocTask.new do |t|
|
16
|
+
t.files = ['lib/**/*.rb'] # optional
|
17
|
+
t.options = ['-odocs'] # optional
|
18
|
+
end
|
data/lib/twilito.rb
CHANGED
@@ -13,7 +13,7 @@ module Twilito
|
|
13
13
|
include API
|
14
14
|
|
15
15
|
def send_sms(**args)
|
16
|
-
response = send_sms!(args)
|
16
|
+
response = send_sms!(**args)
|
17
17
|
Result.success(
|
18
18
|
response: response,
|
19
19
|
sid: JSON.parse(response.read_body)['sid']
|
@@ -28,21 +28,17 @@ module Twilito
|
|
28
28
|
args = merge_configuration(args)
|
29
29
|
|
30
30
|
send_response(args).tap do |response|
|
31
|
-
unless response.is_a? Net::HTTPSuccess
|
32
|
-
raise SendError.new('Error from Twilio API', response)
|
33
|
-
end
|
31
|
+
raise SendError.new('Error from Twilio API', response) unless response.is_a? Net::HTTPSuccess
|
34
32
|
end
|
35
33
|
end
|
36
34
|
|
37
35
|
private
|
38
36
|
|
39
|
-
def merge_configuration(
|
37
|
+
def merge_configuration(args)
|
40
38
|
configuration.to_h.merge(args).tap do |merged|
|
41
39
|
missing_keys = merged.select { |_k, v| v.nil? }.keys
|
42
40
|
|
43
|
-
if missing_keys.any?
|
44
|
-
raise ArgumentError, "Missing argument(s): #{missing_keys.join(', ')}"
|
45
|
-
end
|
41
|
+
raise ArgumentError, "Missing argument(s): #{missing_keys.join(', ')}" if missing_keys.any?
|
46
42
|
end
|
47
43
|
end
|
48
44
|
end
|
data/lib/twilito/api.rb
CHANGED
@@ -7,9 +7,9 @@ module Twilito
|
|
7
7
|
|
8
8
|
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
9
9
|
req = Net::HTTP::Post.new(uri)
|
10
|
-
req.initialize_http_header(
|
10
|
+
req.initialize_http_header('User-Agent' => user_agent)
|
11
11
|
req.basic_auth(args[:account_sid], args[:auth_token])
|
12
|
-
req.set_form_data(
|
12
|
+
req.set_form_data(twilio_form_data(args))
|
13
13
|
|
14
14
|
http.request(req)
|
15
15
|
end
|
@@ -31,12 +31,17 @@ module Twilito
|
|
31
31
|
|
32
32
|
private
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
# NOTE: Converts snake_cased hash of arguments to CamelCase to match Twilio
|
35
|
+
# API expectations. Also, removes auth_token and account_sid as those are
|
36
|
+
# included separately in .send_response as basic auth instead of POST body
|
37
|
+
def twilio_form_data(args)
|
38
|
+
args
|
39
|
+
.merge(auth_token: nil, account_sid: nil).compact
|
40
|
+
.reduce({}) { |result, (k, v)| result.merge(k.to_s.split('_').collect(&:capitalize).join => v) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def user_agent
|
44
|
+
"Ruby Twilito/#{Twilito::VERSION}"
|
40
45
|
end
|
41
46
|
end
|
42
47
|
end
|
data/lib/twilito/result.rb
CHANGED
@@ -1,17 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Twilito
|
4
|
-
Result
|
4
|
+
class Result
|
5
|
+
attr_reader :success, :errors, :sid, :response
|
6
|
+
|
5
7
|
def initialize(success:, errors: [], sid: nil, response: nil)
|
6
|
-
|
8
|
+
@success = success
|
9
|
+
@errors = errors
|
10
|
+
@sid = sid
|
11
|
+
@response = response
|
7
12
|
end
|
8
13
|
|
9
|
-
def self.success(
|
10
|
-
new(success: true,
|
14
|
+
def self.success(response:, sid:)
|
15
|
+
new(success: true, response: response, sid: sid)
|
11
16
|
end
|
12
17
|
|
13
|
-
def self.failure(
|
14
|
-
new(success: false,
|
18
|
+
def self.failure(response:, errors:)
|
19
|
+
new(success: false, response: response, errors: errors)
|
15
20
|
end
|
16
21
|
|
17
22
|
def data
|
@@ -19,15 +24,15 @@ module Twilito
|
|
19
24
|
end
|
20
25
|
|
21
26
|
def success?
|
22
|
-
|
27
|
+
success || false
|
23
28
|
end
|
24
29
|
|
25
30
|
private
|
26
31
|
|
27
32
|
def response_body
|
28
|
-
return nil unless
|
33
|
+
return nil unless response&.respond_to?(:read_body)
|
29
34
|
|
30
|
-
|
35
|
+
response.read_body
|
31
36
|
end
|
32
37
|
end
|
33
38
|
end
|
data/lib/twilito/version.rb
CHANGED
data/twilito.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.licenses = ['MIT']
|
10
10
|
spec.version = Twilito::VERSION
|
11
11
|
spec.authors = ["Alex Ford"]
|
12
|
-
spec.email = ["
|
12
|
+
spec.email = ["alexford@hey.com"]
|
13
13
|
spec.required_ruby_version = '>= 2.4.0'
|
14
14
|
|
15
15
|
spec.summary = "A tiny, zero dependency, and easy to test helper for sending text messages with Twilio"
|
@@ -25,6 +25,8 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency "bundler", "~> 2.0"
|
26
26
|
spec.add_development_dependency "minitest", "~> 5.11"
|
27
27
|
spec.add_development_dependency "pry"
|
28
|
-
spec.add_development_dependency "rake", "~>
|
29
|
-
spec.add_development_dependency "
|
28
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
29
|
+
spec.add_development_dependency "rubocop"
|
30
|
+
spec.add_development_dependency "webmock", "~> 3.12"
|
31
|
+
spec.add_development_dependency "yard", "~> 0.9"
|
30
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twilito
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Ford
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,35 +58,64 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '13.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
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'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: webmock
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: '3'
|
89
|
+
version: '3.12'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.12'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: yard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.9'
|
76
104
|
type: :development
|
77
105
|
prerelease: false
|
78
106
|
version_requirements: !ruby/object:Gem::Requirement
|
79
107
|
requirements:
|
80
108
|
- - "~>"
|
81
109
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
83
|
-
description:
|
110
|
+
version: '0.9'
|
111
|
+
description:
|
84
112
|
email:
|
85
|
-
-
|
113
|
+
- alexford@hey.com
|
86
114
|
executables: []
|
87
115
|
extensions: []
|
88
116
|
extra_rdoc_files: []
|
89
117
|
files:
|
118
|
+
- ".github/workflows/main.yml"
|
90
119
|
- ".gitignore"
|
91
120
|
- ".rubocop.yml"
|
92
121
|
- ".travis.yml"
|
@@ -108,7 +137,7 @@ homepage: https://github.com/alexford/twilito
|
|
108
137
|
licenses:
|
109
138
|
- MIT
|
110
139
|
metadata: {}
|
111
|
-
post_install_message:
|
140
|
+
post_install_message:
|
112
141
|
rdoc_options: []
|
113
142
|
require_paths:
|
114
143
|
- lib
|
@@ -124,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
153
|
version: '0'
|
125
154
|
requirements: []
|
126
155
|
rubygems_version: 3.0.3
|
127
|
-
signing_key:
|
156
|
+
signing_key:
|
128
157
|
specification_version: 4
|
129
158
|
summary: A tiny, zero dependency, and easy to test helper for sending text messages
|
130
159
|
with Twilio
|