clickatell-catcher-rack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7d83eb0da1bfa1e2b86a1b2b5b6d30ee885180e
4
+ data.tar.gz: a69ce1ef3b9864ba29841f4b16b5e828379893b3
5
+ SHA512:
6
+ metadata.gz: 0426d725d9453cae03fb2982b4f2853ae0aac8f8d279a7f725d6638df3be0a60962b40aef8e0d1f1cbd2686ae94a85f20e91016e73ec22ccddc79bc68e23c037
7
+ data.tar.gz: 591acd4e895c453b08638533e8be58ac7fa8ab5c1a387d6bafe6638c69d4f5c85b90e982ec6faf3e9f24e88df47be049185058f54e3613d3b914885b5813abf8
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,11 @@
1
+ .bundle/
2
+ .yardoc
3
+ Gemfile.lock
4
+ _yardoc/
5
+ coverage/
6
+ doc/
7
+ pkg/
8
+ spec/reports/
9
+ tmp/
10
+ vendor/bundle
11
+ clickatell_catcher.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ Metrics/LineLength:
2
+ Max: 105
3
+
4
+ Documentation:
5
+ Enabled: false
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clickatell-catcher-rack.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Grant Petersen-Speelman
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,63 @@
1
+ # Clickatell::Catcher::Rack
2
+
3
+ [![Build Status](https://travis-ci.org/grantspeelman/clickatell-catcher-rack.svg?branch=master)](https://travis-ci.org/grantspeelman/clickatell-catcher-rack)
4
+
5
+ Simple Lightweight rack app the allows for a local sandbox clickatell enviroment.
6
+ This Rack app emulates some of the [Clickatells api](https://www.clickatell.com/help/apidocs/)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ group :development do
14
+ gem 'clickatell-catcher-rack'
15
+ end
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install clickatell-catcher-rack
25
+
26
+ ## Usage
27
+
28
+ using in rails add a initializers file with the following:
29
+
30
+ ```ruby
31
+ if Rails.env.development?
32
+ require 'clickatell/catcher/rack'
33
+ Rails.application.configure do
34
+ config.middleware.use Clickatell::Catcher::Rack::Middleware, logger: Rails.logger
35
+ end
36
+ end
37
+ ```
38
+ or for other rack apps add the following to your config.ru
39
+
40
+ ```ruby
41
+ require 'clickatell/catcher/rack'
42
+ use Clickatell::Catcher::Rack::Middleware
43
+ ```
44
+
45
+ Now in development mode you can point you clickatell sms code at `http://localhost:3000/rest/message` and
46
+ visit `http://localhost:3000/rest/message` to see the messages.
47
+ Suggested a config to be able to switch between `https://api.clickatell.com/rest/message` and `http://localhost:3000/rest/message`
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/grantspeelman/clickatell-catcher-rack.
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
63
+
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ require 'rubocop/rake_task'
6
+ RuboCop::RakeTask.new
7
+
8
+ require 'rspec/core/rake_task'
9
+
10
+ RSpec::Core::RakeTask.new(:spec)
11
+
12
+ task default: %w(rubocop spec build)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'clickatell/catcher/rack'
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
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install --path vendor/bundle
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clickatell/catcher/rack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'clickatell-catcher-rack'
8
+ spec.version = Clickatell::Catcher::Rack::VERSION
9
+ spec.authors = ['Grant Petersen-Speelman']
10
+ spec.email = ['grantspeelman@gmail.com']
11
+
12
+ spec.summary = 'local sms catcher clickatell environment.'
13
+ spec.description = 'Simple rack app the allows for a local catcher clickatell environment.'
14
+ spec.homepage = 'https://github.com/grantspeelman/clickatell-catcher-rack'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'rack'
23
+ spec.add_dependency 'multi_json', '~> 1.0'
24
+
25
+ spec.add_development_dependency 'rack-test'
26
+ spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'coveralls'
28
+ spec.add_development_dependency 'bundler', '~> 1.11'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('lib/clickatell/catcher/rack', __dir__)
4
+ run Clickatell::Catcher::Rack::Middleware
@@ -0,0 +1,56 @@
1
+ require 'rack/request'
2
+ require 'rack/nulllogger'
3
+
4
+ require 'clickatell/catcher/rack/version'
5
+ require 'clickatell/catcher/rack/message_adder'
6
+ require 'clickatell/catcher/rack/messages_renderer'
7
+ require 'clickatell/catcher/rack/shared_array'
8
+
9
+ module Clickatell
10
+ module Catcher
11
+ module Rack
12
+ class Middleware
13
+ attr_reader :messages
14
+
15
+ def initialize(app, options = {})
16
+ @app = app
17
+ @messages = Clickatell::Catcher::Rack::SharedArray.new
18
+ @logger = options[:logger] || ::Rack::NullLogger.new(nil)
19
+ @message_adder = MessageAdder.new(@messages)
20
+ @messages_renderer = MessagesRenderer.new(@messages)
21
+ end
22
+
23
+ def add_message(request_body)
24
+ @message_adder.add(request_body)
25
+ @logger.debug("[#{Process.pid}] Messages: " + @messages.inspect)
26
+ @message_adder.rack_response
27
+ end
28
+
29
+ def render_messages
30
+ @logger.debug("[#{Process.pid}] Rendering Messages: " + @messages.inspect)
31
+ @messages_renderer.rack_response
32
+ end
33
+
34
+ def rest_message(request)
35
+ if request.get?
36
+ render_messages
37
+ else
38
+ add_message(request.body.read)
39
+ end
40
+ end
41
+
42
+ def call(env)
43
+ request = ::Rack::Request.new(env)
44
+ if request.path == '/rest/message'
45
+ rest_message(request)
46
+ else
47
+ status, headers, body = @app.call(env)
48
+
49
+ # return result
50
+ [status, headers, body]
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,35 @@
1
+ require 'multi_json'
2
+
3
+ module Clickatell
4
+ module Catcher
5
+ module Rack
6
+ class MessageAdder
7
+ def initialize(messages)
8
+ @messages = messages
9
+ @message_id = 1
10
+ end
11
+
12
+ def add(request_body)
13
+ message = MultiJson.load(request_body)
14
+ @messages << message.merge('added_at' => Time.now)
15
+ response = { 'data' => { 'message' => build_messages_response(message) } }
16
+ @json_body = MultiJson.dump(response)
17
+ end
18
+
19
+ def rack_response
20
+ [200, { 'Content-Type' => 'application/json' }, [@json_body]]
21
+ end
22
+
23
+ private
24
+
25
+ def build_messages_response(message)
26
+ start_index = @message_id
27
+ @message_id += message['to'].size
28
+ message['to'].map.with_index(start_index) do |to, i|
29
+ { 'accepted' => true, 'to' => to, 'apiMessageId' => i.to_s }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,81 @@
1
+ <html>
2
+ <head>
3
+ <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
4
+ <!--[if lte IE 8]>
5
+ <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-old-ie-min.css">
6
+ <![endif]-->
7
+ <!--[if gt IE 8]><!-->
8
+ <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-min.css">
9
+ <!--<![endif]-->
10
+ <meta name="viewport" content="width=device-width, initial-scale=1">
11
+
12
+ <style>
13
+ .custom-menu-wrapper {
14
+ background-color: #808080;
15
+ margin-bottom: 2.5em;
16
+ white-space: nowrap;
17
+ position: relative;
18
+ }
19
+
20
+ .custom-menu {
21
+ display: inline-block;
22
+ width: auto;
23
+ vertical-align: middle;
24
+ -webkit-font-smoothing: antialiased;
25
+ }
26
+
27
+ .custom-menu .pure-menu-heading {
28
+ color: white;
29
+ }
30
+
31
+ .custom-menu .pure-menu-heading:hover {
32
+ background-color: transparent;
33
+ }
34
+
35
+ .custom-menu-top {
36
+ position: relative;
37
+ padding-top: .5em;
38
+ padding-bottom: .5em;
39
+ }
40
+
41
+ .custom-menu-brand {
42
+ display: block;
43
+ text-align: center;
44
+ position: relative;
45
+ }
46
+ </style>
47
+ </head>
48
+ <body>
49
+
50
+ <div class="custom-menu-wrapper">
51
+ <div class="pure-menu custom-menu custom-menu-top">
52
+ <span class="pure-menu-heading custom-menu-brand">Clickatell sms catcher</span>
53
+ </div>
54
+ </div>
55
+
56
+ <div class="content pure-g">
57
+ <div class="pure-u-1-24"></div>
58
+ <div class="pure-u-23-24">
59
+ <table class="pure-table pure-table-horizontal">
60
+ <thead>
61
+ <tr>
62
+ <th>Time</th>
63
+ <th>To</th>
64
+ <th>Text</th>
65
+ </tr>
66
+ </thead>
67
+
68
+ <tbody>
69
+ <% @messages.each do |message| %>
70
+ <tr>
71
+ <td><%= message['added_at'] %></td>
72
+ <td><%= message['to'].join(',') %></td>
73
+ <td><%= message['text'] %></td>
74
+ </tr>
75
+ <% end %>
76
+ </tbody>
77
+ </table>
78
+ </div>
79
+ </div>
80
+ </body>
81
+ </html>
@@ -0,0 +1,27 @@
1
+ require 'multi_json'
2
+ require 'erb'
3
+
4
+ module Clickatell
5
+ module Catcher
6
+ module Rack
7
+ class MessagesRenderer
8
+ def initialize(messages)
9
+ @messages = messages
10
+ @renderer = ERB.new(File.read(__dir__ + '/messages.html.erb'))
11
+ end
12
+
13
+ def rack_response
14
+ [200,
15
+ { 'Content-Type' => 'text/html' },
16
+ [render_content]]
17
+ end
18
+
19
+ private
20
+
21
+ def render_content
22
+ @renderer.result(binding)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,54 @@
1
+ require 'yaml/store'
2
+
3
+ module Clickatell
4
+ module Catcher
5
+ module Rack
6
+ class SharedArray
7
+ include Enumerable
8
+
9
+ def initialize
10
+ @store = YAML::Store.new(store_patch)
11
+ @store.transaction do |store|
12
+ store[:data] ||= []
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def store_patch
19
+ if File.directory?('tmp')
20
+ 'tmp/clickatell_catcher.yml'
21
+ else
22
+ 'clickatell_catcher.yml'
23
+ end
24
+ end
25
+
26
+ public
27
+
28
+ def clear
29
+ @store.transaction do |store|
30
+ store[:data] = []
31
+ end
32
+ end
33
+
34
+ def <<(item)
35
+ @store.transaction do |store|
36
+ store[:data].unshift(item)
37
+ store[:data].pop if store[:data].size > 25
38
+ end
39
+ end
40
+
41
+ def each
42
+ data = @store.transaction { @store[:data] }
43
+ data.each do |item|
44
+ yield(item)
45
+ end
46
+ end
47
+
48
+ def size
49
+ @store.transaction { @store[:data] }.size
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,7 @@
1
+ module Clickatell
2
+ module Catcher
3
+ module Rack
4
+ VERSION = '0.1.0'.freeze
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clickatell-catcher-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Grant Petersen-Speelman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
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
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
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: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.11'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.11'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ description: Simple rack app the allows for a local catcher clickatell environment.
126
+ email:
127
+ - grantspeelman@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .coveralls.yml
133
+ - .gitignore
134
+ - .rspec
135
+ - .rubocop.yml
136
+ - .travis.yml
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - clickatell-sandbox-rack.gemspec
144
+ - config.ru
145
+ - lib/clickatell/catcher/rack.rb
146
+ - lib/clickatell/catcher/rack/message_adder.rb
147
+ - lib/clickatell/catcher/rack/messages.html.erb
148
+ - lib/clickatell/catcher/rack/messages_renderer.rb
149
+ - lib/clickatell/catcher/rack/shared_array.rb
150
+ - lib/clickatell/catcher/rack/version.rb
151
+ homepage: https://github.com/grantspeelman/clickatell-catcher-rack
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - '>='
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.0.14
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: local sms catcher clickatell environment.
175
+ test_files: []