schema_dot_org 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c235d549a6a316ac26bf5e055b310c947e18382f89294808f8ebc8e5ce35a4cf
4
- data.tar.gz: 2751fa4daf7f808cfb51bf384cf6aed926cf0c6565fb5f7457a36f5fed02335a
3
+ metadata.gz: 4bd245f102143fc57e4bf985a21e2ae949b84fb167cd8c4f7493a1b7a51a60a9
4
+ data.tar.gz: 56f992158f40aa238b21db445f271c2003d2951487dcbf7ae877b7d960bbe234
5
5
  SHA512:
6
- metadata.gz: 1986324b423f381779fbb2d0696853aa875ff86eb9252c7127cab49236f50100c2d60ba94f1730c4cb1e4e6811b5614d3cb991f2f6e2da87cd355981d9b2d89e
7
- data.tar.gz: c5484937988f31451abac9e3e7b5b73a3843fad82b0a3240d1ba845e40b1abb7512dffd99964d99cf170e2254f134b51190eab2557827a34fb098661374a6716
6
+ metadata.gz: bdf3de02430450ea2d6485f70db9d596f7d106bd8a026976381a5807f61209f947e0c5d451acde42fd0bcbbb2852fbad676b8aa7f2a0d9c4f5a5671b59bc8619
7
+ data.tar.gz: f63ed74bab57639271bcf92f73bd391ba7660a3a59a6d0c55332a72ccbb2b79e406c04d4012a7b547ee3e1a3887bfa9424a44f916360d89d47b88a9219193657
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema_dot_org (0.1.0)
4
+ schema_dot_org (0.2.0)
5
5
  validated_object
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,33 +1,55 @@
1
+ [![CircleCI](https://circleci.com/gh/dogweather/schema-dot-org.svg?style=svg)](https://circleci.com/gh/dogweather/schema-dot-org) [![Gem Version](https://badge.fury.io/rb/schema_dot_org.svg)](https://badge.fury.io/rb/schema_dot_org) [![Maintainability](https://api.codeclimate.com/v1/badges/e0c60b4cbc998563a484/maintainability)](https://codeclimate.com/github/dogweather/schema-dot-org/maintainability)
2
+
1
3
  # SchemaDotOrg
2
4
 
5
+ Let's create [Structured Data](https://developers.google.com/search/docs/guides/intro-structured-data) that's correct,
6
+ every single time.
7
+
8
+ > Google Search works hard to understand the content of a page. You can help us by providing explicit clues about the meaning of a page . . .
3
9
 
4
10
  ## Usage
5
11
 
6
- Create the tree of objects, and then call `#to_s` in (e.g.) a Rails template:
12
+ Create the tree of objects, and then call `#to_s` in e.g., a Rails template:
7
13
 
8
14
  ```ruby
9
15
  require 'schema_dot_org/place'
16
+ require 'schema_dot_org/organization'
10
17
  include SchemaDotOrg
11
18
 
12
- @founding_location = Place.new { |p| p.address = "Las Vegas, NV" }
19
+
20
+ @public_law = Organization.new do |org|
21
+ org.name = "Public.Law"
22
+ org.email = "say_hi@public.law"
23
+ org.url = "https://www.public.law"
24
+ org.founding_location = Place.new do |place|
25
+ place.address = "Portland, OR"
26
+ end
27
+ end
13
28
  ```
14
29
 
15
30
  ```html
16
- <%= @founding_location %>
31
+ <%= @public_law %>
17
32
  ```
18
33
 
19
- This results in the generated webpage containing absolutely correct Schema.org JSON-LD markup:
34
+ The generated webpage will contain correct Schema.org JSON-LD markup:
20
35
 
21
36
  ```html
22
37
  <script type="application/ld+json">
23
38
  {
24
- "@type": "Place",
25
- "address": "Las Vegas, NV"
26
- }
27
- </script>
39
+ "@context": "http://schema.org",
40
+ "@type": "Organization",
41
+ "name": "Public.Law",
42
+ "email": "say_hi@public.law",
43
+ "url": "https://www.public.law",
44
+ "foundingLocation": {
45
+ "@type": "Place",
46
+ "address": "Portland, OR"
47
+ }
28
48
  ```
29
49
 
30
- You cannot create invalid markup: E.g., If you use the wrong type or try to set an unknown attribute, SchemaDotOrg will
50
+ ### You cannot create invalid markup
51
+
52
+ E.g., If you use the wrong type or try to set an unknown attribute, SchemaDotOrg will
31
53
  refuse to create the incorrect JSON-LD. Instead, you'll get a message explaining
32
54
  the problem:
33
55
 
@@ -79,7 +101,7 @@ And it should do it in a **typesafe** way. That is, not merely syntactically cor
79
101
  but also _semantically_ correct. It should, e.g., ensure that only allowed
80
102
  attributes are used.
81
103
 
82
- ## Schema Development Status
104
+ ## Schema Development Roadmap
83
105
 
84
106
  | Type | Planned | Completed |
85
107
  | ---- |:-------:|:---------:|
@@ -90,7 +112,7 @@ attributes are used.
90
112
  | URL | X |
91
113
 
92
114
  The plan is to implement a small subset of types and attributes relevant to the Google web crawler.
93
- Add an Issue to propose a new relevant type.
115
+ Propose new types and attributes by opening an Issue.
94
116
 
95
117
  ## Installation
96
118
 
@@ -0,0 +1,23 @@
1
+ require 'schema_dot_org'
2
+
3
+
4
+ module SchemaDotOrg
5
+ class Organization < SchemaType
6
+ attr_accessor :founding_location, :email, :name, :url
7
+
8
+ validates :email, type: String
9
+ validates :founding_location, type: Place
10
+ validates :name, type: String
11
+ validates :url, type: String
12
+
13
+ def to_json_struct
14
+ {
15
+ "@type" => "Organization",
16
+ "name" => name,
17
+ "email" => email,
18
+ "url" => url,
19
+ "foundingLocation" => founding_location.to_json_struct
20
+ }
21
+ end
22
+ end
23
+ end
@@ -1,4 +1,3 @@
1
- require 'json'
2
1
  require 'schema_dot_org'
3
2
 
4
3
 
@@ -1,3 +1,3 @@
1
1
  module SchemaDotOrg
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  require 'validated_object'
2
3
  require 'schema_dot_org/version'
3
4
 
@@ -6,6 +7,8 @@ module SchemaDotOrg
6
7
  # Base class for schema types. Refactors out common code.
7
8
  #
8
9
  class SchemaType < ValidatedObject::Base
10
+ ROOT_ATTR = {"@context" => "http://schema.org"}
11
+
9
12
 
10
13
  def to_s
11
14
  to_json_ld(pretty: true)
@@ -13,15 +16,17 @@ module SchemaDotOrg
13
16
 
14
17
 
15
18
  def to_json_ld(pretty: false)
16
- "<script type=\"application/ld+json\">\n" + to_json(pretty: pretty) + "\n</script>"
19
+ "<script type=\"application/ld+json\">\n" + to_json(pretty: pretty, as_root: true) + "\n</script>"
17
20
  end
18
21
 
19
22
 
20
- def to_json(pretty: false)
23
+ def to_json(pretty: false, as_root: false)
24
+ structure = as_root ? ROOT_ATTR.merge(to_json_struct) : to_json_struct
25
+
21
26
  if pretty
22
- JSON.pretty_generate(to_json_struct)
27
+ JSON.pretty_generate(structure)
23
28
  else
24
- to_json_struct.to_json
29
+ structure.to_json
25
30
  end
26
31
  end
27
32
 
data/test-script.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'schema_dot_org/place'
2
+ require 'schema_dot_org/organization'
3
+ include SchemaDotOrg
4
+
5
+
6
+ public_law = Organization.new do |org|
7
+ org.name = "Public.Law"
8
+ org.email = "say_hi@public.law"
9
+ org.url = "https://www.public.law"
10
+ org.founding_location = Place.new do |place|
11
+ place.address = "Portland, OR"
12
+ end
13
+ end
14
+
15
+ puts public_law
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_dot_org
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
@@ -85,9 +85,11 @@ files:
85
85
  - bin/console
86
86
  - bin/setup
87
87
  - lib/schema_dot_org.rb
88
+ - lib/schema_dot_org/organization.rb
88
89
  - lib/schema_dot_org/place.rb
89
90
  - lib/schema_dot_org/version.rb
90
91
  - schema_dot_org.gemspec
92
+ - test-script.rb
91
93
  homepage: https://github.com/dogweather/schema-dot-org
92
94
  licenses:
93
95
  - MIT