structured-data 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0c7033a829e9a0a17f4d55d068910ff78ddb0d87
4
+ data.tar.gz: 8b62bed97d79ee42de7e6190aa02627c609ff4c0
5
+ SHA512:
6
+ metadata.gz: 5989b843b4fedd18aa5d6fbe07ab2c88f1f4609ba2193f3e6febeff35972efe3299dcff0384838544caec6397530089de7657fc40db13196bf1a6584c3f3d547
7
+ data.tar.gz: fa0c05c53f95e18de9ecc8d4f9770fc66f03cc1eb0de7cf0bb35459a596cbb91e2f9b4b166051f4aa6835e8ef9d339f0f4ab9b3259e4ef999f272cd16a152dce
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in structured_data.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 AKAMATSU Yuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # StructuredData
2
+
3
+ [![CircleCI](https://circleci.com/gh/ukstudio/structured_data/tree/master.svg?style=svg)](https://circleci.com/gh/ukstudio/structured_data/tree/master)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'structured_data'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install structured_data
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ repo = StructuredData::Repository.new
25
+
26
+ breadcrumb = StructuredData::BreadcrumbList.new
27
+ breadcrumb << { url: '/products', name: 'Products' }
28
+ breadcrumb << { url: '/products/book', name: 'Book' }
29
+
30
+ repo << breadcrumb
31
+ repo.dump
32
+ ```
33
+
34
+ ```json
35
+ [{
36
+ "@context": "http://schema.org",
37
+ "@type": "BreadcrumbList",
38
+ "itemListElement": [
39
+ {
40
+ "@type": "ListItem",
41
+ "position": 1,
42
+ "item": {
43
+ "@id": "/products",
44
+ "name": "Products"
45
+ }
46
+ },
47
+ {
48
+ "@type": "ListItem",
49
+ "position": 2,
50
+ "item": {
51
+ "@id": "/products/book",
52
+ "name": "Book"
53
+ }
54
+ }
55
+ ]
56
+ }]
57
+ ```
58
+
59
+ ### Rails support
60
+
61
+ Call the helper method in Controller or View
62
+
63
+ ```slim
64
+ = set_breadcrumb_item(url: '/products', name: 'Products')
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ukstudio/structured_data.
70
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "structured_data"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require 'structured_data'
@@ -0,0 +1,138 @@
1
+ require 'action_controller'
2
+ require 'action_view'
3
+ require 'json'
4
+
5
+ module StructuredData
6
+ module ViewHelper
7
+ def repository
8
+ @structured_data_repository ||= StructuredData::Repository.new
9
+ end
10
+
11
+ def breadcrumb_list
12
+ @breadcrumb_list ||= StructuredData::BreadcrumbList.new
13
+ end
14
+
15
+ def set_breadcrumb_item(url:, name:)
16
+ breadcrumb_list << { url: url, name: name }
17
+ end
18
+
19
+ def site_navigation_element
20
+ @site_navigation_element ||= StructuredData::SiteNavigationElement.new
21
+ end
22
+
23
+ def set_site_navigation_element(url:, name:)
24
+ site_navigation_element << { url: url, name: name }
25
+ end
26
+
27
+ def display_strctured_data
28
+ repository << breadcrumb_list unless breadcrumb_list.empty?
29
+ repository << site_navigation_element unless site_navigation_element.empty?
30
+
31
+ self.content_tag(:script, repository.dump.html_safe, type: 'application/ld+json')
32
+ end
33
+ end
34
+
35
+ module ControllerHelper
36
+ def breadcrumb_list
37
+ @breadcrumb_list ||= StructuredData::BreadcrumbList.new
38
+ end
39
+
40
+ def set_breadcrumb_item(url:, name:)
41
+ breadcrumb_list << { url: url, name: name }
42
+ end
43
+
44
+ def site_navigation_element
45
+ @site_navigation_element ||= StructuredData::SiteNavigationElement.new
46
+ end
47
+
48
+ def set_site_navigation_element(url:, name:)
49
+ site_navigation_element << { url: url, name: name }
50
+ end
51
+ end
52
+
53
+ class Repository
54
+ def initialize
55
+ @items = []
56
+ end
57
+
58
+ def <<(item)
59
+ @items << item
60
+ end
61
+
62
+ def dump
63
+ "[" + @items.map(&:dump).join(',') + "]"
64
+ end
65
+ end
66
+
67
+ class BreadcrumbList
68
+ def initialize
69
+ @items = []
70
+ end
71
+
72
+ def <<(item)
73
+ @items << item
74
+ end
75
+
76
+ def empty?
77
+ @items.empty?
78
+ end
79
+
80
+ def dump
81
+ hash = {
82
+ "@context": "http://schema.org",
83
+ "@type": "BreadcrumbList",
84
+ }
85
+
86
+ hash['itemListElement'] = items_to_hash
87
+ JSON.pretty_generate(hash)
88
+ end
89
+
90
+ private
91
+
92
+ def items_to_hash
93
+ @items.map.with_index do |item, i|
94
+ {
95
+ "@type": "ListItem",
96
+ "position": i + 1,
97
+ "item": {
98
+ "@id": item[:url],
99
+ "name": item[:name]
100
+ }
101
+ }
102
+ end
103
+ end
104
+ end
105
+
106
+ class SiteNavigationElement
107
+ def initialize
108
+ @urls = []
109
+ @names = []
110
+ end
111
+
112
+ def <<(item)
113
+ @urls << item[:url]
114
+ @names << item[:name]
115
+
116
+ item
117
+ end
118
+
119
+ def empty?
120
+ @urls.empty? && @names.empty?
121
+ end
122
+
123
+ def dump
124
+ hash = {
125
+ "@context": "http://schema.org",
126
+ "@type": "SiteNavigationElement",
127
+ }
128
+
129
+ hash[:url] = @urls
130
+ hash[:name] = @names
131
+
132
+ JSON.pretty_generate(hash)
133
+ end
134
+ end
135
+ end
136
+
137
+ ActionView::Base.send :include, StructuredData::ViewHelper
138
+ ActionController::Base.send :include, StructuredData::ControllerHelper
@@ -0,0 +1,3 @@
1
+ module StructuredData
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'structured_data/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "structured-data"
8
+ spec.version = StructuredData::VERSION
9
+ spec.authors = ["AKAMATSU Yuki"]
10
+ spec.email = ["y.akamatsu@ukstudio.jp"]
11
+
12
+ spec.summary = %q{A generator of structured data for SEO}
13
+ spec.description = %q{Generate JSON+LD for Google Structured Data}
14
+ spec.homepage = "https://github.com/ukstudio/structured-data"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "actionpack"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: structured-data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AKAMATSU Yuki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Generate JSON+LD for Google Structured Data
70
+ email:
71
+ - y.akamatsu@ukstudio.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/structured-data.rb
84
+ - lib/structured_data.rb
85
+ - lib/structured_data/version.rb
86
+ - structured_data.gemspec
87
+ homepage: https://github.com/ukstudio/structured-data
88
+ licenses: []
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: A generator of structured data for SEO
110
+ test_files: []