rack-refresh 0.0.1

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: 5c406857b2daed8baa5f6bbd7121368a4473169c
4
+ data.tar.gz: c1b16f8e8194320e3d8d2566b26c52de31539357
5
+ SHA512:
6
+ metadata.gz: 585e9fcf29a000bf4bbb5b4784277d2a4284238e8c205de6206f5ed4e21ce0190a98060504f3d1894b98550bb0b0e312504b685de38caa73c2c9cf4e8bcdf1e1
7
+ data.tar.gz: fd8e28c4722a1fe7e5c367af4e6ac8d03e5a6cf2325916db82b3000b160c211d6f03dd98490d6db37981bd0fed8b687a6218b2a68b396d6b5a77ff8409969274
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-refresh.gemspec
4
+ group :test do
5
+ gem 'rack-test', require: "rack/test"
6
+ gem 'rack', require: "rack/builder"
7
+ end
8
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 namusyaka
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # Rack::Refresh
2
+
3
+ Rack Middleware for adding `Refresh` field to response headers.
4
+
5
+ **`Refresh` is supported by modern browsers, but it isn't official HTTP standard.
6
+ So please be careful when using this.**
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'rack-refresh'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rack-refresh
21
+
22
+ ## Usage
23
+
24
+ `Rack::Refresh` is easy to use.
25
+ I would explain how to use it in three sections.
26
+
27
+ ```ruby
28
+ use Rack::Refresh do
29
+ refresh "/", interval: 5, url: "http://www.google.com/"
30
+ refresh do |env|
31
+ env["PATH_INFO"] == "/path" && env["REQUEST_METHOD"].to_s.upcase == "GET"
32
+ end
33
+ refresh %r{/hello.+?} do |env|
34
+ env["PATH_INFO"].start_with?("/hello")
35
+ end
36
+ end
37
+ ```
38
+
39
+ ### with Path
40
+
41
+ The path can be set string and regex.
42
+ The refresh field will be added to response headers if the path matches with `ENV["PATH_INFO"]`
43
+
44
+ ```ruby
45
+ use Rack::Refresh do
46
+ # String
47
+ refresh "/", url: "http://www.google.com/"
48
+ # Regex
49
+ refresh %r{/foo}, url: "http://www.google.com/"
50
+ end
51
+ ```
52
+
53
+ ### with Block
54
+
55
+ The refresh field will be added to response headers if the return value of block is `true`.
56
+
57
+ ```ruby
58
+ use Rack::Refresh do
59
+ refresh do |env|
60
+ env["PATH_INFO"].start_with?("/hello")
61
+ end
62
+ end
63
+ ```
64
+
65
+ ### Configuration
66
+
67
+ `:url` and `:interval` can be set in `refresh` and `use` methods.
68
+ If options are set in both methods, `refresh`'s option will be priority.
69
+
70
+ ```ruby
71
+ use Rack::Refresh, url: "http://www.google.com/", interval: 5 do
72
+ refresh "/" #=> Refresh: 5; url=http://www.google.com/
73
+ refresh "/foo", url: "http://namusyaka.info/", interval: 0 #=> Refresh: 0; url=http://namusyaka.info/
74
+ end
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it ( https://github.com/namusyaka/rack-refresh/fork )
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create a new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run all specs."
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = 'spec/*_spec.rb'
8
+ spec.rspec_opts = "--color"
9
+ end
10
+ task default: :spec
11
+
@@ -0,0 +1 @@
1
+ require 'rack/refresh'
@@ -0,0 +1,47 @@
1
+ require 'rack/refresh/version'
2
+
3
+ module Rack
4
+ class Refresh
5
+ def initialize(app, url: nil, interval: 0, **options, &block)
6
+ @app = app
7
+ @url = url
8
+ @interval = interval
9
+ instance_eval(&block) if block_given?
10
+ end
11
+
12
+ def call(env)
13
+ dup._call(env)
14
+ end
15
+
16
+ def _call(env)
17
+ response = @app.call(env)
18
+ route = find_refresh_route_from(env)
19
+ refresh_for(response, route[:interval], route[:url]) if route
20
+ response
21
+ end
22
+
23
+ def refresh(path = nil, interval: nil, url: nil, &matcher)
24
+ current_route = {interval: interval || @interval, url: url || @url}
25
+ raise ArgumentError, "`:url` must be set in refresh" unless current_route[:url]
26
+ return unless path || block_given?
27
+ current_route[:path] = path
28
+ current_route[:matcher] = matcher
29
+ routes << current_route
30
+ end
31
+
32
+ def refresh_for(response, interval, url)
33
+ response[1]["Refresh"] = "#{interval}; url=#{url}"
34
+ end
35
+
36
+ def find_refresh_route_from(env)
37
+ path_info = env["PATH_INFO"]
38
+ routes.find{|route| route[:path] ? (route[:path] === path_info) : route[:matcher].call(env) }
39
+ end
40
+
41
+ def routes
42
+ @routes ||= []
43
+ end
44
+
45
+ private :find_refresh_route_from, :routes, :refresh_for
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Refresh
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/refresh/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-refresh"
8
+ spec.version = Rack::Refresh::VERSION
9
+ spec.authors = ["namusyaka"]
10
+ spec.email = ["namusyaka@gmail.com"]
11
+ spec.summary = %q{Rack Middleware for adding Refresh field to response headers}
12
+ spec.description = %q{Rack Middleware for adding Refresh field to response headers}
13
+ spec.homepage = "https://github.com/namusyaka/rack-refresh"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Refresh do
4
+ context "with path" do
5
+ before do
6
+ mock_app do
7
+ use Rack::Refresh do
8
+ refresh "/", url: "https://www.google.co.jp/"
9
+ refresh "/some", url: "https://www.google.co.jp/some", interval: 5
10
+ refresh %r{\A/archives/\d+\z}, url: "http://namusyaka.info/"
11
+ end
12
+ run TestApp.new
13
+ end
14
+ end
15
+
16
+ it "can be set path as the condition for refreshing response" do
17
+ get "/"
18
+ expect(last_response.headers["Refresh"]).to eq("0; url=https://www.google.co.jp/")
19
+ get "/some"
20
+ expect(last_response.headers["Refresh"]).to eq("5; url=https://www.google.co.jp/some")
21
+ end
22
+
23
+ it "can be set path as the condition for refreshing response if path is an instance of regexp" do
24
+ get "/archives/1234"
25
+ expect(last_response.headers["Refresh"]).to eq("0; url=http://namusyaka.info/")
26
+ end
27
+
28
+ it "should not refresh response if the path is not matched with env['PATH_INFO']" do
29
+ get "/hello_world"
30
+ expect(last_response.headers["Refresh"]).to be_nil
31
+ end
32
+ end
33
+
34
+ context "with block" do
35
+ before do
36
+ mock_app do
37
+ use Rack::Refresh do
38
+ refresh url: "https://www.google.co.jp/" do |env|
39
+ env["PATH_INFO"].start_with?("/hello")
40
+ end
41
+ end
42
+ run TestApp.new
43
+ end
44
+ end
45
+
46
+ it "can be set block as the condition for refreshing response" do
47
+ get "/hello"
48
+ expect(last_response.headers["Refresh"]).to eq("0; url=https://www.google.co.jp/")
49
+ end
50
+
51
+ it "should not refresh response if the return value of block is false" do
52
+ get "/hell_world"
53
+ expect(last_response.headers["Refresh"]).to be_nil
54
+ end
55
+ end
56
+
57
+ context "with parent options" do
58
+ before do
59
+ mock_app do
60
+ use Rack::Refresh, url: "https://www.google.co.jp/", interval: 5 do
61
+ refresh "/"
62
+ refresh %r{/\d+}, url: "http://namusyaka.info/", interval: 3
63
+ end
64
+ run TestApp.new
65
+ end
66
+ end
67
+
68
+ it "can be set options as the default condition for refreshing response" do
69
+ get "/"
70
+ expect(last_response.headers["Refresh"]).to eq("5; url=https://www.google.co.jp/")
71
+ end
72
+
73
+ it "can override the parent options by setting refresh options" do
74
+ get "/1234"
75
+ expect(last_response.headers["Refresh"]).to eq("3; url=http://namusyaka.info/")
76
+ end
77
+ end
78
+
79
+ describe "exception" do
80
+ it "should raise ArgumentError if :url is not set in refresh method" do
81
+ expect { Rack::Refresh.new{ refresh "/" }}.to raise_error(ArgumentError)
82
+ end
83
+
84
+ it "should not raise ArgumentError if :url is set as the parent options" do
85
+ expect { Rack::Refresh.new(nil, url: "https://www.google.co.jp/"){ refresh "/" }}.to_not raise_error
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path("../../lib/rack/refresh", __FILE__)
2
+ require 'bundler' unless defined?(Bundler)
3
+ Bundler.require(:test)
4
+
5
+ RSpec.configure do |config|
6
+ config.include Rack::Test::Methods
7
+ end
8
+
9
+ def app
10
+ @app
11
+ end
12
+
13
+ def mock_app(&block)
14
+ @app = Rack::Builder.new(&block)
15
+ end
16
+
17
+ class TestApp
18
+ def call(env)
19
+ [200, {"Content-Type" => "text/plain"}, ["test app"]]
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-refresh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - namusyaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ description: Rack Middleware for adding Refresh field to response headers
56
+ email:
57
+ - namusyaka@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/rack-refresh.rb
68
+ - lib/rack/refresh.rb
69
+ - lib/rack/refresh/version.rb
70
+ - rack-refresh.gemspec
71
+ - spec/refresh_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/namusyaka/rack-refresh
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.14
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Rack Middleware for adding Refresh field to response headers
97
+ test_files:
98
+ - spec/refresh_spec.rb
99
+ - spec/spec_helper.rb
100
+ has_rdoc: