heroku_header 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/heroku_header.gemspec +22 -0
- data/lib/heroku_header/middle_ware.rb +79 -0
- data/lib/heroku_header/version.rb +3 -0
- data/lib/heroku_header.rb +6 -0
- data/spec/middle_ware_spec.rb +83 -0
- data/spec/spec_helper.rb +22 -0
- metadata +130 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ben McRedmond
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# HerokuHeader
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'heroku_header'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install heroku_header
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/heroku_header/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ben McRedmond"]
|
6
|
+
gem.email = ["ben@benmcredmond.com"]
|
7
|
+
gem.description = %q{A piece of Rack middleware that inserts the Heroku header on a particular subdomain and caches it.}
|
8
|
+
gem.summary = gem.description
|
9
|
+
gem.homepage = "http://github.com/intercom/heroku_header"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "heroku_header"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = HerokuHeader::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency('rspec')
|
19
|
+
gem.add_development_dependency('rack-test')
|
20
|
+
gem.add_development_dependency('pry')
|
21
|
+
gem.add_development_dependency('sinatra')
|
22
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module HerokuHeader
|
2
|
+
class MiddleWare
|
3
|
+
|
4
|
+
HEADER_LOCATION = URI.parse("http://nav.heroku.com/v1/providers/header")
|
5
|
+
|
6
|
+
def initialize(app, options = {})
|
7
|
+
@app = app
|
8
|
+
@subdomain = options[:subdomain] || 'heroku'
|
9
|
+
@timeout = options[:timeout] || 5
|
10
|
+
@cache = options[:cache] # An ActiveSupport::Cache::Store instance
|
11
|
+
@cache_key = options[:cache_key] || 'heroku_header_html'
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
@env = env
|
16
|
+
@status, @headers, @response = @app.call(@env)
|
17
|
+
|
18
|
+
[@status, @headers, self]
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
return @response.each(&block) if !html_response? || !heroku_subdomain?
|
23
|
+
|
24
|
+
@response.each do |resp|
|
25
|
+
yield resp.gsub(%r{<body[^<]*>}, '\\0' + heroku_header.to_s)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def html_response?
|
31
|
+
@headers['Content-Type'].include?('text/html')
|
32
|
+
end
|
33
|
+
|
34
|
+
def heroku_subdomain?
|
35
|
+
@env['HTTP_HOST'] && (!!@env['HTTP_HOST'].match(/#{@subdomain}\..+/))
|
36
|
+
end
|
37
|
+
|
38
|
+
def heroku_header
|
39
|
+
Timeout::timeout(@timeout) do
|
40
|
+
retry_block :on => [Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError] do
|
41
|
+
cache_fetch_or_http_get_heroku_header
|
42
|
+
end
|
43
|
+
end
|
44
|
+
rescue Timeout::Error
|
45
|
+
""
|
46
|
+
end
|
47
|
+
|
48
|
+
def retry_block(options = {})
|
49
|
+
options[:max_attempts] ||= 5
|
50
|
+
|
51
|
+
attempts = 0
|
52
|
+
|
53
|
+
begin
|
54
|
+
return yield
|
55
|
+
rescue *(options[:on])
|
56
|
+
attempts += 1
|
57
|
+
sleep(0.5) and retry if attempts <= options[:max_attempts]
|
58
|
+
end
|
59
|
+
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
|
63
|
+
def cache_fetch_or_http_get_heroku_header
|
64
|
+
cache(@cache_key, (60 * 60 * 24 * 7)) do
|
65
|
+
response = Net::HTTP.get_response(HEADER_LOCATION)
|
66
|
+
raise unless response.code == '200'
|
67
|
+
raise unless response.body
|
68
|
+
|
69
|
+
response.body
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def cache(key, ttl, &block)
|
74
|
+
return yield if !@cache || !@cache.call.respond_to?(:fetch)
|
75
|
+
@cache.call.fetch(key, :expires_in => ttl, &block)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HerokuHeader::MiddleWare do
|
4
|
+
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
after :each do
|
8
|
+
HerokuHeader::MiddleWare.any_instance.rspec_reset
|
9
|
+
HerokuHeader::MiddleWare.rspec_reset
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
context "heroku_subdomain?" do
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
@heroku_header = HerokuHeader::MiddleWare.new(nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return true when the subdomain is == @subdomain" do
|
20
|
+
@heroku_header.instance_variable_set(:@env, {'HTTP_HOST' => 'heroku.intercom.io'})
|
21
|
+
@heroku_header.send(:heroku_subdomain?).should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return false when the subdomain is != @subdomain" do
|
25
|
+
@heroku_header.instance_variable_set(:@env, {'HTTP_HOST' => 'blah.intercom.io'})
|
26
|
+
@heroku_header.send(:heroku_subdomain?).should be_false
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return false when HTTP_HOST is nil" do
|
30
|
+
@heroku_header.instance_variable_set(:@env, {'HTTP_HOST' => nil})
|
31
|
+
@heroku_header.send(:heroku_subdomain?).should be_false
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return false when HTTP_HOST is ''" do
|
35
|
+
@heroku_header.instance_variable_set(:@env, {'HTTP_HOST' => ''})
|
36
|
+
@heroku_header.send(:heroku_subdomain?).should be_false
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "http_response?" do
|
42
|
+
|
43
|
+
before :each do
|
44
|
+
@heroku_header = HerokuHeader::MiddleWare.new(nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return true if Content-Type is text/html" do
|
48
|
+
@heroku_header.instance_variable_set(:@headers, {'Content-Type' => 'text/html'})
|
49
|
+
@heroku_header.send(:html_response?).should be_true
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return false if Content-Type is not text/html" do
|
53
|
+
@heroku_header.instance_variable_set(:@headers, {'Content-Type' => 'text/xml'})
|
54
|
+
@heroku_header.send(:html_response?).should be_false
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
context "inserting the header" do
|
60
|
+
|
61
|
+
before :each do
|
62
|
+
HerokuHeader::MiddleWare.any_instance.stub(:cache_fetch_or_http_get_heroku_header) {
|
63
|
+
'HEROKU HEADER'
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
def app
|
68
|
+
make_app { use HerokuHeader::MiddleWare }
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should not insert anything if we are not on a heroku subdomain" do
|
72
|
+
get 'http://intercom.io/'
|
73
|
+
last_response.body.should == TestApp::DEFAULT_BODY
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should insert the header if we are on a heroku subdomain" do
|
77
|
+
get 'http://heroku.intercom.io/'
|
78
|
+
last_response.body.should == "<html><body>HEROKU HEADERHello world</body></html>"
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'heroku_header'
|
3
|
+
require 'rack/test'
|
4
|
+
require 'sinatra/base'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
# Inspired by Heroku's heroku-nav specs
|
8
|
+
class TestApp < Sinatra::Base
|
9
|
+
|
10
|
+
DEFAULT_BODY = "<html><body>Hello world</body></html>"
|
11
|
+
|
12
|
+
get '/' do
|
13
|
+
params[:body] || DEFAULT_BODY
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def make_app(&block)
|
19
|
+
handler = Class.new(TestApp)
|
20
|
+
handler.class_eval(&block)
|
21
|
+
handler
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heroku_header
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben McRedmond
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rack-test
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sinatra
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: A piece of Rack middleware that inserts the Heroku header on a particular
|
79
|
+
subdomain and caches it.
|
80
|
+
email:
|
81
|
+
- ben@benmcredmond.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- heroku_header.gemspec
|
92
|
+
- lib/heroku_header.rb
|
93
|
+
- lib/heroku_header/middle_ware.rb
|
94
|
+
- lib/heroku_header/version.rb
|
95
|
+
- spec/middle_ware_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
homepage: http://github.com/intercom/heroku_header
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: 1013909249883803082
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: 1013909249883803082
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.23
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: A piece of Rack middleware that inserts the Heroku header on a particular
|
127
|
+
subdomain and caches it.
|
128
|
+
test_files:
|
129
|
+
- spec/middle_ware_spec.rb
|
130
|
+
- spec/spec_helper.rb
|