katgut 0.0.1

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.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +107 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/katgut/application.js +13 -0
  6. data/app/assets/stylesheets/katgut/application.css +15 -0
  7. data/app/controllers/katgut/application_controller.rb +5 -0
  8. data/app/controllers/katgut/rules_controller.rb +21 -0
  9. data/app/helpers/katgut/application_helper.rb +4 -0
  10. data/app/models/katgut/rule.rb +57 -0
  11. data/app/views/layouts/katgut/application.html.erb +14 -0
  12. data/config/routes.rb +3 -0
  13. data/db/migrate/20160704065128_create_katgut_rules.rb +13 -0
  14. data/lib/generators/katgut/initializer/USAGE +9 -0
  15. data/lib/generators/katgut/initializer/initializer_generator.rb +9 -0
  16. data/lib/generators/katgut/initializer/templates/initializer.rb +4 -0
  17. data/lib/katgut.rb +12 -0
  18. data/lib/katgut/config.rb +11 -0
  19. data/lib/katgut/engine.rb +14 -0
  20. data/lib/katgut/version.rb +3 -0
  21. data/lib/tasks/katgut_tasks.rake +4 -0
  22. data/public/katgut_success.html +14 -0
  23. data/spec/controllers/rules_controller_spec.rb +35 -0
  24. data/spec/dummy/README.rdoc +28 -0
  25. data/spec/dummy/Rakefile +6 -0
  26. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  27. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  28. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  30. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  31. data/spec/dummy/bin/bundle +3 -0
  32. data/spec/dummy/bin/rails +4 -0
  33. data/spec/dummy/bin/rake +4 -0
  34. data/spec/dummy/bin/setup +29 -0
  35. data/spec/dummy/config.ru +4 -0
  36. data/spec/dummy/config/application.rb +26 -0
  37. data/spec/dummy/config/boot.rb +5 -0
  38. data/spec/dummy/config/database.yml +25 -0
  39. data/spec/dummy/config/environment.rb +5 -0
  40. data/spec/dummy/config/environments/development.rb +41 -0
  41. data/spec/dummy/config/environments/production.rb +79 -0
  42. data/spec/dummy/config/environments/test.rb +42 -0
  43. data/spec/dummy/config/initializers/assets.rb +11 -0
  44. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  45. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  46. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  47. data/spec/dummy/config/initializers/inflections.rb +16 -0
  48. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  49. data/spec/dummy/config/initializers/session_store.rb +3 -0
  50. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  51. data/spec/dummy/config/locales/en.yml +23 -0
  52. data/spec/dummy/config/routes.rb +4 -0
  53. data/spec/dummy/config/secrets.yml +22 -0
  54. data/spec/dummy/db/development.sqlite3 +0 -0
  55. data/spec/dummy/db/schema.rb +26 -0
  56. data/spec/dummy/db/test.sqlite3 +0 -0
  57. data/spec/dummy/log/development.log +753 -0
  58. data/spec/dummy/log/test.log +6697 -0
  59. data/spec/dummy/public/404.html +67 -0
  60. data/spec/dummy/public/422.html +67 -0
  61. data/spec/dummy/public/500.html +66 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/dummy/tmp/pids/server.pid +1 -0
  64. data/spec/factories/rules.rb +7 -0
  65. data/spec/integration/navigation_test.rb +8 -0
  66. data/spec/katgut_test.rb +7 -0
  67. data/spec/models/katgut/rule_spec.rb +67 -0
  68. data/spec/rails_helper.rb +30 -0
  69. data/spec/requests/rules_spec.rb +50 -0
  70. data/spec/spec_helper.rb +12 -0
  71. data/spec/support/factory_girl.rb +3 -0
  72. data/spec/test_helper.rb +21 -0
  73. metadata +241 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 042b13c3474e8a85d7bfa2d7491b8c6232ea4ffa
4
+ data.tar.gz: a4ccdcfe656a435c6c5c8be9f84a20a20da0ce1d
5
+ SHA512:
6
+ metadata.gz: b52ff06cb0d08a8131affd2d5d1f9a4ae1af0326206980f295d7fbb7e0181dbb92da3dec5a7273600142e994f7b5922852677ffa8c2369e8bd356ab99f89b721
7
+ data.tar.gz: 3e55f17958e6709406a3fb35ffc254d96286919ce4dc9754d27062ab8a98c954219dee7e9ef756d1a6b99bfbe16e5503cc64956cd578f9681053858e9eea72f4
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 HAMADA Kazuki
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.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # Katgut
2
+ Katgut is a Rails Engine for adding simple url redirection features to your apps.
3
+
4
+ The gem provides:
5
+ * Dynamic redirection rules on DB
6
+ * After redirection callbacks
7
+
8
+ # Requirements
9
+ * Ruby 2.3 or later
10
+ * Rails 4.2.6 or later
11
+
12
+ # Installation
13
+ Add `katgut` to your Gemfile
14
+
15
+ ```ruby
16
+ gem 'katgut', '~> 0.0.1'
17
+ ```
18
+
19
+ Then install the migration files by running:
20
+
21
+ ```bash
22
+ $ rake katgut:install:migrations
23
+ ```
24
+
25
+ And apply them to your database.
26
+
27
+ ```bash
28
+ $ rake db:migrate
29
+ ```
30
+
31
+ Katgut is implemented as an Rails Engine. You can mount it anywhere you like:
32
+
33
+ ```ruby
34
+ # config/routes.rb
35
+
36
+ Rails.application.routes.draw do
37
+ mount Katgut::Engine => "/katgut" # /katgut/xxxxxx will be the redirection path
38
+ end
39
+ ```
40
+
41
+ # Usage
42
+ ## Defining the redirection rules
43
+ The redirection rules will be stored in `katgut_rules` table. A rule is a set of following parameters:
44
+
45
+ Example:
46
+
47
+ ```ruby
48
+ # Define a rule...
49
+ Katgut::Rule.new(source: "go-to-google", destination: "https://www.google.co.jp/").save
50
+
51
+ # ... to create this redirection
52
+ # GET /katgut/go-to-google => GET https://www.google.co.jp/
53
+ ```
54
+
55
+ You can set these 3 attributes to each redirection rule.
56
+
57
+ * source
58
+ * destination
59
+ * active
60
+
61
+ ### `source`
62
+ Set a unique key to determine which rule should be used to redirect the request.
63
+ Only word characters and hyphens\(`[^a-zA-Z0-9_\-]`) are allowed and at least 5 characters needed.
64
+
65
+ ```
66
+ /mount_point/:source
67
+ ```
68
+
69
+ ### `destination`
70
+ Set the desination url.
71
+
72
+ * You can omit `http://` scheme.
73
+ * Only `http` and `https` schemes are allowed.
74
+ * Urls begin with `/` will be assumed as a same-origin path.
75
+
76
+ ### `active`
77
+ Set `false` to ignore the rule.
78
+
79
+ ## Customization
80
+ You can customize the behavour of the gem through the initializer script.
81
+
82
+ ```bash
83
+ $ rails g katgut:initializer
84
+ # => config/initializers/katgut.rb
85
+ ```
86
+
87
+ ### Fallback path
88
+ You can set a alternative destination url for the case the given source url does not exist.
89
+
90
+ ```ruby
91
+ Katgut.configure do |config|
92
+ config.fall_back_path = '/not_found.html' # default: '/'
93
+ end
94
+ ```
95
+
96
+ ### Custom codes after redirection
97
+ You can define a custom block that will be called after every redirection.
98
+ The block should have 3 argumets: `request`, `parameters` and `rule`.
99
+ The `rule` will be `nil` when the specified source url on the request was not valid.
100
+
101
+ ```ruby
102
+ Katgut.configure do |config|
103
+ config.after_refirection_callback = do |request, parameters, rule|
104
+ # add custom behaviour here, such as logging, overwriting the rule, etc...
105
+ end
106
+ end
107
+ ```
data/Rakefile ADDED
@@ -0,0 +1,34 @@
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
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Katgut'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ module Katgut
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ module Katgut
2
+ class RulesController < ApplicationController
3
+ def show
4
+ @rule = Rule.active.find_by!(source: params[:id])
5
+ redirect_to @rule.regular_destination
6
+ rescue => e
7
+ redirect_to Katgut.config.fall_back_path
8
+ ensure
9
+ after_redirection_callback
10
+ end
11
+
12
+ private
13
+
14
+ def after_redirection_callback
15
+ cb = Katgut.config.after_redirection_callback
16
+ return unless cb
17
+
18
+ instance_exec(request, params, @rule, &cb)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module Katgut
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,57 @@
1
+ module Katgut
2
+ class Rule < ActiveRecord::Base
3
+ ALLOWED_SCHEMES = [:https, :http]
4
+ DEFAULT_SCHEME = :http
5
+ MINIMUM_SOURCE_LENGTH = 5
6
+
7
+ scope :active, -> { where(active: true) }
8
+
9
+ validates :source,
10
+ uniqueness: true,
11
+ presence: true,
12
+ format: { with: /\A[a-zA-Z0-9\-_]*\z/ },
13
+ length: { in: MINIMUM_SOURCE_LENGTH .. 255 }
14
+ validates :destination,
15
+ presence: true,
16
+ format: { without: URI::UNSAFE },
17
+ length: { maximum: 255 }
18
+ validate :ensure_destination_has_no_unallowed_scheme
19
+
20
+ def regular_destination
21
+ if URI.extract(destination).present?
22
+ destination
23
+ else
24
+ case destination
25
+ when /\A\//
26
+ destination
27
+ else
28
+ "#{self.class::DEFAULT_SCHEME}://#{destination}"
29
+ end
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ class << self
36
+ def allowed_schemes_with_suffix
37
+ ALLOWED_SCHEMES.map {|s| "#{s}://" }
38
+ end
39
+
40
+ def unallowed_scheme_in?(url)
41
+ if url.present?
42
+ !URI.extract(url).all? do |str|
43
+ ALLOWED_SCHEMES.include? URI.split(str)[0].to_sym
44
+ end
45
+ else
46
+ false
47
+ end
48
+ end
49
+ end
50
+
51
+ def ensure_destination_has_no_unallowed_scheme
52
+ if self.class.unallowed_scheme_in?(destination)
53
+ errors.add(:destination, "has unallowed scheme.")
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Katgut</title>
5
+ <%= stylesheet_link_tag "katgut/application", media: "all" %>
6
+ <%= javascript_include_tag "katgut/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Katgut::Engine.routes.draw do
2
+ get '/:id', to: 'rules#show'
3
+ end
@@ -0,0 +1,13 @@
1
+ class CreateKatgutRules < ActiveRecord::Migration
2
+ def change
3
+ create_table :katgut_rules do |t|
4
+ t.string :source, null: false
5
+ t.string :destination, null: false
6
+ t.boolean :active, null: false, default: false
7
+
8
+ t.timestamps null: false
9
+ end
10
+
11
+ add_index :katgut_rules, :source, unique: true, name: 'idx_katgut_source'
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generates a boilerplate of config file for Katgut.
3
+ You can modify the generated file to change the behaviour of the engine.
4
+
5
+ Example:
6
+ rails generate katgut:initializer
7
+
8
+ This will create:
9
+ config/initializers/katgut.rb
@@ -0,0 +1,9 @@
1
+ module Katgut
2
+ class InitializerGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def copy_initializer_file
6
+ copy_file "initializer.rb", "config/initializers/katgut.rb"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ Katgut.configure do |config|
2
+ config.after_redirection_callback = nil
3
+ config.fall_back_path = ''
4
+ end if defined?(Katgut)
data/lib/katgut.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "katgut/engine"
2
+ require "katgut/config"
3
+
4
+ module Katgut
5
+ def self.config
6
+ @config ||= Config.new
7
+ end
8
+
9
+ def self.configure
10
+ yield config if block_given?
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Katgut
2
+ class Config
3
+ include ActiveSupport::Configurable
4
+ config_accessor :after_redirection_callback, :fall_back_path
5
+
6
+ configure do |config|
7
+ config.after_redirection_callback = nil
8
+ config.fall_back_path = '/'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module Katgut
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Katgut
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, fixture: false
7
+ g.fixture_replacement :factory_girl, dir: "spec/factories"
8
+ end
9
+
10
+ initializer "static assets" do |app|
11
+ app.middleware.insert_before(::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Katgut
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :katgut do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
5
+ <meta charset="utf-8">
6
+ <title>Katgut works perfectly!</title>
7
+ <meta name="description" content="Katgut test page">
8
+ <meta name="author" content="hello">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+ </head>
11
+ <body>
12
+ It works!
13
+ </body>
14
+ </html>
@@ -0,0 +1,35 @@
1
+ require 'rails_helper'
2
+
3
+ module Katgut
4
+ describe RulesController do
5
+ routes { Engine.routes }
6
+
7
+ describe "#after_redirection_callback" do
8
+ let!(:rule) { create(:rule, destination: "/advice") }
9
+ let!(:callback) { -> (request, params, rule) { throw :called } }
10
+
11
+ context "after redirection callback is not set in config" do
12
+ it "doesn't calls the after redirection callback" do
13
+ expect {
14
+ get :show, id: 'advice'
15
+ }.not_to throw_symbol :called
16
+ end
17
+ end
18
+
19
+ context "after redirection callback is set" do
20
+ before do
21
+ allow(Katgut.config).to(
22
+ receive(:after_redirection_callback)
23
+ .and_return(callback)
24
+ )
25
+ end
26
+
27
+ it "calls the after redirection callback" do
28
+ expect {
29
+ get :show, id: 'advice'
30
+ }.to throw_symbol :called
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end