fake-twilio 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/fake-twilio-server +12 -0
- data/lib/fake_twilio.rb +7 -0
- data/lib/fake_twilio/message.rb +50 -0
- data/lib/fake_twilio/server/routes.rb +17 -0
- data/lib/fake_twilio/server/routes/messages.rb +14 -0
- data/lib/fake_twilio/server/routes/phonenumbers.rb +11 -0
- data/lib/fake_twilio/version.rb +4 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de6d635593a06194d4db8e849824a970948b1399
|
4
|
+
data.tar.gz: 82b79c432976501450842bd56b25894ede442b65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 897d89a935e54708473f2eeab2031072e6fe1b85175a788190d8c9a5157327b69925f34e886e914af075445ffd6f5342e6e3d969f3a4655b064895dc27c1960e
|
7
|
+
data.tar.gz: f43ebfde09f51ee39b054de0b1ad19d6b55138944951b1ab193b9945cda57e874f9e7401af394d922cba5e972a8c3a09a81bd6ba42ffde07e2e5ce3e063570cf
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'sinatra'
|
4
|
+
|
5
|
+
WRITE_FILES = ENV.key?('WRITE_OUTPUT')
|
6
|
+
|
7
|
+
# By default, bind to all interfaces on port 4444, using the thin webserver.
|
8
|
+
set :bind, ENV.key?('LISTEN_IP') ? ENV['LISTEN_IP'] : '0.0.0.0'
|
9
|
+
set :port, ENV.key?('LISTEN_PORT') ? ENV['LISTEN_PORT'] : 4444
|
10
|
+
set :server, ENV.key?('HTTP_SERVER') ? ENV['HTTP_SERVER'] : 'thin'
|
11
|
+
|
12
|
+
require 'fake_twilio/server/routes'
|
data/lib/fake_twilio.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module FakeTwilio
|
2
|
+
# Minimal implementation to mimic sending of SMS messages.
|
3
|
+
class Message
|
4
|
+
attr_reader :to, :body, :success, :sent
|
5
|
+
|
6
|
+
# Mimic processing a Message.
|
7
|
+
def initialize(params)
|
8
|
+
@success = verify_params(params)
|
9
|
+
@to = params['To']
|
10
|
+
@body = params['Body']
|
11
|
+
@sent = Time.now
|
12
|
+
end
|
13
|
+
|
14
|
+
# Produce output representing whether the SMS succeeded or failed.
|
15
|
+
def to_json
|
16
|
+
[status, price, date]
|
17
|
+
.reduce(&:merge)
|
18
|
+
.to_json
|
19
|
+
end
|
20
|
+
|
21
|
+
# Represent the date sent. If the attempt to send an SMS failed, do not
|
22
|
+
# list the date.
|
23
|
+
def date
|
24
|
+
success ? { 'datesent' => sent } : {}
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# Represent the state of the message. Since no actual process exists,
|
30
|
+
# constrain possible statuses to success or failure.
|
31
|
+
def status
|
32
|
+
{ 'status' => success ? 'sent' : 'failed' }
|
33
|
+
end
|
34
|
+
|
35
|
+
# Represent the cost to send the message. Possibilities are constrained to
|
36
|
+
# 3 cents and nothing, predicated on success.
|
37
|
+
def price
|
38
|
+
{ 'price' => success ? '0.03' : '0.00' }
|
39
|
+
end
|
40
|
+
|
41
|
+
# Implement minimal checking for the parameters. The body must be between
|
42
|
+
# 1 and 1600 bytes and a recipient must be specified.
|
43
|
+
def verify_params(params)
|
44
|
+
return false if params['Body'].to_s.empty?
|
45
|
+
return false if params['Body'].to_s.bytes.size > 1600
|
46
|
+
return false unless /\+\d+$/.match(params['To'])
|
47
|
+
true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'fake_twilio/server/routes/messages'
|
2
|
+
require 'fake_twilio/server/routes/phonenumbers'
|
3
|
+
|
4
|
+
helpers do
|
5
|
+
# Optionally write a file, predicated upon the $WRITE_FILES global variable.
|
6
|
+
# Any number of directories can be passed to path glob, which will be joined
|
7
|
+
# in an OS independent fashion.
|
8
|
+
# The full directory structure must exist at the time of testing.
|
9
|
+
def write_file(*path, content)
|
10
|
+
return unless WRITE_FILES
|
11
|
+
|
12
|
+
prefix = ENV.key?('PREFIX') ? ENV['PREFIX'] : '.'
|
13
|
+
File.open(File.join(File.expand_path(prefix), *path), 'w') do |file|
|
14
|
+
file.write(content)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'fake_twilio'
|
2
|
+
|
3
|
+
# Emulate the Messages API, reporting the delivered SMS via stdout and
|
4
|
+
# optionally writing message info to a file.
|
5
|
+
post "/#{FakeTwilio::API_VERSION}/Accounts/:account_sid/Messages.?:format" do
|
6
|
+
message = FakeTwilio::Message.new(params)
|
7
|
+
if message.success
|
8
|
+
path = "sms_#{message.to.delete('+')}_#{Time.now.to_i}"
|
9
|
+
write_file('sms', path, message.body)
|
10
|
+
puts "Message delivered to #{message.to} on #{message.sent}"
|
11
|
+
end
|
12
|
+
|
13
|
+
message.to_json
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'fake_twilio'
|
2
|
+
|
3
|
+
# Emulate the minimal phone number lookup API
|
4
|
+
# Note that a real ISO country code is NOT returned.
|
5
|
+
get '/v1/PhoneNumbers/:number' do
|
6
|
+
{
|
7
|
+
'country_code': 'XX',
|
8
|
+
'phone_number': params['number'],
|
9
|
+
'national_format': params['number']
|
10
|
+
}.to_json
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fake-twilio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Instructure, Inc.
|
8
|
+
- Tina Wuest
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-12-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sinatra
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.4'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.4'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: thin
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.6'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.6'
|
42
|
+
description: Fake Twilio service to aid in local development
|
43
|
+
email: cwuest@instructure.com
|
44
|
+
executables:
|
45
|
+
- fake-twilio-server
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- bin/fake-twilio-server
|
50
|
+
- lib/fake_twilio.rb
|
51
|
+
- lib/fake_twilio/message.rb
|
52
|
+
- lib/fake_twilio/server/routes.rb
|
53
|
+
- lib/fake_twilio/server/routes/messages.rb
|
54
|
+
- lib/fake_twilio/server/routes/phonenumbers.rb
|
55
|
+
- lib/fake_twilio/version.rb
|
56
|
+
homepage: https://github.com/wuest/fake-twilio-ruby
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.1.1
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.4.8
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Fake Twilio service to aid in local development
|
80
|
+
test_files: []
|