snipp 0.0.4 → 0.0.5

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
@@ -3,7 +3,7 @@
3
3
  [![Coverage Status](https://coveralls.io/repos/yulii/snipp/badge.png?branch=master)](https://coveralls.io/r/yulii/snipp)
4
4
  [![Build Status](https://travis-ci.org/yulii/snipp.png)](https://travis-ci.org/yulii/snipp)
5
5
 
6
- TODO: Write a gem description
6
+ Rich snippets and structured data helpers.
7
7
 
8
8
  ## Installation
9
9
 
@@ -21,6 +21,29 @@ Or install it yourself as:
21
21
  $ gem install snipp
22
22
  ```
23
23
  ## Usage
24
+ Call `item_tag` like `content_tag`
25
+ ```html+ruby
26
+ <%= item_tag :div, scope: true, type: :review do %>
27
+ <%= item_tag :span, 'Snipp', prop: :itemreviewd %>
28
+ Reviewed by <%= item_tag :span, '@yulii', prop: :reviewer %> on
29
+ <%= item_tag :time, 'April 1', prop: :dtreviewd, datetime: "2013-04-01" %>.
30
+ <%= item_tag :span, 'Review summary...', prop: :summary %>
31
+ <%= item_tag :span, 'Review description...', prop: :description %>
32
+ Rating: <%= item_tag :span, '4.5', prop: :rating %>
33
+ <% end %>
34
+ ```
35
+ This would output
36
+ ```html
37
+ <div itemscope itemtype="http://data-vocabulary.org/Review">
38
+ <span itemprop="itemreviewed">Snipp</span>
39
+ Reviewed by <span itemprop="reviewer">@yulii</span> on
40
+ <time itemprop="dtreviewed" datetime="2013-04-01">April 1</time>.
41
+ <span itemprop="summary">Review summary...</span>
42
+ <span itemprop="description">Review description...</span>
43
+ Rating: <span itemprop="rating">4.5</span>
44
+ </div>
45
+ ```
46
+
24
47
  ### Breadcrumbs
25
48
  ```html+ruby
26
49
  <%= breadcrumb [:root, :food, :food_fruit, :food_fruit_red, :food_fruit_red_apple], s: "/" %>
@@ -41,7 +64,7 @@ en:
41
64
  food_fruit_red_apple: "Apple"
42
65
  ```
43
66
 
44
- more https://github.com/yulii/snipp/blob/master/app/views/snipp/index.html.erb
67
+ For more information, read erb files in `app/view/snipp/`
45
68
 
46
69
  ## Contributing
47
70
 
@@ -1,23 +1,23 @@
1
- <h1>Breadcumbs - Rich Snippets Sample</h1>
1
+ <h1>Breadcrumb - Rich Snippets Sample</h1>
2
2
  <h2>Basic</h2>
3
3
  <section id="case-A-1">
4
- <h3>Simple Breadcumb</h3>
5
- <%= breadcrumb [:root, :foods, :fruits] %>
4
+ <h3>Simple Breadcrumb</h3>
5
+ <%= breadcrumb [:index, :foods, :fruits] %>
6
6
  </section>
7
7
  <section id="case-A-2">
8
8
  <h3>Designate separator</h3>
9
- <%= breadcrumb [:root, :foods, :fruits], s: "/" %>
9
+ <%= breadcrumb [:index, :foods, :fruits], s: "/" %>
10
10
  </section>
11
11
  <h2>Advanced</h2>
12
12
  <section id="case-B-1">
13
13
  <h3>Specify Parameters for URL (path) and i18n</h3>
14
- <%= breadcrumb [:root, :foods, :fruits, :fruits_color, :food], s: "/", args: [:color, :name] %>
14
+ <%= breadcrumb [:index, :foods, :fruits, :fruits_color, :food], s: "/", args: [:color, :name] %>
15
15
  </section>
16
16
  <section id="case-B-2">
17
17
  <h3>Customize Variables</h3>
18
- <%= breadcrumb [:root, :foods, :fruits, { path: fruits_color_path('Red'), label: 'Red' }, { path: food_path(color: 'Red', name: 'Apple'), label: 'Apple' }], s: "/" %>
18
+ <%= breadcrumb [:index, :foods, :fruits, { path: fruits_color_path('Red'), label: 'Red' }, { path: food_path(color: 'Red', name: 'Apple'), label: 'Apple' }], s: "/" %>
19
19
  </section>
20
20
  <section id="case-B-3">
21
21
  <h3>Customize Variables and Specify Parameters</h3>
22
- <%= breadcrumb [:root, :foods, :fruits, { path: fruits_color_path, label: :fruits_color }, { path: food_path, label: :food }], s: "/", args: [:color, :name] %>
22
+ <%= breadcrumb [:index, :foods, :fruits, { path: fruits_color_path, label: :fruits_color }, { path: food_path, label: :food }], s: "/", args: [:color, :name] %>
23
23
  </section>
@@ -0,0 +1,6 @@
1
+ <h1>Geo - Rich Snippets Sample</h1>
2
+ <h2>Basic</h2>
3
+ <section id="case-A-1">
4
+ <h3>Simple Geo</h3>
5
+ <%= geo 38.5323, 77.0040 %>
6
+ </section>
@@ -1,5 +1,21 @@
1
- <h1>Rich Snippets Sample</h1>
2
- <h2>Breadcumb</h2>
3
- <% = breadcrumb [:root, :foods, :fruits, :fruits_color, :food], s: "/", params: [:color, :name] %>
1
+ <h1>Index - Rich Snippets Sample</h1>
4
2
 
5
- <% = breadcrumb [:root, :foods, :fruits, { path: fruits_color_path(:red), label: 'Red' }, { path: food_path(color: :red, name: :apple), label: 'Apple' }], s: "/" %>
3
+ <h2>Reviews</h2>
4
+ <h3>ERB code</h3>
5
+ <%= item_tag :div, scope: true, type: :review do %>
6
+ <%= item_tag :span, 'Snipp', prop: :itemreviewd %>
7
+ Reviewed by <%= item_tag :span, '@yulii', prop: :reviewer %> on
8
+ <%= item_tag :time, 'April 1', prop: :dtreviewd, datetime: "2013-04-01" %>.
9
+ <%= item_tag :span, 'Review summary...', prop: :summary %>
10
+ <%= item_tag :span, 'Review description...', prop: :description %>
11
+ Rating: <%= item_tag :span, '4.5', prop: :rating %>
12
+ <% end %>
13
+ <h3>Output code</h3>
14
+ <div itemscope itemtype="http://data-vocabulary.org/Review">
15
+ <span itemprop="itemreviewed">Snipp</span>
16
+ Reviewed by <span itemprop="reviewer">@yulii</span> on
17
+ <time itemprop="dtreviewed" datetime="2013-04-01">April 1</time>.
18
+ <span itemprop="summary">Review summary...</span>
19
+ <span itemprop="description">Review description...</span>
20
+ Rating: <span itemprop="rating">4.5</span>
21
+ </div>
@@ -2,7 +2,7 @@ en:
2
2
  views:
3
3
  breadcrumb:
4
4
  # Sample
5
- root: "Top"
5
+ index: "Top"
6
6
  foods: "Foods"
7
7
  fruits: "Fruits"
8
8
  fruits_color: "%{color}"
@@ -4,7 +4,7 @@ module Snipp
4
4
 
5
5
  def breadcrumb paths, options = {}
6
6
  sepalator = "&nbsp;#{options.delete(:sepalator)||options.delete(:s)||'&gt;'}&nbsp;"
7
- sepalator << '<div itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">'
7
+ sepalator << "<div#{item_options prop: :child, scope: true, type: :breadcrumb}>"
8
8
 
9
9
  # options for i18n
10
10
  i18n_options = { scope: [:views, :breadcrumb] }
@@ -22,7 +22,7 @@ module Snipp
22
22
  bc.push link_to body, send("#{e}_path"), itemprop: :url
23
23
  end
24
24
  end
25
- output = '<nav class="breadcrumb" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">'
25
+ output = "<nav class=\"breadcrumb\"#{item_options scope: true, type: :breadcrumb}>"
26
26
  output << bc.join(sepalator)
27
27
 
28
28
  (paths.size - 1).times do
@@ -33,6 +33,33 @@ module Snipp
33
33
  return output.html_safe
34
34
  end
35
35
 
36
+ def geo latitude, longitude, options = {}
37
+ item_tag :span, prop: :geo, scope: true, type: :geo, class: "geo" do
38
+ "#{content_tag :span, latitude, itemprop: :latitude}#{content_tag :span, longitude, itemprop: :longitude}".html_safe
39
+ end
40
+ end
41
+
42
+ def item_tag name, content_or_options_with_block = nil, options = nil, escape = true, &block
43
+ if block_given?
44
+ options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
45
+ content = capture(&block)
46
+ else
47
+ content = content_or_options_with_block
48
+ end
49
+ item_options = item_options options, escape if options
50
+ tag_options = tag_options options, escape if options
51
+ content = if block_given? then capture(&block) else content_or_options_with_block end
52
+ "<#{name}#{tag_options}#{item_options}>#{escape ? ERB::Util.h(content) : content}</#{name}>".html_safe
53
+ end
54
+
55
+ def item_options options, escape = true
56
+ attrs = ''
57
+ attrs << " itemprop=\"#{escape ? ERB::Util.h(options.delete :prop) : options.delete(:prop)}\"" if options.key? :prop
58
+ attrs << " itemscope" if options.delete :scope
59
+ attrs << " itemtype=\"http://data-vocabulary.org/#{options.delete(:type).to_s.camelize}\"" if options.key? :type
60
+ attrs.html_safe
61
+ end
62
+
36
63
  end
37
64
  end
38
65
  end
data/lib/snipp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Snipp
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/spec/fake_rails.rb CHANGED
@@ -11,8 +11,12 @@ end
11
11
  app.initialize!
12
12
 
13
13
  # routing
14
+ ITEMTYPES = [:index, :breadcrumb, :geo]
14
15
  app.routes.draw do
15
- get "/" =>"snipp#index" ,as: :root
16
+
17
+ ITEMTYPES.each do |e|
18
+ get "/#{e}" => "snipp##{e}", as: e
19
+ end
16
20
 
17
21
  # for Breadcumb
18
22
  get "/foods" => "snipp#breadcrumb", as: :foods
@@ -26,9 +30,10 @@ class ApplicationController < ActionController::Base ; end
26
30
 
27
31
  class SnippController < ApplicationController
28
32
 
29
- def index ; end
30
-
31
- def breadcrumb ; end
33
+ ITEMTYPES.each do |e|
34
+ define_method e do
35
+ end
36
+ end
32
37
 
33
38
  end
34
39
 
@@ -1,89 +1,10 @@
1
1
  # coding: UTF-8
2
2
  require 'spec_helper'
3
3
 
4
- class Snipp::ActionViewExtension::Spec
5
-
6
- BREADCRUMBS = [
7
- { id: "#case-A-1", path: [:root, :foods, :fruits], separator: '>' },
8
- { id: "#case-A-2", path: [:root, :foods, :fruits], separator: '/' },
9
- { id: "#case-B-1", path: [:root, :foods, :fruits, :fruits_color, :food], separator: '/', params: [:color, :name] },
10
- # { id: "#case-B-2", path: [:root, :foods, :fruits, { path: app.fruits_color_path('Red'), label: 'Red' }, { path: app.food_path(color: 'Red', name: 'Apple'), label: 'Apple' }], separator: '/' },
11
- # { id: "#case-B-3", path: [:root, :foods, :fruits, { path: app.fruits_color_path, label: :fruits_color }, { path: app.food_path, label: :food }], separator: '/', params: [:color, :name] },
12
- ]
13
-
14
- end
15
-
16
4
  describe Snipp::ActionViewExtension do
17
5
 
18
6
  before do
19
7
  Snipp::Hooks.init
20
- visit "/foods/fruits/Red/Apple"
21
- end
22
-
23
- # <nav class="breadcrumb" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
24
- # <a href="/" itemprop="url">
25
- # <span itemprop="title">Top</span>
26
- # </a>&nbsp;/&nbsp;
27
- # <div itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
28
- # <a href="/foods" itemprop="url">
29
- # <span itemprop="title">Foods</span>
30
- # </a>&nbsp;/&nbsp;
31
- # <div itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
32
- # <a href="/foods/fruits" itemprop="url">
33
- # <span itemprop="title">Fruits</span>
34
- # </a>&nbsp;/&nbsp;
35
- # <div itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
36
- # <a href="/foods/fruits/Red" itemprop="url">
37
- # <span itemprop="title">Red</span>
38
- # </a>&nbsp;/&nbsp;
39
- # <div itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
40
- # <a href="/foods/fruits/Red/Apple" itemprop="url">
41
- # <span itemprop="title">Apple</span>
42
- # </a>
43
- # </div>
44
- # </div>
45
- # </div>
46
- # </div>
47
- # </nav>
48
-
49
-
50
- let(:itemtype) { "http://data-vocabulary.org/Breadcrumb" }
51
-
52
- describe "breadcrumbs" do
53
-
54
- Snipp::ActionViewExtension::Spec::BREADCRUMBS.each do |e|
55
- context e[:id] do
56
- it "should have a `nav[itemscope]` tag" do
57
- within(e[:id]) { expect(page).to have_selector("nav[itemtype=\"#{itemtype}\"][itemscope]", count: 1) }
58
- end
59
- it "should have correct number of `div[itemscope]` tags" do
60
- within(e[:id]) { expect(page).to have_selector("div[itemtype=\"#{itemtype}\"][itemscope]", count: (e[:path].size - 1)) }
61
- end
62
- it "should have correct number of `itemprop` attribute that equals `child`" do
63
- within(e[:id]) { expect(page).to have_selector('[itemprop="child"][itemscope]', count: (e[:path].size - 1)) }
64
- end
65
- it "should have correct number of `a[itemprop\"url\"]` tags" do
66
- within(e[:id]) { expect(page).to have_selector('a[itemprop="url"]', count: e[:path].size) }
67
- end
68
- it "should have correct number of `span[itemprop\"title\"]` tags" do
69
- within(e[:id]) { expect(page).to have_selector('span[itemprop="title"]', count: e[:path].size) }
70
- end
71
- it "should have correct number of separators" do
72
- within(e[:id]) { expect(page).to have_content(e[:separator], count: (e[:path].size - 1)) }
73
- end
74
- end
75
- end
76
-
77
- describe "a variety of ways to define breadcrumbs" do
78
- let(:html) { page.find("#case-B-1 nav").native.to_s }
79
-
80
- it "should be equivalent" do
81
- expect(page.find("#case-B-2 nav").native.to_s).to eq html
82
- end
83
- it "should be equivalent" do
84
- expect(page.find("#case-B-3 nav").native.to_s).to eq html
85
- end
86
- end
87
8
  end
88
9
 
89
10
  end
@@ -0,0 +1,60 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ class Snipp::Markup::Microdata::Spec
5
+
6
+ BREADCRUMBS = [
7
+ { id: "#case-A-1", path: [:index, :foods, :fruits], separator: '>' },
8
+ { id: "#case-A-2", path: [:index, :foods, :fruits], separator: '/' },
9
+ { id: "#case-B-1", path: [:index, :foods, :fruits, :fruits_color, :food], separator: '/', params: [:color, :name] },
10
+ # { id: "#case-B-2", path: [:index, :foods, :fruits, { path: app.fruits_color_path('Red'), label: 'Red' }, { path: app.food_path(color: 'Red', name: 'Apple'), label: 'Apple' }], separator: '/' },
11
+ # { id: "#case-B-3", path: [:index, :foods, :fruits, { path: app.fruits_color_path, label: :fruits_color }, { path: app.food_path, label: :food }], separator: '/', params: [:color, :name] },
12
+ ]
13
+
14
+ end
15
+
16
+ describe Snipp::Markup::Microdata do
17
+
18
+ before do
19
+ Snipp::Hooks.init
20
+ visit "/foods/fruits/Red/Apple"
21
+ end
22
+
23
+ let(:itemtype) { "http://data-vocabulary.org/Breadcrumb" }
24
+
25
+ describe "breadcrumbs" do
26
+
27
+ Snipp::Markup::Microdata::Spec::BREADCRUMBS.each do |e|
28
+ context e[:id] do
29
+ it "should have a `nav[itemscope]` tag" do
30
+ within(e[:id]) { expect(page).to have_selector("nav[itemtype=\"#{itemtype}\"][itemscope]", count: 1) }
31
+ end
32
+ it "should have correct number of `div[itemscope]` tags" do
33
+ within(e[:id]) { expect(page).to have_selector("div[itemtype=\"#{itemtype}\"][itemscope]", count: (e[:path].size - 1)) }
34
+ end
35
+ it "should have correct number of `itemprop` attribute that equals `child`" do
36
+ within(e[:id]) { expect(page).to have_selector('[itemprop="child"][itemscope]', count: (e[:path].size - 1)) }
37
+ end
38
+ it "should have correct number of separators" do
39
+ within(e[:id]) { expect(page).to have_content(e[:separator], count: (e[:path].size - 1)) }
40
+ end
41
+ ['[itemscope]', 'a[itemprop="url"]', 'span[itemprop="title"]'].each do |s|
42
+ it "should have correct number of `#{s}` tags" do
43
+ within(e[:id]) { expect(page).to have_selector(s, count: e[:path].size) }
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "a variety of ways to define breadcrumbs" do
50
+ let(:html) { page.find("#case-B-1 nav").native.to_s }
51
+
52
+ ["#case-B-2", "#case-B-3"].each do |e|
53
+ it "should be equivalent" do
54
+ expect(page.find("#{e} nav").native.to_s).to eq html
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,44 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ class Snipp::Markup::Microdata::Spec
5
+
6
+ GEO = [
7
+ { id: "#case-A-1", latitude: 38.5323, longitude: 77.0040 },
8
+ ]
9
+
10
+ end
11
+
12
+ describe Snipp::Markup::Microdata do
13
+
14
+ before do
15
+ Snipp::Hooks.init
16
+ visit "/geo"
17
+ end
18
+
19
+ let(:itemtype) { "http://data-vocabulary.org/Geo" }
20
+
21
+ shared_examples_for 'defined the latitude and longitude' do |e|
22
+ it { expect(page).to have_selector('[itemprop="latitude"]', text: e[:latitude], count: 1) }
23
+ it { expect(page).to have_selector('[itemprop="longitude"]', text: e[:longitude], count: 1) }
24
+ end
25
+
26
+ describe "geo" do
27
+
28
+ Snipp::Markup::Microdata::Spec::GEO.each do |e|
29
+ context e[:id] do
30
+ it "should have a `span[itemscope]` tag" do
31
+ within(e[:id]) { expect(page).to have_selector("span[itemtype=\"#{itemtype}\"][itemscope]", count: 1) }
32
+ end
33
+ [:latitude, :longitude].each do |arg|
34
+ it "should have a `itemprop` attribute that equals `#{arg}`" do
35
+ within(e[:id]) { expect(page).to have_selector("[itemprop=\"#{arg}\"]", count: 1) }
36
+ end
37
+ end
38
+ it_should_behave_like 'defined the latitude and longitude', e
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
data/spec/snipp_spec.rb CHANGED
@@ -5,10 +5,21 @@ describe Snipp do
5
5
 
6
6
  before do
7
7
  Snipp::Hooks.init
8
- visit "/foods/fruits/Red/Apple"
9
8
  end
10
9
 
11
- it "should be no error" do
12
- expect(page).to have_content("Breadcumbs - Rich Snippets Sample")
10
+ ITEMTYPES.each do |e|
11
+ describe "no errors" do
12
+ before do
13
+ case e
14
+ when :breadcrumb
15
+ visit "/foods/fruits/Red/Apple"
16
+ else
17
+ visit send("#{e}_path")
18
+ end
19
+ end
20
+ it "should have no errors when visits /#{e}" do
21
+ expect(page).to have_content("#{e.to_s.camelize} - Rich Snippets Sample")
22
+ end
23
+ end
13
24
  end
14
25
  end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,7 @@ RSpec.configure do |config|
12
12
  config.expect_with :rspec do |c|
13
13
  c.syntax = :expect # disables `should`
14
14
  end
15
+ config.include Rails.application.routes.url_helpers
15
16
  config.include Capybara::DSL
16
17
  end
17
18
  Capybara.configure do |config|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snipp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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-04-27 00:00:00.000000000 Z
12
+ date: 2013-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -43,6 +43,7 @@ files:
43
43
  - Rakefile
44
44
  - app/views/layouts/application.html.erb
45
45
  - app/views/snipp/breadcrumb.html.erb
46
+ - app/views/snipp/geo.html.erb
46
47
  - app/views/snipp/index.html.erb
47
48
  - config/locales/snipp.yml
48
49
  - deploy_gem.sh
@@ -57,6 +58,8 @@ files:
57
58
  - snipp.gemspec
58
59
  - spec/fake_rails.rb
59
60
  - spec/snipp/helpers/action_view_extension_spec.rb
61
+ - spec/snipp/markups/microdata/breadcrumb_spec.rb
62
+ - spec/snipp/markups/microdata/geo_spec.rb
60
63
  - spec/snipp_spec.rb
61
64
  - spec/spec_helper.rb
62
65
  homepage: https://github.com/yulii/snipp
@@ -86,5 +89,7 @@ summary: Let search engines understand the content on your website
86
89
  test_files:
87
90
  - spec/fake_rails.rb
88
91
  - spec/snipp/helpers/action_view_extension_spec.rb
92
+ - spec/snipp/markups/microdata/breadcrumb_spec.rb
93
+ - spec/snipp/markups/microdata/geo_spec.rb
89
94
  - spec/snipp_spec.rb
90
95
  - spec/spec_helper.rb