metanol 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -58,6 +58,14 @@ There are three methods for setting values for meta tags:
58
58
  * `og_meta` - sets a value(s) for OpenGraph's meta tags
59
59
  * `wm_meta` - sets a value(s) for Webmaster verification's meta tags
60
60
 
61
+ This plugin also gives you the ability to set the TITLE tag: `meta :title, "Page title"` renders into `<title>Page title</title>`.
62
+
63
+ There are some filters which you can set for methods above:
64
+ * `html` - to get rid of HTML tags in a value (i.e. `meta :title, "Page title", :html`)
65
+ * `overspaces` - to get rid of too many spaces, leave only one space between words
66
+ * `whitespaces` - to get rid of whitespaces in a value (i.e. `meta({title: "Page title", description: 'Page description'}, :html, :whitespaces)`)
67
+ * `clean` - apply `html`, `overspaces` and `whitespaces` filters for a value
68
+
61
69
  Here is the helper's methods for rendering meta tags:
62
70
  * `metanol_tags` - renders all meta tags (Webmaster, OpenGraph and other)
63
71
  * `metanol_og_tags` - renders only OpenGraph's meta tags
@@ -31,23 +31,26 @@ module Metanol
31
31
 
32
32
  def add_meta_tag(type, *args)
33
33
  if args[0].is_a? Hash
34
+ filters = args[1..-1]
34
35
  args[0].each do |name, value|
35
- add_meta_by_type type, name, value
36
+ add_meta_by_type type, name, value, filters
36
37
  end
37
- elsif args.length == 2
38
+ else
38
39
  name = args[0].to_sym
39
- value = args[1].to_sym
40
- add_meta_by_type type, name, value
40
+ value = args[1]
41
+ filters = args[2..-1]
42
+ add_meta_by_type type, name, value, filters
41
43
  end
42
44
  end
43
45
 
44
- def add_meta_by_type(type, name, value)
46
+ def add_meta_by_type(type, name, value, filters=[])
45
47
  data = meta_data(name)[type]
46
48
  key = data[:key]
47
49
  if metanol_options.key? key
48
50
  metanol_options[key].value = value
51
+ metanol_options[key].filters = filters
49
52
  else
50
- metanol_options[key] = data[:type].new(name, value)
53
+ metanol_options[key] = data[:type].new(name, value, filters)
51
54
  end
52
55
  end
53
56
 
@@ -4,21 +4,72 @@ module Metanol::Meta
4
4
  attr_reader :name
5
5
  attr_accessor :value
6
6
 
7
- def initialize(name, value)
7
+ SUPPORTED_FILTERS = [:html, :overspaces, :whitespaces, :clean]
8
+
9
+ def initialize(name, value, filters=[])
8
10
  raise NameError.new "The meta tag '#{name}' isn't supported.", name unless valid?(name)
9
11
  @name = name
10
12
  @value = value
13
+ self.filters = filters
14
+ end
15
+
16
+ def filters=(value)
17
+ @filters = validate_filters(value)
11
18
  end
12
19
 
13
20
  def render
14
21
  raise StandardError.new "Please override this method in a child class"
15
22
  end
16
23
 
24
+ def self.filter_html(text)
25
+ text = text.gsub(/\<br\/?\>/, ' ')
26
+ text.gsub(/\<\/?\w+\/?\>/, '')
27
+ end
28
+
29
+ def self.filter_overspaces(text)
30
+ text.gsub(/[\ ]{2,}/, ' ')
31
+ end
32
+
33
+ def self.filter_whitespaces(text)
34
+ text.gsub(/\s/, ' ')
35
+ end
36
+
17
37
  protected
18
38
 
19
39
  def valid?(name)
20
40
  true
21
41
  end
42
+
43
+ def filtered_value
44
+ result = @value
45
+ return result unless filters?
46
+ result = self.class.filter_html(result) if @filters.include?(:html) || @filters.include?(:clean)
47
+ result = self.class.filter_whitespaces(result) if @filters.include?(:whitespaces) || @filters.include?(:clean)
48
+ result = self.class.filter_overspaces(result) if @filters.include?(:overspaces) || @filters.include?(:clean)
49
+ result
50
+ end
51
+
52
+ def filters?
53
+ @filters && !@filters.empty?
54
+ end
55
+
56
+ private
57
+
58
+ def validate_filters(filters=[])
59
+ result = []
60
+ raise StandardError.new("The filters parameter must be an Array.") unless filters.is_a?(Array)
61
+ begin
62
+ filters.each do |filter|
63
+ filter_value = filter.to_sym
64
+ StandardError.new("Only #{SUPPORTED_FILTERS.join(', ')} filters are supported.") unless SUPPORTED_FILTERS.include? filter_value
65
+ result << filter_value
66
+ end
67
+ rescue NoMethodError
68
+ raise StandardError.new("The filters parameter must includes only string or symbol values.")
69
+ end
70
+ result
71
+ end
72
+
22
73
  end
23
74
 
24
75
  end
@@ -2,7 +2,8 @@ module Metanol::Meta
2
2
 
3
3
  class Main < Base
4
4
  def render
5
- "<meta name=\"#{@name}\" content=\"#{@value}\" />"
5
+ return "<title>#{filtered_value}</title>" if @name == :title
6
+ "<meta name=\"#{@name}\" content=\"#{filtered_value}\" />"
6
7
  end
7
8
  end
8
9
 
@@ -1,21 +1,13 @@
1
1
  module Metanol::Meta
2
2
 
3
3
  class OpenGraph < Base
4
- SUPPORT_TAGS = [:title, :description, :url, :type, :locale, :site_name, :image]
5
-
6
4
  def render
7
- "<meta property=\"og:#{@name}\" content=\"#{@value}\" />"
5
+ "<meta property=\"og:#{@name}\" content=\"#{filtered_value}\" />"
8
6
  end
9
7
 
10
8
  def self.render_current_url(url)
11
9
  "<meta property=\"og:url\" content=\"#{url}\" />"
12
10
  end
13
-
14
- protected
15
-
16
- def valid?(name)
17
- SUPPORT_TAGS.include? name.to_sym
18
- end
19
11
  end
20
12
 
21
13
  end
@@ -9,7 +9,7 @@ module Metanol::Meta
9
9
  }
10
10
 
11
11
  def render
12
- "<meta name=\"#{SUPPORT_TAGS[@name]}\" content=\"#{@value}\" />"
12
+ "<meta name=\"#{SUPPORT_TAGS[@name]}\" content=\"#{filtered_value}\" />"
13
13
  end
14
14
 
15
15
  protected
@@ -1,3 +1,3 @@
1
1
  module Metanol
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -20,7 +20,7 @@ describe TestsController do
20
20
 
21
21
  it { response.should_not have_selector('meta[content="OpenGraph Title"]', property: 'og:title') }
22
22
  it { response.should_not have_selector('meta[content="OpenGraph Description"]', property: 'og:description') }
23
- it { response.should have_selector('meta[content="Users List"]', name: 'title') }
23
+ it { response.should have_selector('title', content: 'Users List') }
24
24
  it { response.should have_selector('meta[content="Description for a users list"]', name: 'description') }
25
25
  end
26
26
 
@@ -40,7 +40,7 @@ describe TestsController do
40
40
 
41
41
  it { response.should have_selector('meta[content="OpenGraph Title"]', property: 'og:title') }
42
42
  it { response.should have_selector('meta[content="OpenGraph Description"]', property: 'og:description') }
43
- it { response.should_not have_selector('meta[content="Users List"]', name: 'title') }
43
+ it { response.should_not have_selector('title', content: 'Users List') }
44
44
  it { response.should_not have_selector('meta[content="Description for a users list"]', name: 'description') }
45
45
  end
46
46
 
@@ -60,7 +60,7 @@ describe TestsController do
60
60
 
61
61
  it { response.should have_selector('meta[content="OpenGraph Title"]', property: 'og:title') }
62
62
  it { response.should have_selector('meta[content="OpenGraph Description"]', property: 'og:description') }
63
- it { response.should have_selector('meta[content="Users List"]', name: 'title') }
63
+ it { response.should have_selector('title', content: 'Users List') }
64
64
  it { response.should have_selector('meta[content="Description for a users list"]', name: 'description') }
65
65
  end
66
66
 
@@ -68,7 +68,7 @@ describe TestsController do
68
68
  controller do
69
69
  def index
70
70
  meta :title, "Users List"
71
- og_meta fake: "OpenGraph Description"
71
+ wm_meta fake: "Fake value"
72
72
  render :inline => <<-ERB
73
73
  <%= metanol_tags %>
74
74
  ERB
@@ -78,4 +78,79 @@ describe TestsController do
78
78
  it { expect { get :index }.to raise_error(NameError, "The meta tag 'fake' isn't supported.") }
79
79
  end
80
80
 
81
+ context "filter whitespaces" do
82
+ controller do
83
+ def index
84
+ meta :description, "Description \t\nfor \ta \tusers \r\nlist", :whitespaces
85
+ render :inline => <<-ERB
86
+ <%= metanol_main_tags %>
87
+ ERB
88
+ end
89
+ end
90
+
91
+ before { get :index }
92
+
93
+ it('success') { response.should have_selector('meta[content="Description for a users list"]', name: 'description') }
94
+ end
95
+
96
+ context "filter HTML tags" do
97
+ controller do
98
+ def index
99
+ meta({description: "<div>Description <br/>for <b>a users</b> <br>list</div>", keywords: "key,word"}, :html)
100
+ render :inline => <<-ERB
101
+ <%= metanol_main_tags %>
102
+ ERB
103
+ end
104
+ end
105
+
106
+ before { get :index }
107
+
108
+ it('success') { response.should have_selector('meta[content="Description for a users list"]', name: 'description') }
109
+ end
110
+
111
+ context "filter HTML tags and whitespaces" do
112
+ controller do
113
+ def index
114
+ meta(:description, "<div>\tDescription \r\n<br/>for \t<b>a users</b> \r\n<br>list</div>", :html, :whitespaces)
115
+ render :inline => <<-ERB
116
+ <%= metanol_main_tags %>
117
+ ERB
118
+ end
119
+ end
120
+
121
+ before { get :index }
122
+
123
+ it('success') { response.should have_selector('meta[content=" Description for a users list"]', name: 'description') }
124
+ end
125
+
126
+ context "filter spaces - leave only 1 space between words" do
127
+ controller do
128
+ def index
129
+ meta :description, "Description for a users list", :overspaces
130
+ render :inline => <<-ERB
131
+ <%= metanol_main_tags %>
132
+ ERB
133
+ end
134
+ end
135
+
136
+ before { get :index }
137
+
138
+ it('success') { response.should have_selector('meta[content="Description for a users list"]', name: 'description') }
139
+ end
140
+
141
+ context "clean up a value from whitespaces, html tags etc (run all filters)" do
142
+ controller do
143
+ def index
144
+ meta(:description, "<div>\tDescription \r\n<br/>for \t<b>a users</b> \r\n<br>list</div>", :clean)
145
+ render :inline => <<-ERB
146
+ <%= metanol_main_tags %>
147
+ ERB
148
+ end
149
+ end
150
+
151
+ before { get :index }
152
+
153
+ it { response.should have_selector('meta[content=" Description for a users list"]', name: 'description') }
154
+ end
155
+
81
156
  end
@@ -1,4 +1,3 @@
1
- #require 'rails/all'
2
1
  require 'action_controller/railtie'
3
2
  require 'action_view/railtie'
4
3
  require 'active_record'
@@ -24,9 +23,8 @@ class ApplicationController < ActionController::Base; end
24
23
 
25
24
  class ParentController < ApplicationController
26
25
  wm_meta :alexa, 'alexa code'
27
- wm_meta :bing, 'bing code'
28
- wm_meta :google, 'google code'
29
26
  wm_meta :yandex, 'yandex code'
27
+ wm_meta bing: 'bing code', google: 'google code'
30
28
  end
31
29
 
32
30
  class HomeController < ParentController
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-17 00:00:00.000000000 Z
12
+ date: 2013-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails