rack-metadata 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/Gemfile +10 -0
- data/LICENSE +20 -0
- data/README.md +30 -0
- data/Rakefile +18 -0
- data/lib/rack/metadata.rb +35 -0
- data/lib/rack-metadata.rb +1 -0
- data/rack-metadata.gemspec +18 -0
- data/test/metadata_test.rb +66 -0
- data/test/test_helper.rb +2 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0771f809c465c5e1fe652ee6719f7f4bd4e6b7ff
|
4
|
+
data.tar.gz: 123ef81e8ab84f6a76f51d8853660bb964848c29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7cf501d143e7ead98b4a42b4990b6c47adc18cf1d0d4f95cd5ac83cb4427541e0bd4e658bb5a6992d0c39a79a66f5d227c70fb02d3191ec7fcd649d92ff6b7a9
|
7
|
+
data.tar.gz: 6372585c026f693a07e67567271092dbb3bea4994f27cb577f5e469bc1685cbe17d5bc39445cbe1010de31f241186e28a22db4d47ac83444ecdb8f1fa9f9ab96
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Jeff Ching
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# rack-metadata [![Build Status](https://travis-ci.org/chingor13/rack-metadata.png)](https://travis-ci.org/chingor13/rack-metadata)
|
2
|
+
|
3
|
+
Inject meta tags into your head. This rack middleware collects meta data about a page and will inject meta tags into the head of your html content.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```
|
8
|
+
# in your rack up/config file
|
9
|
+
use Rack::Metadata
|
10
|
+
|
11
|
+
# in your application
|
12
|
+
env['rack.metadata'] = {
|
13
|
+
description: "My page's meta description",
|
14
|
+
keywords: "Foo, bar"
|
15
|
+
}
|
16
|
+
```
|
17
|
+
|
18
|
+
It the request is `text/html`, the middleware will add meta tags for description and keywords:
|
19
|
+
|
20
|
+
```
|
21
|
+
<head>
|
22
|
+
…
|
23
|
+
<meta name="description" content="My page's meta description" />
|
24
|
+
<meta name="keywords" content="Foo, bar" />
|
25
|
+
…
|
26
|
+
</head>
|
27
|
+
```
|
28
|
+
|
29
|
+
All other content types are currently ignored.
|
30
|
+
|
data/Rakefile
ADDED
@@ -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
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'rake/testtask'
|
10
|
+
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.libs << 'test'
|
14
|
+
t.pattern = 'test/**/*_test.rb'
|
15
|
+
t.verbose = false
|
16
|
+
end
|
17
|
+
|
18
|
+
task default: :test
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Rack
|
2
|
+
class Metadata
|
3
|
+
|
4
|
+
def initialize(app, options = {})
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
env['rack.metadata'] = {}
|
10
|
+
status, headers, response = @app.call(env)
|
11
|
+
|
12
|
+
if headers['Content-Type'].to_s.include?('text/html') # Only update HTML bodies
|
13
|
+
if env['rack.metadata']
|
14
|
+
body = ""
|
15
|
+
response.each { |part| body << part }
|
16
|
+
index = body.rindex("</head>")
|
17
|
+
if index
|
18
|
+
body.insert(index, env['rack.metadata'].map{|k,v| message(k,v)}.join(""))
|
19
|
+
headers["Content-Length"] = body.length.to_s
|
20
|
+
response = [body]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
[status, headers, response]
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def message(name, content)
|
31
|
+
%{<meta name="#{name}" content="#{content}" />}
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack/metadata'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "rack-metadata"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.description = "Add metadata tags to the header"
|
5
|
+
s.summary = "Add metadata tags to the html header"
|
6
|
+
s.add_dependency "rack"
|
7
|
+
s.add_development_dependency "rack-test"
|
8
|
+
s.add_development_dependency "nokogiri"
|
9
|
+
|
10
|
+
s.author = "Jeff Ching"
|
11
|
+
s.email = "jeff@chingr.com"
|
12
|
+
s.homepage = "http://github.com/chingor13/rack-metadata"
|
13
|
+
s.license = "MIT"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
class MetadataTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def test_embeds_meta_at_end_of_html_head
|
7
|
+
response = request({
|
8
|
+
description: "My page's meta description",
|
9
|
+
keywords: "Foo, bar"
|
10
|
+
})
|
11
|
+
doc = Nokogiri::HTML(response.body)
|
12
|
+
metas = doc.search('html head meta')
|
13
|
+
assert_equal(2, metas.count)
|
14
|
+
|
15
|
+
description = metas.first
|
16
|
+
assert_equal("description", description['name'])
|
17
|
+
assert_equal("My page's meta description", description['content'])
|
18
|
+
|
19
|
+
keywords = metas.last
|
20
|
+
assert_equal("keywords", keywords['name'])
|
21
|
+
assert_equal("Foo, bar", keywords['content'])
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_does_nothing_on_non_html
|
25
|
+
response = request({
|
26
|
+
description: "My page's meta description",
|
27
|
+
keywords: "Foo, bar"
|
28
|
+
}, {content_type: 'text/javascript'})
|
29
|
+
assert_equal(HTML_DOC, response.body)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_does_nothing_when_no_head_tag
|
33
|
+
response = request({
|
34
|
+
description: "My page's meta description",
|
35
|
+
keywords: "Foo, bar"
|
36
|
+
}, {body: ["Some text"]})
|
37
|
+
assert_equal("Some text", response.body)
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
HTML_DOC = <<-EOF
|
43
|
+
<html>
|
44
|
+
<head>
|
45
|
+
<title>Rack::Metadata</title>
|
46
|
+
</head>
|
47
|
+
<body>
|
48
|
+
<h1>Rack::Metadata</h1>
|
49
|
+
</body>
|
50
|
+
</html>
|
51
|
+
EOF
|
52
|
+
|
53
|
+
def request(metadata = {}, opts = {})
|
54
|
+
body = opts.delete(:body) || [HTML_DOC]
|
55
|
+
content_type = opts.delete(:content_type) || "text/html"
|
56
|
+
app = lambda { |env|
|
57
|
+
env['rack.metadata'] = metadata
|
58
|
+
[200, {'Content-Type' => content_type}, body]
|
59
|
+
}
|
60
|
+
@application = Rack::Metadata.new(app, opts)
|
61
|
+
@request = Rack::MockRequest.new(@application).get("/")
|
62
|
+
yield(@application, @request) if block_given?
|
63
|
+
@request
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-metadata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Ching
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-21 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: rack-test
|
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: nokogiri
|
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: Add metadata tags to the header
|
56
|
+
email: jeff@chingr.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- LICENSE
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- lib/rack-metadata.rb
|
67
|
+
- lib/rack/metadata.rb
|
68
|
+
- rack-metadata.gemspec
|
69
|
+
- test/metadata_test.rb
|
70
|
+
- test/test_helper.rb
|
71
|
+
homepage: http://github.com/chingor13/rack-metadata
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.0.3
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Add metadata tags to the html header
|
95
|
+
test_files:
|
96
|
+
- test/metadata_test.rb
|
97
|
+
- test/test_helper.rb
|