minitext 0.0.6
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 +7 -0
- data/Rakefile +18 -0
- data/lib/minitext.rb +32 -0
- data/lib/minitext/message.rb +47 -0
- data/lib/minitext/missing_parameter.rb +3 -0
- data/lib/minitext/railtie.rb +25 -0
- data/lib/minitext/test_gateway.rb +13 -0
- data/lib/minitext/twilio_gateway.rb +23 -0
- data/lib/minitext/whitelist_proxy.rb +24 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9c09b84aeebf872441bef7d4269ad34c7036d5cf
|
4
|
+
data.tar.gz: 9dd04e82430dd4654ed93a935774170df8128d05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a49958f36894e6da2268be5336011183527e1f02679e5503cad88a4418862e02a3beffc9dc62e3e8cad2ab87e5b13268e0e5cffc73b05f25ae5a29e47bdfb17
|
7
|
+
data.tar.gz: 220380f7378e04189e00343b0c6a68afe1bd43ed91b19f58fc72a1025a43aa570f2776101bc9b5c2c0735f18d527ff9265278522055cdbb30f246fe0c8dbc049
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'rake/testtask'
|
10
|
+
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.libs << 'test'
|
14
|
+
t.pattern = 'test/**/*_test.rb'
|
15
|
+
t.verbose = false
|
16
|
+
end
|
17
|
+
|
18
|
+
task default: :test
|
data/lib/minitext.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Minitext
|
2
|
+
autoload :Message, 'minitext/message'
|
3
|
+
autoload :MissingParameter, 'minitext/missing_parameter'
|
4
|
+
autoload :TestGateway, 'minitext/test_gateway'
|
5
|
+
autoload :TwilioGateway, 'minitext/twilio_gateway'
|
6
|
+
autoload :WhitelistProxy, 'minitext/whitelist_proxy'
|
7
|
+
|
8
|
+
def self.gateway
|
9
|
+
@gateway ||= TestGateway.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.gateway=(gateway)
|
13
|
+
@gateway = gateway
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.defaults
|
17
|
+
@defaults ||= {}
|
18
|
+
{gateway: gateway}.merge(@defaults)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.defaults=(defaults)
|
22
|
+
@defaults = defaults
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.text(params)
|
26
|
+
Message.new(defaults.merge(params))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if defined?(Rails)
|
31
|
+
require 'minitext/railtie'
|
32
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Minitext
|
2
|
+
class Message
|
3
|
+
attr_accessor :from, :to, :body, :gateway
|
4
|
+
|
5
|
+
def initialize(params)
|
6
|
+
params.each do |attr, value|
|
7
|
+
self.public_send("#{attr}=", value)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def deliver!
|
12
|
+
deliver || raise_errors
|
13
|
+
end
|
14
|
+
|
15
|
+
def deliver
|
16
|
+
return false unless valid?
|
17
|
+
!!gateway.deliver(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid?
|
21
|
+
valid_param?(from) && valid_param?(to) && valid_param?(body) && !gateway.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def valid_param?(param)
|
27
|
+
!(param.nil? || param.empty?)
|
28
|
+
end
|
29
|
+
|
30
|
+
def raise_errors
|
31
|
+
case
|
32
|
+
when !valid_param?(from)
|
33
|
+
raise_missing_parameter('from')
|
34
|
+
when !valid_param?(to)
|
35
|
+
raise_missing_parameter('to')
|
36
|
+
when !valid_param?(body)
|
37
|
+
raise_missing_parameter('body')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def raise_missing_parameter(param)
|
44
|
+
raise Minitext::MissingParameter.new("#{param} parameter cannot be nil or empty")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'minitext'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module Minitext
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
config.minitext = ActiveSupport::OrderedOptions.new
|
7
|
+
|
8
|
+
initializer "minitext.configure" do |app|
|
9
|
+
Minitext.gateway = app.config.minitext.gateway
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Usage:
|
15
|
+
# development
|
16
|
+
# config.minitext.gateway = Minitext::TestGateway.new
|
17
|
+
|
18
|
+
# production
|
19
|
+
# config.minitext.gateway = Minitext::TwilioGateway.new(sid: '123', token: 'abc')
|
20
|
+
|
21
|
+
# staging
|
22
|
+
# whitelist = YAML.load_file('twilio_whitelist')
|
23
|
+
# gateway = Minitext::TwilioGateway.new(sid: '123', token: 'abc')
|
24
|
+
# config.minitext.gateway = Minitext::WhitelistProxy.new(whitelist: whitelist, gateway: gateway)
|
25
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'twilio-ruby'
|
2
|
+
|
3
|
+
module Minitext
|
4
|
+
class TwilioGateway
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(config={})
|
8
|
+
sid = config[:sid]
|
9
|
+
token = config[:token]
|
10
|
+
subaccount = config[:subaccount]
|
11
|
+
@client = Twilio::REST::Client.new(sid, token)
|
12
|
+
@client = client.accounts.find(subaccount) if subaccount
|
13
|
+
end
|
14
|
+
|
15
|
+
def deliver(message)
|
16
|
+
client.sms.messages.create(
|
17
|
+
from: message.from,
|
18
|
+
to: message.to,
|
19
|
+
body: message.body.strip,
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Minitext
|
2
|
+
class WhitelistProxy
|
3
|
+
attr_reader :whitelist, :gateway
|
4
|
+
|
5
|
+
def initialize(params)
|
6
|
+
@whitelist = Array(params.fetch(:whitelist) {Hash.new})
|
7
|
+
@gateway = params.fetch(:gateway) {TestGateway.new}
|
8
|
+
end
|
9
|
+
|
10
|
+
def deliver(message)
|
11
|
+
gateway.deliver(message) if whitelisted?(message.to)
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method, *args, &block)
|
15
|
+
gateway.send(method, *args, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def whitelisted?(recipient)
|
21
|
+
whitelist.include?(recipient)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minitext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Rippey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: twilio-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A lightweight SMS framework
|
28
|
+
email: kylerippey@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- Rakefile
|
34
|
+
- lib/minitext.rb
|
35
|
+
- lib/minitext/message.rb
|
36
|
+
- lib/minitext/missing_parameter.rb
|
37
|
+
- lib/minitext/railtie.rb
|
38
|
+
- lib/minitext/test_gateway.rb
|
39
|
+
- lib/minitext/twilio_gateway.rb
|
40
|
+
- lib/minitext/whitelist_proxy.rb
|
41
|
+
homepage: http://github.com/kylerippey/minitext
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.4.5
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: ''
|
65
|
+
test_files: []
|