motorurl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +3 -0
  4. data/Rakefile +18 -0
  5. data/app/assets/stylesheets/motorurl/application.css +13 -0
  6. data/app/controllers/motorurl/application_controller.rb +12 -0
  7. data/app/controllers/motorurl/sensors_controller.rb +18 -0
  8. data/app/helpers/motorurl/application_helper.rb +4 -0
  9. data/app/views/layouts/motorurl/application.html.erb +14 -0
  10. data/config/routes.rb +3 -0
  11. data/lib/motorurl.rb +46 -0
  12. data/lib/motorurl/capistrano/recipes.rb +33 -0
  13. data/lib/motorurl/engine.rb +12 -0
  14. data/lib/motorurl/railtie.rb +7 -0
  15. data/lib/motorurl/registrar.rb +29 -0
  16. data/lib/motorurl/report.rb +22 -0
  17. data/lib/motorurl/transmitter.rb +41 -0
  18. data/lib/motorurl/version.rb +3 -0
  19. data/lib/tasks/motorurl_tasks.rake +11 -0
  20. data/spec/controllers/sensors_controller_spec.rb +39 -0
  21. data/spec/dummy/Capfile +28 -0
  22. data/spec/dummy/Procfile +2 -0
  23. data/spec/dummy/README.rdoc +28 -0
  24. data/spec/dummy/Rakefile +6 -0
  25. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  26. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  27. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  28. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  29. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/spec/dummy/bin/bundle +3 -0
  31. data/spec/dummy/bin/rails +4 -0
  32. data/spec/dummy/bin/rake +4 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +24 -0
  35. data/spec/dummy/config/boot.rb +5 -0
  36. data/spec/dummy/config/database.yml +25 -0
  37. data/spec/dummy/config/deploy.rb +40 -0
  38. data/spec/dummy/config/deploy/production.rb +43 -0
  39. data/spec/dummy/config/deploy/staging.rb +43 -0
  40. data/spec/dummy/config/environment.rb +5 -0
  41. data/spec/dummy/config/environments/development.rb +29 -0
  42. data/spec/dummy/config/environments/production.rb +80 -0
  43. data/spec/dummy/config/environments/test.rb +36 -0
  44. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  45. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  46. data/spec/dummy/config/initializers/inflections.rb +16 -0
  47. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  48. data/spec/dummy/config/initializers/motorurl.rb +10 -0
  49. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  50. data/spec/dummy/config/initializers/session_store.rb +3 -0
  51. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  52. data/spec/dummy/config/locales/en.yml +23 -0
  53. data/spec/dummy/config/routes.rb +6 -0
  54. data/spec/dummy/db/development.sqlite3 +0 -0
  55. data/spec/dummy/db/schema.rb +16 -0
  56. data/spec/dummy/db/test.sqlite3 +0 -0
  57. data/spec/dummy/lib/job.rb +19 -0
  58. data/spec/dummy/log/development.log +4698 -0
  59. data/spec/dummy/log/sidekiq.log +16656 -0
  60. data/spec/dummy/public/404.html +58 -0
  61. data/spec/dummy/public/422.html +58 -0
  62. data/spec/dummy/public/500.html +57 -0
  63. data/spec/dummy/public/favicon.ico +0 -0
  64. data/spec/dummy/tmp/pids/server.pid +1 -0
  65. data/spec/lib/registrar_spec.rb +63 -0
  66. data/spec/lib/report_spec.rb +21 -0
  67. data/spec/lib/transmitter_spec.rb +48 -0
  68. data/spec/modules/motorurl_spec.rb +74 -0
  69. data/spec/routing/engine_routes_spec.rb +21 -0
  70. data/spec/spec_helper.rb +52 -0
  71. data/spec/support/passwords.rb +5 -0
  72. metadata +333 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8fa8e072eff182c4023154eb6d06ca55641ba34
4
+ data.tar.gz: a34945c07b2f2a4734c3b165729a5f6fe7e6837b
5
+ SHA512:
6
+ metadata.gz: 32576568c150f493b7970e3505f8c56959570758a4312577f611ed5e9264f9840c33d1b7fc65c47dd6d896e13bae645574c64746cac0e4ef21a78af9d3c39941
7
+ data.tar.gz: 211772320d12cfc748aeea3a5b25ad6f7dad257d8ff4147b7b5823dbca5f22489934a05443e884e5ab8dd705630a70419c90c6e8cf1c5527c15b94ff67b423fe
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = Motorurl
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -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
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
8
+ load 'rails/tasks/engine.rake'
9
+ require 'rspec/core'
10
+ require 'rspec/core/rake_task'
11
+
12
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
13
+
14
+ Bundler::GemHelper.install_tasks
15
+
16
+ desc "Run all specs in spec directory (excluding plugin specs)"
17
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
18
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,12 @@
1
+ module Motorurl
2
+ class ApplicationController < ActionController::Base
3
+ before_filter :validate_password
4
+
5
+ private
6
+
7
+ def validate_password
8
+ render(json: {}, status: :unauthorized) unless
9
+ Motorurl.validate_password(params[:password])
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ module Motorurl
2
+ class SensorsController < ApplicationController
3
+ def create
4
+ Motorurl.process url_params
5
+ render json: {}, status: :ok
6
+ end
7
+
8
+ def show
9
+ render text: 'OK', status: :ok
10
+ end
11
+
12
+ private
13
+
14
+ def url_params
15
+ params.require(:url).permit(:url)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Motorurl
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Motorurl</title>
5
+ <%= stylesheet_link_tag "motorurl/application", media: "all" %>
6
+ <%= javascript_include_tag "motorurl/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ Motorurl::Engine.routes.draw do
2
+ resource :sensor, only: [:create, :show]
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'hashie'
2
+ require 'motorurl/engine'
3
+ require 'motorurl/registrar'
4
+ require 'motorurl/report'
5
+ require 'rotp'
6
+
7
+ module Motorurl
8
+ MissingJobClass = Class.new Exception
9
+
10
+ def self.configuration
11
+ @@configuration ||= Hashie::Mash.new
12
+ end
13
+
14
+ def self.configure
15
+ yield(configuration) if block_given?
16
+ end
17
+
18
+ def self.password_mux
19
+ @@mux ||= ROTP::TOTP.new configuration.shared_secret
20
+ end
21
+
22
+ def self.process(params)
23
+ configuration.sensor_job_class.perform params
24
+ end
25
+
26
+ def self.register
27
+ Register.register
28
+ end
29
+
30
+ def self.report(results)
31
+ Report.send results
32
+ end
33
+
34
+ def self.sensor_job_class
35
+ @@klass ||= begin
36
+ klass = configuration.sensor_job_class
37
+ klass = klass.safe_constantize if klass.is_a?(String)
38
+ klass or raise(MissingJobClass, 'no job class set')
39
+ klass
40
+ end
41
+ end
42
+
43
+ def self.validate_password(candidate)
44
+ password_mux.verify (candidate || 0).to_i
45
+ end
46
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ # When installed as a plugin this is loaded automatically.
4
+ #
5
+ # When installed as a gem, you need to add
6
+ # require 'motorurl/capistrano/recipes'
7
+ # to your deploy.rb
8
+ #
9
+ # The task will run on the primary app server unless no_release is true
10
+ #
11
+ require 'capistrano/version'
12
+
13
+ def command
14
+ env = fetch(:rack_env, nil) || fetch(:rails_env, nil)
15
+ release = fetch :latest_release
16
+ bundle = fetch :bundle_cmd, 'bundle'
17
+ puts "cd #{release} && #{bundle} exec rake motorurl:register RACK_ENV=#{env}"
18
+ end
19
+
20
+ ##
21
+ # Cap version 3
22
+ if defined?(Capistrano::VERSION)
23
+ namespace :motorurl do
24
+ desc "Register the sensor with Spiderman"
25
+ task :register do
26
+ on primary :app do
27
+ command
28
+ end
29
+ end
30
+ end
31
+ else
32
+
33
+ end
@@ -0,0 +1,12 @@
1
+ module Motorurl
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Motorurl
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, :fixture => false
7
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
+ g.assets false
9
+ g.helper false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Motorurl
2
+ class MotorurlRailtie < Rails::Railtie
3
+ rake_tasks do
4
+ load 'tasks/motorurl_tasks.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'transmitter'
2
+
3
+ module Motorurl
4
+ class Registrar < Transmitter
5
+ attr_reader :friendly_name, :price, :callback_url, :canonical_name
6
+
7
+ def self.register(options = {})
8
+ new(options).xmit
9
+ end
10
+
11
+ def initialize(options = {})
12
+ super
13
+ @friendly_name = params.fetch :friendly_name, Motorurl.configuration.friendly_name
14
+ @price = params.fetch :price, (Motorurl.configuration.price || 0)
15
+ @callback_url = params.fetch :callback_url, Motorurl.configuration.callback_url
16
+ @canonical_name = params.fetch :canonical_name, Motorurl.configuration.canonical_name
17
+ end
18
+
19
+ private
20
+
21
+ def body
22
+ {
23
+ friendly_name: friendly_name,
24
+ price: price,
25
+ callback_url: callback_url,
26
+ canonical_name: canonical_name }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'transmitter'
2
+
3
+ module Motorurl
4
+ class Report < Transmitter
5
+ attr_reader :data
6
+
7
+ def self.report(data)
8
+ new(data).xmit
9
+ end
10
+
11
+ def initialize(data)
12
+ super
13
+ @data = data
14
+ end
15
+
16
+ private
17
+
18
+ def body
19
+ { data: data }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ require 'httparty'
2
+
3
+ module Motorurl
4
+ class Transmitter
5
+ include HTTParty
6
+
7
+ debug_output unless Rails.env.production?
8
+
9
+ attr_reader :base_uri, :params, :password, :response, :version
10
+
11
+ def initialize(options = {})
12
+ @params = options.symbolize_keys
13
+ @base_uri = params.fetch :base_uri, Motorurl.configuration.spiderman
14
+ @password = params.fetch :password, Motorurl.password_mux.now
15
+ @version = params.fetch :version, Motorurl.configuration.version
16
+ end
17
+
18
+ def xmit
19
+ @response = self.class.post( '/sensor',
20
+ base_uri: base_uri,
21
+ body: extended_body,
22
+ headers: { 'Content-Type' => 'application/json' },
23
+ query_string_normalizer: ->(query){query.to_json} )
24
+
25
+ self
26
+ end
27
+
28
+ private
29
+
30
+ def body
31
+ {}
32
+ end
33
+
34
+ def extended_body
35
+ body.merge(
36
+ canonical_name: Motorurl.configuration.canonical_name,
37
+ password: password,
38
+ version: version )
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Motorurl
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ namespace :motorurl do
2
+ desc 'Generate a shared secret suitable for use with ROTP'
3
+ task :generate do
4
+ puts ROTP::Base32.random_base32
5
+ end
6
+
7
+ desc 'Register the sensor application'
8
+ task register: :environment do
9
+ Motorurl::Registrar.register
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module Motorurl
4
+ class DummyJob
5
+ def self.perform(params)
6
+ end
7
+ end
8
+
9
+ describe SensorsController do
10
+ routes { Motorurl::Engine.routes }
11
+
12
+ before(:all) do
13
+ Motorurl.configuration.sensor_job_class = DummyJob
14
+ end
15
+
16
+ let!(:password) { generate_one_time_password Motorurl.configuration.shared_secret }
17
+ let!(:params) { { url: { url: 'whatever' }, password: password } }
18
+
19
+ describe 'Assigns' do
20
+ it 'works' do
21
+ post :create, params, format: :json
22
+ expect(response.status).to eq(200)
23
+ end
24
+ end
25
+
26
+ describe 'API Key enforcement' do
27
+ it 'rejects the request if the key does not match' do
28
+ params.merge! password: :fish
29
+ post :create, params, format: :json
30
+ expect(response.status).to eq(401)
31
+ end
32
+
33
+ it 'accepts the request if the key matches' do
34
+ post :create, params, format: :json
35
+ expect(response.status).to eq(200)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ # Load DSL and Setup Up Stages
2
+ require 'capistrano/setup'
3
+
4
+ # Includes default deployment tasks
5
+ require 'capistrano/deploy'
6
+
7
+ # Includes tasks from other gems included in your Gemfile
8
+ #
9
+ # For documentation on these, see for example:
10
+ #
11
+ # https://github.com/capistrano/rvm
12
+ # https://github.com/capistrano/rbenv
13
+ # https://github.com/capistrano/chruby
14
+ # https://github.com/capistrano/bundler
15
+ # https://github.com/capistrano/rails/tree/master/assets
16
+ # https://github.com/capistrano/rails/tree/master/migrations
17
+ #
18
+ # require 'capistrano/rvm'
19
+ # require 'capistrano/rbenv'
20
+ # require 'capistrano/chruby'
21
+ require 'capistrano/bundler'
22
+ require 'capistrano/rails/assets'
23
+ require 'capistrano/rails/migrations'
24
+
25
+ # Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
26
+ Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
27
+
28
+ require 'motorurl/capistrano/recipes'
@@ -0,0 +1,2 @@
1
+ web: bundle exec rails server
2
+ worker: bundle exec sidekiq -L log/sidekiq.log
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.