twilito 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7761e9e3ab42c77312bdb7207896ece722ce001a9a1c099c4a79b702f72c2aba
4
+ data.tar.gz: e1e4830cf7ea75dd00f55ffb796bf034128490a2ecbc6e066d8195771e6bf133
5
+ SHA512:
6
+ metadata.gz: 12b4f5cf4934394c5e4e2f2ff3a9931c42ea6d33ebdd8faa0e7a53f27ee89a6a7a779b2cac9b122264cfbd7358556c974728e7550075db92449b1f873a6b2ba8
7
+ data.tar.gz: 7a4e56dfac70088de70c35bd34012573bb87c807461883062ec41a6149716676d2317f3fb849947b8a36b8bafe88251550f0f9d61d083c2840cc2a1ebe24efcc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /tmp/
8
+
9
+ .ruby-version
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "bin/**/*"
4
+ - "twilito.gemspec"
5
+
6
+ Metrics/BlockLength:
7
+ Exclude:
8
+ - "test/**/*"
9
+
10
+ Style/StringLiterals:
11
+ Enabled: false
12
+
13
+ Layout/MultilineMethodCallIndentation:
14
+ EnforcedStyle: indented
15
+ IndentationWidth: 2
16
+
17
+ Style/Documentation:
18
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ ruby '2.6.1'
4
+
5
+ source "https://rubygems.org"
6
+
7
+ # Specify your gem's dependencies in twilito.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ twilito (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.6.0)
10
+ public_suffix (>= 2.0.2, < 4.0)
11
+ coderay (1.1.2)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ hashdiff (0.3.8)
15
+ method_source (0.9.2)
16
+ minitest (5.11.3)
17
+ pry (0.12.2)
18
+ coderay (~> 1.1.0)
19
+ method_source (~> 0.9.0)
20
+ public_suffix (3.0.3)
21
+ rake (10.5.0)
22
+ safe_yaml (1.0.5)
23
+ webmock (3.5.1)
24
+ addressable (>= 2.3.6)
25
+ crack (>= 0.3.2)
26
+ hashdiff
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ bundler (~> 2.0)
33
+ minitest (~> 5.11)
34
+ pry
35
+ rake (~> 10.0)
36
+ twilito!
37
+ webmock (~> 3)
38
+
39
+ RUBY VERSION
40
+ ruby 2.6.1p33
41
+
42
+ BUNDLED WITH
43
+ 2.0.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Alex Ford
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Twilito
2
+
3
+ [![Build Status](https://travis-ci.org/alexford/twilito.svg?branch=master)](https://travis-ci.org/alexford/twilito)
4
+
5
+ A tiny, zero dependency helper for sending text messages with Twilio
6
+
7
+ ## Why
8
+
9
+ Twilio's full on Ruby library does a lot, and has a large memory footprint—too large for just sending an SMS. It's also more difficult to mock and verify than I'd like for a simple task like sending an individual SMS.
10
+
11
+ Using Twilio's REST API directly is fine, but can be cumbersome.
12
+
13
+ Twilito is just enough of a wrapper to abstract away the REST API without loading the rest of the Twilio ecosystem.
14
+
15
+ ## Usage
16
+
17
+ #### Install the gem
18
+
19
+ ```
20
+ gem 'twilito'
21
+ ```
22
+
23
+ #### Simplest case
24
+
25
+ ```ruby
26
+ # All options are required (but can be defaulted, see below)
27
+ result = Twilito.send_sms(
28
+ to: '+15555555555',
29
+ from: '+15554444444',
30
+ content: 'This is my content'
31
+ account_sid: '...', # Twilio Credentials
32
+ auth_token: '...'
33
+ )
34
+
35
+ # Returns Twilito::Result struct
36
+
37
+ result.success? # => boolean
38
+ result.errors # => [] or error messages
39
+ result.sid #=> Twilio SID for Message (SM[...])
40
+ result.response # => Raw response (instance of Net::HTTPResponse)
41
+ result.data # => Hash of response data (parsed from JSON)
42
+ ```
43
+
44
+ #### Use send! to raise on error instead
45
+
46
+ ```ruby
47
+ begin
48
+ Twilito.send_sms!(
49
+ to: '+15555555555',
50
+ from: '+12333',
51
+ body: 'This is my content',
52
+ account_sid: '...',
53
+ auth_token: '...'
54
+ )
55
+ rescue Twilito::SendError => e
56
+ e.message # => 'Error from Twilio API'
57
+ e.response # => Raw response (instance of Net::HTTPResponse)
58
+ end
59
+ ```
60
+
61
+ #### Every argument can be defaulted
62
+
63
+ ```ruby
64
+ # In an initializer or something like that:
65
+
66
+ Twilito.configure do |config|
67
+ # Store your secrets elsewhere
68
+ config.account_sid = ENV['TWILIO_ACCOUNT_SID']
69
+ config.auth_token = ENV['TWILIO_AUTH_TOKEN']
70
+
71
+ config.from = '+16145555555'
72
+ end
73
+ ```
74
+
75
+ ```ruby
76
+ # Later, in your code:
77
+
78
+ Twilito.send_sms!(to: '+15555555555', body: 'Foo')
79
+ ```
80
+
81
+ #### Really, everything
82
+
83
+ ```ruby
84
+ # In an initializer or something like that:
85
+
86
+ Twilito.configure do |config|
87
+ # Store your secrets elsewhere
88
+ config.account_sid = ENV['TWILIO_ACCOUNT_SID']
89
+ config.auth_token = ENV['TWILIO_AUTH_TOKEN']
90
+
91
+ config.from = '+16145555555'
92
+ config.to = '+15555555555'
93
+ config.body = 'A new user signed up'
94
+ end
95
+ ```
96
+
97
+ ```ruby
98
+ # Later, in your code:
99
+
100
+ Twilito.send_sms!
101
+ ```
102
+
103
+ ## Testing your code
104
+
105
+ _TODO: Add examples of mocking and/or test helpers for asserting your code sends an SMS_
106
+
107
+ ## Contributing
108
+
109
+ _TODO_
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twilito"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Twilito
4
+ module API
5
+ def send_response(args)
6
+ uri = messages_uri(args[:account_sid])
7
+
8
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
9
+ req = Net::HTTP::Post.new(uri)
10
+ req.basic_auth(args[:account_sid], args[:auth_token])
11
+ req.set_form_data(twilio_params(args))
12
+
13
+ http.request(req)
14
+ end
15
+ end
16
+
17
+ def messages_uri(account_sid)
18
+ components = [
19
+ Configuration::TWILIO_VERSION,
20
+ 'Accounts',
21
+ account_sid,
22
+ 'Messages.json'
23
+ ]
24
+
25
+ URI::HTTPS.build(
26
+ host: Configuration::TWILIO_HOST,
27
+ path: '/' + components.join('/')
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ def twilio_params(args)
34
+ {
35
+ 'To' => args[:to],
36
+ 'From' => args[:from],
37
+ 'Body' => args[:body]
38
+ }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Twilito
4
+ def self.configure
5
+ yield(configuration)
6
+ end
7
+
8
+ def self.configuration
9
+ @configuration ||= Configuration.new
10
+ end
11
+
12
+ def self.reset_configuration!
13
+ @configuration = Configuration.new
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :account_sid, :auth_token, :from, :to, :body,
18
+ :twilio_host, :twilio_version
19
+
20
+ TWILIO_HOST = 'api.twilio.com'
21
+ TWILIO_VERSION = '2010-04-01'
22
+
23
+ def to_h
24
+ {
25
+ to: to,
26
+ from: from,
27
+ body: body,
28
+ account_sid: account_sid,
29
+ auth_token: auth_token
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Twilito
4
+ Result = Struct.new(:success?, :errors, :sid, :response, keyword_init: true) do
5
+ def self.success(**args)
6
+ new(success?: true, **args)
7
+ end
8
+
9
+ def self.failure(**args)
10
+ new(success?: false, **args)
11
+ end
12
+
13
+ def errors
14
+ to_h[:errors] || []
15
+ end
16
+
17
+ def data
18
+ JSON.parse(response_body || '{}')
19
+ end
20
+
21
+ private
22
+
23
+ def response_body
24
+ return nil unless to_h[:response]&.respond_to?(:read_body)
25
+
26
+ to_h[:response].read_body
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Twilito
4
+ class SendError < StandardError
5
+ attr_reader :response
6
+
7
+ def initialize(message, response)
8
+ @response = response
9
+ super(message)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Twilito
4
+ VERSION = "0.1.0"
5
+ end
data/lib/twilito.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "twilito/version"
4
+ require "twilito/configuration"
5
+ require "twilito/result"
6
+ require "twilito/api"
7
+ require "twilito/send_error"
8
+
9
+ module Twilito
10
+ class ArgumentError < StandardError; end
11
+
12
+ class << self
13
+ include API
14
+
15
+ def send_sms(**args)
16
+ response = send_sms!(args)
17
+ Result.success(
18
+ response: response,
19
+ sid: JSON.parse(response.read_body)['sid']
20
+ )
21
+ rescue SendError => e
22
+ Result.failure(errors: [e.message], response: e.response)
23
+ rescue JSON::ParserError
24
+ Result.failure(errors: ['Unable to parse response'], response: response)
25
+ end
26
+
27
+ def send_sms!(**args)
28
+ args = merge_configuration(args)
29
+
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
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def merge_configuration(**args)
40
+ configuration.to_h.merge(args).tap do |merged|
41
+ missing_keys = merged.select { |_k, v| v.nil? }.keys
42
+
43
+ if missing_keys.any?
44
+ raise ArgumentError, "Missing argument(s): #{missing_keys.join(', ')}"
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
data/twilito.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "twilito/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "twilito"
9
+ spec.licenses = ['MIT']
10
+ spec.version = Twilito::VERSION
11
+ spec.authors = ["Alex Ford"]
12
+ spec.email = ["alexford87@me.com"]
13
+
14
+ spec.summary = "A tiny, zero dependency, and easy to test helper for sending text messages with Twilio"
15
+ spec.homepage = "https://github.com/alexford/twilito"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.0"
25
+ spec.add_development_dependency "minitest", "~> 5.11"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "webmock", "~> 3"
29
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twilito
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Ford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
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: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3'
83
+ description:
84
+ email:
85
+ - alexford87@me.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rubocop.yml"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/twilito.rb
101
+ - lib/twilito/api.rb
102
+ - lib/twilito/configuration.rb
103
+ - lib/twilito/result.rb
104
+ - lib/twilito/send_error.rb
105
+ - lib/twilito/version.rb
106
+ - twilito.gemspec
107
+ homepage: https://github.com/alexford/twilito
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.0.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A tiny, zero dependency, and easy to test helper for sending text messages
130
+ with Twilio
131
+ test_files: []