manage_meta 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +33 -7
- data/Rakefile +2 -8
- data/lib/manage_meta/manage_meta.rb +33 -18
- data/test/manage_meta_test.rb +30 -9
- metadata +18 -29
data/README.markdown
CHANGED
@@ -5,15 +5,24 @@ ManageMeta is yet another meta tag manager for Rails 3.x. Its features are: reco
|
|
5
5
|
should render as HTTP-EQUIV; supports the Google 'canonical' link; is extensible; is non-intrusive
|
6
6
|
|
7
7
|
NOTE: ManageMeta works by `include`ing itself into ActionController::Base via an `initializer`
|
8
|
-
which is enclosed in ManageMeta::Railtie. This works for Rails 3.0.x
|
9
|
-
|
8
|
+
which is enclosed in ManageMeta::Railtie. This works for Rails 3.0.x and 3.1.x. Don't know if it
|
9
|
+
works in Rails 2.x
|
10
|
+
|
11
|
+
What's New in Release 0.0.13?
|
12
|
+
------------
|
13
|
+
|
14
|
+
Support for Facebook Open Graph.
|
15
|
+
|
16
|
+
It's a bit clunky, but it will work and can be used to use ManageMeta as a prerequisite
|
17
|
+
for an Open Graph metadata handling module. I may do that 'one of these days', but until
|
18
|
+
then, see sketch below.
|
10
19
|
|
11
20
|
How to use it
|
12
21
|
-----------
|
13
22
|
|
14
23
|
Include the gem by adding this to your Gemfile file
|
15
24
|
|
16
|
-
`gem "manage_meta", ">=0.0.
|
25
|
+
`gem "manage_meta", ">=0.0.13"`
|
17
26
|
|
18
27
|
or
|
19
28
|
|
@@ -80,9 +89,9 @@ Here are the ugly details
|
|
80
89
|
|
81
90
|
*add_meta()* accepts values in any of three formats:
|
82
91
|
|
83
|
-
* `add_meta(name, value[, :format => :format_name])`
|
84
|
-
* `add_meta(name, value[, :format => :format_name]) do ... end`
|
85
|
-
* `add_meta(name[, :format => :format_name]) do ... end`
|
92
|
+
* `add_meta(name, value[, :format => :format_name] [, :no_capitalize => true/false])`
|
93
|
+
* `add_meta(name, value[, :format => :format_name][, :no_capitalize => true/false]) do ... end`
|
94
|
+
* `add_meta(name[, :format => :format_name][, :no_capitalize => true/false]) do ... end`
|
86
95
|
|
87
96
|
Arguments:
|
88
97
|
|
@@ -90,9 +99,12 @@ Arguments:
|
|
90
99
|
attribute of the meta tag.
|
91
100
|
* *value* must be something which responds to 'to_s' and is not a Hash. Normally it will simply
|
92
101
|
be a string. If given, it supplies the leading part of the *content* attribute of the meta tag
|
93
|
-
* The
|
102
|
+
* The *option* :format must supply an existing key in @manage_meta_format_hash. It is
|
94
103
|
used to associate a meta tag format with *name*. If not given and not currently defined
|
95
104
|
in `@manage_meta_format_hash`, it will be set to *:named*. (see below for details)
|
105
|
+
* The *option* :no_capitalize controls capitalization of each word in the *name* attribute
|
106
|
+
when translated to a String. This was added to deal with Facebook Open Graph *property* tags
|
107
|
+
which are of the form 'og:title' and 'fb:admins', etc. :no_capitalize should be a boolean.
|
96
108
|
* The *optional block* is evaluated and the return value is used as the second [or only] part
|
97
109
|
of the _content_ attribute of the meta tag.
|
98
110
|
|
@@ -166,3 +178,17 @@ Maps meta tag name symbols to meta tag format string symbols. Contains entries
|
|
166
178
|
such as `:content_length => :http_equiv`
|
167
179
|
|
168
180
|
both keys and values are symbols
|
181
|
+
|
182
|
+
A Facebook Open Graph Sketch
|
183
|
+
-----------------------
|
184
|
+
|
185
|
+
First define an appropriate format:
|
186
|
+
|
187
|
+
`add_meta_format :property, '<meta property="#{name}" content="#{content}" char-encoding="utf-8">'`
|
188
|
+
|
189
|
+
Then, add each Open Graph meta tag you need, specifying both :format and :no_capitalize options:
|
190
|
+
|
191
|
+
`add_meta 'og:title', 'This is a Title', :format => :property, :no_capitalize => true`
|
192
|
+
|
193
|
+
You can specify the Open Graph property name as a string (above) or as a symbol `:'og:title'`,
|
194
|
+
but - inasmuch as they contain colons (:) - you need to enclose them in quotes.
|
data/Rakefile
CHANGED
@@ -21,14 +21,8 @@ task :gem do
|
|
21
21
|
system 'gem build manage_meta.gemspec'
|
22
22
|
end
|
23
23
|
|
24
|
-
desc "
|
25
|
-
task :
|
26
|
-
system 'git add .'
|
27
|
-
system 'git commit -m "checkin"'
|
28
|
-
end
|
29
|
-
|
30
|
-
desc "commit changes and tag as #{manage_meta_version}"
|
31
|
-
task :tag => :commit do
|
24
|
+
desc "tag as #{manage_meta_version}"
|
25
|
+
task :tag do
|
32
26
|
system "git tag #{manage_meta_version}"
|
33
27
|
end
|
34
28
|
|
@@ -10,6 +10,7 @@ module ManageMeta
|
|
10
10
|
def _manage_meta_init
|
11
11
|
return if @manage_meta_meta_hash.instance_of? Hash
|
12
12
|
@manage_meta_meta_hash = {}
|
13
|
+
@manage_meta_options = {}
|
13
14
|
|
14
15
|
@manage_meta_format_hash = {
|
15
16
|
:named => '<meta name="#{name}" content="#{content}" char-encoding="utf-8" />',
|
@@ -44,12 +45,13 @@ module ManageMeta
|
|
44
45
|
# Note: if no both 'value' and 'block' are given, then the content of the meta tag is the concatenation
|
45
46
|
# of both values.
|
46
47
|
# options:
|
47
|
-
# :format => symbol - where 'symbol' is one of :named, :http_equiv, :canonical, or a format
|
48
|
+
# :format => symbol - where 'symbol' is one of :named, :http_equiv, :canonical, :property, or a format
|
48
49
|
# added with 'add_meta_format'
|
50
|
+
# :no-capitalize => true or false - default is false
|
49
51
|
# all other options keys are ignored
|
50
52
|
#--
|
51
53
|
def add_meta(name, opt_value = nil, options = {}, &block)
|
52
|
-
_manage_meta_init
|
54
|
+
_manage_meta_init
|
53
55
|
|
54
56
|
# make sure name is a string
|
55
57
|
name = _manage_meta_name_to_sym name
|
@@ -60,11 +62,11 @@ module ManageMeta
|
|
60
62
|
value = opt_value
|
61
63
|
value += yield.to_s if block_given?
|
62
64
|
when opt_value.is_a?(Hash)
|
63
|
-
raise ArgumentError, "Value for meta tag #{name} missing"
|
65
|
+
raise ArgumentError, "Value for meta tag #{name} missing" unless block_given?
|
64
66
|
value = yield.to_s
|
65
67
|
options = opt_value
|
66
68
|
when opt_value.nil?
|
67
|
-
raise ArgumentError, "Value for meta tag #{name} missing"
|
69
|
+
raise ArgumentError, "Value for meta tag #{name} missing" unless block_given?
|
68
70
|
value = yield.to_s
|
69
71
|
when opt_value.respond_to?(:to_s)
|
70
72
|
value = opt_value.to_s
|
@@ -74,16 +76,10 @@ module ManageMeta
|
|
74
76
|
end
|
75
77
|
@manage_meta_meta_hash[name] = value
|
76
78
|
|
77
|
-
|
78
|
-
raise RuntimeError, "add_meta(#{name}, ...): illegal option key(s): #{options.keys - [:format]}"
|
79
|
-
end
|
79
|
+
_manage_meta_set_options name, options
|
80
80
|
|
81
81
|
# if format is explicitly called out or if name is not yet known
|
82
|
-
|
83
|
-
raise RuntimeError, "Unsuported Format: #{options[:format]}: formats are #{@manage_meta_format_hash.keys.join(',')}" \
|
84
|
-
if !@manage_meta_format_hash.has_key?(options[:format].to_sym)
|
85
|
-
@manage_meta_name_to_format[name] = options[:format].to_sym
|
86
|
-
elsif !@manage_meta_name_to_format.has_key?(name)
|
82
|
+
unless @manage_meta_name_to_format.has_key?(name)
|
87
83
|
@manage_meta_name_to_format[name] = :named
|
88
84
|
end
|
89
85
|
end
|
@@ -94,7 +90,7 @@ module ManageMeta
|
|
94
90
|
# if _name_ is in @manage_meta_meta_hash, then it will be deleted
|
95
91
|
#--
|
96
92
|
def del_meta(name)
|
97
|
-
_manage_meta_init
|
93
|
+
_manage_meta_init
|
98
94
|
|
99
95
|
name = _manage_meta_name_to_sym name
|
100
96
|
@manage_meta_meta_hash.delete name if @manage_meta_meta_hash.has_key? name
|
@@ -104,12 +100,13 @@ module ManageMeta
|
|
104
100
|
# add_meta_format(key, format)
|
105
101
|
#
|
106
102
|
# adds the format _format_ to @manage_meta_format_hash using the key _key_
|
103
|
+
# unless it is already defined
|
107
104
|
#--
|
108
105
|
def add_meta_format(key, format)
|
109
|
-
_manage_meta_init
|
106
|
+
_manage_meta_init
|
110
107
|
|
111
|
-
key = key
|
112
|
-
@manage_meta_format_hash[key] = format
|
108
|
+
key = _manage_meta_name_to_sym key
|
109
|
+
@manage_meta_format_hash[key] = format unless @manage_meta_format_hash[key]
|
113
110
|
end
|
114
111
|
|
115
112
|
#++
|
@@ -119,7 +116,7 @@ module ManageMeta
|
|
119
116
|
# using their name-specific formats and indented two spaces.
|
120
117
|
#--
|
121
118
|
def render_meta
|
122
|
-
_manage_meta_init
|
119
|
+
_manage_meta_init
|
123
120
|
|
124
121
|
' ' + @manage_meta_meta_hash.map do |name, content|
|
125
122
|
@manage_meta_format_hash[@manage_meta_name_to_format[name]].sub('#{name}', _manage_meta_sym_to_name(name)).sub('#{content}', content)
|
@@ -128,13 +125,31 @@ module ManageMeta
|
|
128
125
|
|
129
126
|
private
|
130
127
|
def _manage_meta_sym_to_name(sym)
|
131
|
-
|
128
|
+
_manage_meta_init
|
129
|
+
@manage_meta_options[sym] && @manage_meta_options[sym][:no_capitalize] ? sym.to_s.gsub(/[-_]+/, '-') : sym.to_s.split(/[_-]/).map {|x| x.capitalize }.join('-')
|
132
130
|
end
|
133
131
|
|
134
132
|
def _manage_meta_name_to_sym(name)
|
135
133
|
name.to_s.downcase.gsub(/[-_]+/, '_').to_sym
|
136
134
|
end
|
137
135
|
|
136
|
+
def _manage_meta_set_options name, options
|
137
|
+
_manage_meta_init
|
138
|
+
options.keys.each do |option|
|
139
|
+
case option
|
140
|
+
when :format
|
141
|
+
raise RuntimeError, "Unsuported Format: #{options[:format]}: formats are #{@manage_meta_format_hash.keys.join(',')}" \
|
142
|
+
unless @manage_meta_format_hash.has_key? _manage_meta_name_to_sym(options[:format])
|
143
|
+
@manage_meta_name_to_format[name] = _manage_meta_name_to_sym options[:format]
|
144
|
+
when :no_capitalize
|
145
|
+
@manage_meta_options[name] = {} unless @manage_meta_options[name]
|
146
|
+
@manage_meta_options[name][option] = options[option]
|
147
|
+
else
|
148
|
+
raise RuntimeError, "add_meta(#{name}, ...): illegal option key(s): #{options.keys - [:format]}"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
138
153
|
public :add_meta, :del_meta, :add_meta_format, :render_meta
|
139
154
|
private :_manage_meta_init, :_manage_meta_sym_to_name, :_manage_meta_name_to_sym
|
140
155
|
|
data/test/manage_meta_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
$LOAD_PATH << File.expand_path("../../lib", __FILE__)
|
2
2
|
require 'test/unit'
|
3
|
+
require 'pry'
|
3
4
|
require 'manage_meta'
|
4
5
|
|
5
6
|
class NoToS
|
@@ -28,13 +29,13 @@ class ManageMetaTest < Test::Unit::TestCase
|
|
28
29
|
# include ManageMeta after setup
|
29
30
|
include ManageMeta
|
30
31
|
|
31
|
-
def
|
32
|
+
def test_manage_meta_init
|
32
33
|
refute self.instance_variables.map {|x| x.to_sym }.include?(:@manage_meta_meta_hash), "self does not contain @manage_meta_meta_hash"
|
33
34
|
self.send(:_manage_meta_init)
|
34
35
|
assert self.instance_variables.map {|x| x.to_sym }.include?(:@manage_meta_meta_hash), "self contains @manage_meta_meta_hash"
|
35
36
|
end
|
36
37
|
|
37
|
-
def
|
38
|
+
def test_manage_meta_init_is_itempotent
|
38
39
|
self.send(:_manage_meta_init)
|
39
40
|
h = {}
|
40
41
|
self.instance_variables.grep(/manage_meta/).each do |name|
|
@@ -63,17 +64,30 @@ class ManageMetaTest < Test::Unit::TestCase
|
|
63
64
|
assert self.private_methods.grep(/_manage_meta_sym_to_name/), "has private method _manage_meta_sym_to_name"
|
64
65
|
end
|
65
66
|
|
66
|
-
def
|
67
|
+
def test__manage_meta_set_options
|
68
|
+
_manage_meta_init
|
69
|
+
_manage_meta_set_options :foo, :format => :canonical, :no_capitalize => true
|
70
|
+
assert_equal :canonical, @manage_meta_name_to_format[:foo], "format of :foo should be canonical"
|
71
|
+
assert @manage_meta_options[:foo][:no_capitalize], ":foo should have no_capitalize flag set"
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_manage_meta_sym_to_name
|
75
|
+
_manage_meta_init
|
76
|
+
@manage_meta_options[:'og:title'] = {}
|
77
|
+
@manage_meta_options[:'og:title'][:no_capitalize] = true
|
67
78
|
{:foo => 'Foo', :foo_bar => "Foo-Bar", :foo_bar_baz => "Foo-Bar-Baz", "Foo-Bar" => "Foo-Bar",
|
68
|
-
"Foo_bar" => "Foo-Bar" }.each do |key, val|
|
69
|
-
assert_equal _manage_meta_sym_to_name(key),
|
79
|
+
"Foo_bar" => "Foo-Bar", :'og:title' => 'og:title'}.each do |key, val|
|
80
|
+
assert_equal val, _manage_meta_sym_to_name(key), "_manage_meta_sym_to_name(#{key}) == #{val}"
|
70
81
|
end
|
71
82
|
end
|
72
83
|
|
73
|
-
def
|
84
|
+
def test_manage_meta_name_to_sym
|
85
|
+
_manage_meta_init
|
86
|
+
@manage_meta_options[:'og:title'] = {}
|
87
|
+
@manage_meta_options[:'og:title'][:no_capitalize] = true
|
74
88
|
{ 'Foo' => :foo, 'Foo-Bar' => :foo_bar, 'Foo--Bar_Baz' => :foo_bar_baz,
|
75
|
-
:foo_bar_baz => :foo_bar_baz, :foo____bar => :foo_bar }.each do |key, val|
|
76
|
-
assert_equal _manage_meta_name_to_sym(key),
|
89
|
+
:foo_bar_baz => :foo_bar_baz, :foo____bar => :foo_bar, 'og:title' => :'og:title' }.each do |key, val|
|
90
|
+
assert_equal val, _manage_meta_name_to_sym(key), "_manage_meta_name_to_sym(#{key}) == #{val}"
|
77
91
|
end
|
78
92
|
end
|
79
93
|
|
@@ -140,7 +154,7 @@ class ManageMetaTest < Test::Unit::TestCase
|
|
140
154
|
|
141
155
|
# test 'add_meta_format' do
|
142
156
|
def test_add_meta_format_adds_a_format
|
143
|
-
format = '<meta foo-type="#{name}" content="#{content}"'
|
157
|
+
format = '<meta foo-type="#{name}" content="#{content}">'
|
144
158
|
add_meta_format(:foo, format)
|
145
159
|
assert self.instance_variable_get("@manage_meta_format_hash").key?(:foo),
|
146
160
|
"add_meta_format adds key to format_hash using symbol"
|
@@ -168,4 +182,11 @@ class ManageMetaTest < Test::Unit::TestCase
|
|
168
182
|
refute @@helper_args.nil?, "helper_method called with at least one argument"
|
169
183
|
end
|
170
184
|
|
185
|
+
# test open graph metadata
|
186
|
+
def test_adding_og_metadata
|
187
|
+
format = '<meta property="#{name}" content="#{content}">'
|
188
|
+
add_meta_format(:property, format)
|
189
|
+
add_meta "og:title", 'The Grand Octopus', :format => :property, :no_capitalize => true
|
190
|
+
assert_match '<meta property="og:title" content="The Grand Octopus">', render_meta, "og:title metadata should be correct"
|
191
|
+
end
|
171
192
|
end
|
metadata
CHANGED
@@ -1,28 +1,22 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: manage_meta
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.13
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.12
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Mike Howard
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
date: 2011-04-22 00:00:00 -06:00
|
14
|
-
default_executable:
|
12
|
+
date: 2012-01-09 00:00:00.000000000Z
|
15
13
|
dependencies: []
|
16
|
-
|
17
14
|
description: Provides (semi-)intellegent management of meta tags for Rails 3
|
18
15
|
email: mike@clove.com
|
19
16
|
executables: []
|
20
|
-
|
21
17
|
extensions: []
|
22
|
-
|
23
18
|
extra_rdoc_files: []
|
24
|
-
|
25
|
-
files:
|
19
|
+
files:
|
26
20
|
- lib/manage_meta/manage_meta.rb
|
27
21
|
- lib/manage_meta/railtie.rb
|
28
22
|
- lib/manage_meta.rb
|
@@ -30,33 +24,28 @@ files:
|
|
30
24
|
- MIT-LICENSE
|
31
25
|
- Rakefile
|
32
26
|
- README.markdown
|
33
|
-
has_rdoc: true
|
34
27
|
homepage: http://github.com/mikehoward/manage_meta
|
35
28
|
licenses: []
|
36
|
-
|
37
29
|
post_install_message:
|
38
30
|
rdoc_options: []
|
39
|
-
|
40
|
-
require_paths:
|
31
|
+
require_paths:
|
41
32
|
- lib
|
42
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
34
|
none: false
|
44
|
-
requirements:
|
45
|
-
- -
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version:
|
48
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
40
|
none: false
|
50
|
-
requirements:
|
51
|
-
- -
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version:
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
54
45
|
requirements: []
|
55
|
-
|
56
46
|
rubyforge_project:
|
57
|
-
rubygems_version: 1.6
|
47
|
+
rubygems_version: 1.8.6
|
58
48
|
signing_key:
|
59
49
|
specification_version: 3
|
60
50
|
summary: ManageMeta - Yet Another Meta Tag manager for Rails 3
|
61
51
|
test_files: []
|
62
|
-
|