babysms 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ module BabySMS
2
+ class Receipt
3
+ attr_reader :message
4
+ attr_reader :adapter
5
+ attr_reader :exceptions
6
+ attr_reader :message_uuid
7
+
8
+ def initialize(message:, adapter:, exceptions:, message_uuid:)
9
+ @message = message
10
+ @adapter = adapter
11
+ @exceptions = exceptions
12
+ @message_uuid = message_uuid
13
+ end
14
+
15
+ def exceptions?
16
+ !exceptions.empty?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module BabySMS
2
+ class Report
3
+ attr_reader :adapter
4
+ attr_reader :log
5
+
6
+ def initialize(adapter)
7
+ @adapter = adapter
8
+ @log = []
9
+ end
10
+
11
+ def incoming_message(message)
12
+ log.push([adapter, :incoming_message, { message: message }])
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module BabySMS
2
+ VERSION = '0.5.1'.freeze
3
+ end
@@ -0,0 +1,87 @@
1
+ require 'babysms'
2
+ require 'sinatra/base'
3
+
4
+ # WebApplication is a Sinatra app that handles all incoming web hook
5
+ # HTTP requests. It multiplexes to all adapters that are enabled.
6
+ module BabySMS
7
+ class WebApplication < Sinatra::Base
8
+ helpers do
9
+ def h(text)
10
+ Rack::Utils.escape_html(text)
11
+ end
12
+ end
13
+
14
+ def error_response(code, msg)
15
+ [code, { 'Content-Type' => 'text/plain' }, msg]
16
+ end
17
+
18
+ post '/:adapter_id' do
19
+ adapter = BabySMS::Adapter.for_adapter_id(params['adapter_id'])
20
+ if adapter&.web_hook?
21
+ report = BabySMS::Report.new(adapter)
22
+ begin
23
+ adapter.web_hook.process(app: self, report: report)
24
+ rescue BabySMS::Malformed => e
25
+ error_response(500, "malformed: #{e.message}")
26
+ rescue BabySMS::Unauthorized => e
27
+ error_response(403, "unauthorized: #{e.message}")
28
+ rescue BabySMS::WebHookError => e
29
+ error_response(500, "web hook error: #{e.message}")
30
+ end
31
+ else
32
+ error_response(404, "not found")
33
+ end
34
+ end
35
+
36
+ get '/' do
37
+ index_html = <<~'HTML'.freeze
38
+ <!DOCTYPE html>
39
+ <head>
40
+ <title>BabySMS %<version>s Endpoint</title>
41
+ <style>
42
+ html { margin: 4em; font-family: monospace; }
43
+ body { max-width: 45em; }
44
+ h2 { margin-top: 2em; }
45
+ </style>
46
+ </head>
47
+ <body>
48
+ <h1>BabySMS %{version} Web Hook Endpoint</h1>
49
+ <p>BabySMS is reachable here to allow SMS providers to contact your web hooks.</p>
50
+ <p>This application should be off-limits to world at large, either via password
51
+ protection, host-based authorization, or both.
52
+
53
+ <p>See the <i>Web Hooks</i> section of the BabySMS README.
54
+
55
+ <h2>BabySMS Configuration:</h2>
56
+ <ul>%{config_list}</ul>
57
+
58
+ <h2>Web Hook Endpoints:</h2>
59
+ <ul>%{adapters_list}</ul>
60
+ </body>
61
+ </html>
62
+ HTML
63
+
64
+ config_list = {
65
+ web_hook_root: BabySMS.web_hook_root,
66
+ strategy: BabySMS.strategy,
67
+ adapters: BabySMS.adapters.map(&:adapter_id),
68
+ available_adapters: BabySMS.available_adapters(test: true).map(&:adapter_name)
69
+ }.map do |k, v|
70
+ "<li>#{h k}: #{h v.inspect}</li>"
71
+ end.join("")
72
+
73
+ adapters = BabySMS.adapters.select { |a| a.web_hook }
74
+ adapters_list = adapters.map do |a|
75
+ rel_href = h(a.web_hook.mount_point)
76
+ abs_href = h(a.web_hook.end_point)
77
+
78
+ "<li><a href='#{rel_href}'>#{abs_href}</a><br>" \
79
+ "<b>#{h a.class.name} for #{h a.from}</b></li>"
80
+ end.join("")
81
+
82
+ sprintf(index_html, config_list: config_list,
83
+ adapters_list: adapters_list,
84
+ version: h(BabySMS::VERSION))
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,21 @@
1
+ module BabySMS
2
+ class WebHook
3
+ attr_reader :adapter
4
+
5
+ def initialize(adapter)
6
+ @adapter = adapter
7
+ end
8
+
9
+ # This is where the webhook handler is mounted relative to the Rack application
10
+ def mount_point
11
+ "/#{adapter.adapter_id}"
12
+ end
13
+
14
+ # This is the public URL of the webhook
15
+ def end_point
16
+ # Remove optional trailing '/' in configuration
17
+ root = BabySMS.web_hook_root.gsub(/\/\z/, '')
18
+ root + mount_point
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,243 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: babysms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike A. Owens
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rainbow
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: phony
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0.14'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '1.17'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '1.17'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rack-test
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.1.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.1.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: twilio-ruby
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 5.18.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 5.18.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: nexmo
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 5.5.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 5.5.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: ruby-bandwidth
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: 1.0.20
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: 1.0.20
167
+ - !ruby/object:Gem::Dependency
168
+ name: plivo
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: 4.1.6
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: 4.1.6
181
+ description: Functionality to send SMS via Twilio, Nexmo, etc.
182
+ email:
183
+ - mike@filespanker.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".rspec"
190
+ - ".rubocop-airbnb.yml"
191
+ - ".rubocop.yml"
192
+ - ".travis.yml"
193
+ - Gemfile
194
+ - LICENSE.txt
195
+ - README.md
196
+ - Rakefile
197
+ - babysms.gemspec
198
+ - bin/console
199
+ - bin/setup
200
+ - config.ru
201
+ - lib/babysms.rb
202
+ - lib/babysms/adapter.rb
203
+ - lib/babysms/adapters/bandwidth_adapter.rb
204
+ - lib/babysms/adapters/nexmo_adapter.rb
205
+ - lib/babysms/adapters/plivo_adapter.rb
206
+ - lib/babysms/adapters/signalwire_adapter.rb
207
+ - lib/babysms/adapters/test_adapter.rb
208
+ - lib/babysms/adapters/twilio_adapter.rb
209
+ - lib/babysms/errors.rb
210
+ - lib/babysms/mail_man.rb
211
+ - lib/babysms/message.rb
212
+ - lib/babysms/receipt.rb
213
+ - lib/babysms/report.rb
214
+ - lib/babysms/version.rb
215
+ - lib/babysms/web_application.rb
216
+ - lib/babysms/web_hook.rb
217
+ homepage: https://github.com/mieko/babysms
218
+ licenses:
219
+ - MIT
220
+ metadata:
221
+ homepage_uri: https://github.com/mieko/babysms
222
+ source_code_uri: https://github.com/mieko/babysms
223
+ changelog_uri: https://github.com/mieko/babysms/CHANGELOG.md
224
+ post_install_message:
225
+ rdoc_options: []
226
+ require_paths:
227
+ - lib
228
+ required_ruby_version: !ruby/object:Gem::Requirement
229
+ requirements:
230
+ - - ">="
231
+ - !ruby/object:Gem::Version
232
+ version: '0'
233
+ required_rubygems_version: !ruby/object:Gem::Requirement
234
+ requirements:
235
+ - - ">="
236
+ - !ruby/object:Gem::Version
237
+ version: '0'
238
+ requirements: []
239
+ rubygems_version: 3.0.6
240
+ signing_key:
241
+ specification_version: 4
242
+ summary: Simple Rails interface to a SMS APIs
243
+ test_files: []