schema_dot_org 2.1 → 2.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 663a21ded2eb0b843caa9f238b4fca6b46705355296b2e68f199e1288e5fe73e
4
- data.tar.gz: 16ae086bf62fd2c3ff5c73bf24a16ec2654b7935e23a334a3b990a51d35d0a58
3
+ metadata.gz: e51372213bf0a5e8a9330e21695ef2da7953777627d38df1e5fc3381ea01c446
4
+ data.tar.gz: 9d8faef75d7ef9461d40af04900cbeed15f9afb3f1eab7278e70b49e775d2228
5
5
  SHA512:
6
- metadata.gz: 8e76b02294c13dab79d20be1851b181c1147f40b24e7a84a1191b1e92e2d08956d977e6bac977e02838d04522bd6ab559d7a31693babe26296d5664dca16e2d5
7
- data.tar.gz: '0748bf804827c3439e018a85b7f0b9741f7d238ab6ccbbeb364bc770ee09131a23cc421eeaa5a3ac5fe18e7ada157c119e5ccdbcbff86ef830471a308240bd5b'
6
+ metadata.gz: df934ac4daf68337bed44ff56b99763ea350f0d9a09f476f2bae1a9d7231b7b1583696fe0dd1c6c74786e59d55d39ccec9a51807a89f77ad69e6c70208ad6a72
7
+ data.tar.gz: 68a81e34cb06baccf9743fe25ac20f674cf9e2c3972b252f4a80b212fb12b232555288c7dd2e3833d4505cefaefde5e2097ce4ba8a4e46cfaf7d3e301dbf69d5
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema_dot_org (2.1)
5
- validated_object (~> 2.2)
4
+ schema_dot_org (2.2.1)
5
+ validated_object (~> 2.3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -45,10 +45,10 @@ GEM
45
45
  rspec-support (~> 3.12.0)
46
46
  rspec-support (3.12.1)
47
47
  ruby2_keywords (0.0.5)
48
- sorbet-runtime (0.5.10993)
48
+ sorbet-runtime (0.5.11072)
49
49
  tzinfo (2.0.6)
50
50
  concurrent-ruby (~> 1.0)
51
- validated_object (2.2.0)
51
+ validated_object (2.3.0)
52
52
  activemodel (>= 3.2.21)
53
53
  sorbet-runtime (>= 0.5.5890)
54
54
 
data/README.md CHANGED
@@ -18,6 +18,7 @@ Let's say you have a Rails app. If you put this in a controller:
18
18
  founding_date: Date.new(2009, 3, 6),
19
19
  founding_location: Place.new(address: 'Portland, OR'),
20
20
  email: 'say_hi@public.law',
21
+ telephone: '+1 123 456 7890',
21
22
  url: 'https://www.public.law',
22
23
  logo: 'https://www.public.law/favicon-196x196.png',
23
24
  same_as: [
@@ -42,6 +43,7 @@ Let's say you have a Rails app. If you put this in a controller:
42
43
  "@type": "Organization",
43
44
  "name": "Public.Law",
44
45
  "email": "say_hi@public.law",
46
+ "telephone": "+1 123 456 7890",
45
47
  "url": "https://www.public.law",
46
48
  "logo": "https://www.public.law/favicon-196x196.png",
47
49
  "foundingDate": "2009-03-06",
@@ -88,6 +90,12 @@ This type safety comes from the [ValidatedObject gem](https://github.com/dogweat
88
90
 
89
91
  ## Supported Schema.org Types
90
92
 
93
+ AggregateOffer, ContactPoint, ItemList, ListItem, Offer, Organization, Person, Place,
94
+ Product, SearchAction, and WebSite.
95
+
96
+ Here are a few example. The source code for these is **extremely easy** to read. Check them out to see
97
+ all the available attributes.
98
+
91
99
  ### WebSite
92
100
 
93
101
  Example with only the required attributes:
@@ -144,19 +152,26 @@ Add this line to your application's Gemfile:
144
152
  gem 'schema_dot_org'
145
153
  ```
146
154
 
147
- And then execute:
148
-
149
- $ bundle
155
+ ## Development
150
156
 
151
- Or install it yourself as:
157
+ The Schema.org classes are as DRY as I could possibly make them. They're really
158
+ easy to create and add to. For example, `Product`:
152
159
 
153
- $ gem install schema_dot_org
160
+ ```ruby
161
+ class Product < SchemaType
162
+ validated_attr :description, type: String, allow_nil: true
163
+ validated_attr :image, type: Array, allow_nil: true
164
+ validated_attr :name, type: String
165
+ validated_attr :offers, type: SchemaDotOrg::AggregateOffer
166
+ validated_attr :url, type: String
167
+ end
168
+ ```
154
169
 
155
- ## Development
170
+ The attributes are from the [Schema.org Product spec](https://schema.org/Product).
156
171
 
157
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
172
+ All Rails validations are available. These are just the attributes we've felt like
173
+ adding. PR's are welcome if you want to add more. Also for more Schema.org types.
158
174
 
159
- To install this gem onto your local machine, run `bundle exec rake install`.
160
175
 
161
176
  ## Contributing
162
177
 
@@ -8,29 +8,11 @@ require 'schema_dot_org/offer'
8
8
  #
9
9
  module SchemaDotOrg
10
10
  class AggregateOffer < SchemaType
11
- attr_accessor :priceCurrency,
12
- :highPrice,
13
- :lowPrice,
14
- :offerCount,
15
- :offers
11
+ attr_reader :priceCurrency
16
12
 
17
- validates :lowPrice, type: Numeric
18
- validates :highPrice, type: Numeric, allow_nil: true
19
- validates :offerCount, type: String, allow_nil: true
20
- validates :offers, type: Array, allow_nil: true
21
-
22
- def _to_json_struct
23
- {
24
- "priceCurrency" => priceCurrency,
25
- "lowPrice" => lowPrice,
26
- "highPrice" => highPrice,
27
- "offerCount" => offerCount,
28
- "offers" => offers.map(&:to_json_struct)
29
- }
30
- end
31
-
32
- def offers
33
- @offers || []
34
- end
13
+ validated_attr :lowPrice, type: Numeric
14
+ validated_attr :highPrice, type: Numeric, allow_nil: true
15
+ validated_attr :offerCount, type: String, allow_nil: true
16
+ validated_attr :offers, type: Array, allow_nil: true
35
17
  end
36
18
  end
@@ -3,29 +3,15 @@
3
3
  require 'schema_dot_org'
4
4
 
5
5
 
6
+ #
7
+ # Model the Schema.org `ContactPoint`. See http://schema.org/ContactPoint
8
+ #
6
9
  module SchemaDotOrg
7
- # Model the Schema.org `ContactPoint`. See http://schema.org/ContactPoint
8
10
  class ContactPoint < SchemaType
9
- attr_accessor :telephone,
10
- :contact_type,
11
- :contact_option,
12
- :area_served,
13
- :available_language
14
-
15
- validates :telephone, type: String, presence: true
16
- validates :contact_type, type: String, presence: true
17
- validates :contact_option, type: String, allow_nil: true
18
- validates :area_served, type: Array, allow_nil: true
19
- validates :available_language, type: Array, allow_nil: true
20
-
21
- def _to_json_struct
22
- {
23
- 'telephone' => telephone,
24
- 'contactType' => contact_type,
25
- 'contactOption' => contact_option,
26
- 'areaServed' => area_served,
27
- 'availableLanguage' => available_language
28
- }
29
- end
11
+ validated_attr :area_served, type: Array, allow_nil: true
12
+ validated_attr :available_language, type: Array, allow_nil: true
13
+ validated_attr :contact_option, type: String, allow_nil: true
14
+ validated_attr :contact_type, type: String, presence: true
15
+ validated_attr :telephone, type: String, presence: true
30
16
  end
31
17
  end
@@ -2,36 +2,16 @@
2
2
 
3
3
  require 'schema_dot_org'
4
4
 
5
+ #
6
+ # Model the Schema.org `ItemList`. See https://schema.org/ItemList
7
+ #
5
8
  module SchemaDotOrg
6
- # Model the Schema.org `ItemList`. See https://schema.org/ItemList
7
9
  class ItemList < SchemaType
8
- attr_accessor :itemListOrder,
9
- :numberOfItems,
10
- :url,
11
- :image,
12
- :itemListElement
10
+ validated_attr :itemListElement, type: Array, presence: true
11
+ validated_attr :itemListOrder, type: String, allow_nil: true
12
+ validated_attr :numberOfItems, type: Integer, allow_nil: true
13
13
 
14
-
15
- validates :itemListElement, type: Array, presence: true
16
- validates :itemListOrder, type: String, allow_nil: true
17
- validates :numberOfItems, type: Integer, allow_nil: true
18
-
19
- validates :url, type: String, allow_nil: true
20
- validates :image, type: String, allow_nil: true
21
-
22
-
23
- def _to_json_struct
24
- {
25
- 'itemListOrder' => itemListOrder,
26
- 'numberOfItems' => numberOfItems,
27
- 'url' => url,
28
- 'image' => image,
29
- 'itemListElement' => itemListElement.map(&:to_json_struct)
30
- }
31
- end
32
-
33
- def itemListElement
34
- @itemListElement || []
35
- end
14
+ validated_attr :url, type: String, allow_nil: true
15
+ validated_attr :image, type: String, allow_nil: true
36
16
  end
37
17
  end
@@ -7,24 +7,10 @@ require 'schema_dot_org/product'
7
7
  module SchemaDotOrg
8
8
  # Model the Schema.org `ItemListElement`. See https://schema.org/ItemListElement
9
9
  class ListItem < SchemaType
10
- attr_accessor :position, :item,
11
- :url, :name, :image
12
-
13
- validates :position, type: Integer, presence: true
14
- validates :url, type: String, allow_nil: true
15
- validates :name, type: String, allow_nil: true
16
- validates :image, type: String, allow_nil: true
17
-
18
- validates :item, type: Product, allow_nil: true
19
-
20
- def _to_json_struct
21
- {
22
- 'position' => position,
23
- 'url' => url,
24
- 'name' => name,
25
- 'image' => image,
26
- 'item' => object_to_json_struct(item)
27
- }
28
- end
10
+ validated_attr :image, type: String, allow_nil: true
11
+ validated_attr :item, type: Product, allow_nil: true
12
+ validated_attr :name, type: String, allow_nil: true
13
+ validated_attr :position, type: Integer, presence: true
14
+ validated_attr :url, type: String, allow_nil: true
29
15
  end
30
16
  end
@@ -3,27 +3,14 @@
3
3
  require 'date'
4
4
  require 'schema_dot_org'
5
5
 
6
+ #
6
7
  # Model the Schema.org `Thing > Place`. See https://schema.org/Offer
7
8
  #
8
9
  module SchemaDotOrg
9
10
  class Offer < SchemaType
10
- attr_accessor :priceCurrency,
11
- :price,
12
- :availability,
13
- :url
14
-
15
- validates :priceCurrency, type: String
16
- validates :price, type: Numeric
17
- validates :availability, type: String, allow_nil: true
18
- validates :url, type: String, allow_nil: true
19
-
20
- def _to_json_struct
21
- {
22
- "price" => price,
23
- "priceCurrency" => priceCurrency,
24
- "availability" => availability,
25
- "url" => url
26
- }
27
- end
11
+ validated_attr :priceCurrency, type: String
12
+ validated_attr :price, type: Numeric
13
+ validated_attr :availability, type: String, allow_nil: true
14
+ validated_attr :url, type: String, allow_nil: true
28
15
  end
29
16
  end
@@ -9,45 +9,15 @@ require 'schema_dot_org/contact_point'
9
9
 
10
10
  module SchemaDotOrg
11
11
  class Organization < SchemaType
12
- attr_accessor :email,
13
- :telephone,
14
- :founder,
15
- :founding_date,
16
- :founding_location,
17
- :logo,
18
- :name,
19
- :url,
20
- :same_as,
21
- :contact_points
22
-
23
- validates :email, type: String, allow_nil: true
24
- validates :telephone, type: String, allow_nil: true
25
- validates :founder, type: SchemaDotOrg::Person, allow_nil: true
26
- validates :founding_date, type: Date, allow_nil: true
27
- validates :founding_location, type: SchemaDotOrg::Place, allow_nil: true
28
- validates :logo, type: String
29
- validates :name, type: String
30
- validates :url, type: String
31
- validates :same_as, type: Array, allow_nil: true
32
- validates :contact_points, type: Array, allow_nil: true
33
-
34
- def _to_json_struct
35
- {
36
- "name" => name,
37
- "email" => email,
38
- "telephone" => telephone,
39
- "url" => url,
40
- "logo" => logo,
41
- "founder" => object_to_json_struct(founder),
42
- "foundingDate" => founding_date.to_s,
43
- "foundingLocation" => object_to_json_struct(founding_location),
44
- "sameAs" => same_as,
45
- "contactPoint" => contact_points.map(&:to_json_struct)
46
- }
47
- end
48
-
49
- def contact_points
50
- @contact_points || []
51
- end
12
+ validated_attr :contact_points, type: Array, allow_nil: true
13
+ validated_attr :email, type: String, allow_nil: true
14
+ validated_attr :founder, type: SchemaDotOrg::Person, allow_nil: true
15
+ validated_attr :founding_date, type: Date, allow_nil: true
16
+ validated_attr :founding_location, type: SchemaDotOrg::Place, allow_nil: true
17
+ validated_attr :logo, type: String
18
+ validated_attr :name, type: String
19
+ validated_attr :same_as, type: Array, allow_nil: true
20
+ validated_attr :telephone, type: String, allow_nil: true
21
+ validated_attr :url, type: String
52
22
  end
53
23
  end
@@ -1,23 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
-
3
+ #
4
+ # Model the Schema.org **Person**. See http://schema.org/Person
5
+ #
4
6
  module SchemaDotOrg
5
- # Model the Schema.org `Person`. See http://schema.org/Person
6
7
  class Person < SchemaType
7
- attr_accessor :name,
8
- :url,
9
- :same_as
10
-
11
- validates :name, type: String, presence: true
12
- validates :url, type: String, allow_nil: true
13
- validates :same_as, type: Array, allow_nil: true
14
-
15
- def _to_json_struct
16
- {
17
- 'name' => name,
18
- 'url' => url,
19
- 'same_as' => same_as
20
- }
21
- end
8
+ validated_attr :name, type: String, presence: true
9
+ validated_attr :same_as, type: Array, allow_nil: true
10
+ validated_attr :url, type: String, allow_nil: true
22
11
  end
23
12
  end
@@ -1,16 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
-
3
+ #
4
+ # Model the Schema.org `Thing > Place`. See http://schema.org/Place
5
+ #
4
6
  module SchemaDotOrg
5
- # Model the Schema.org `Thing > Place`. See http://schema.org/Place
6
7
  class Place < SchemaType
7
- attr_accessor :address
8
- validates :address, type: String, presence: true
9
-
10
- def _to_json_struct
11
- {
12
- 'address' => address
13
- }
14
- end
8
+ validated_attr :address, type: String, presence: true
15
9
  end
16
10
  end
@@ -4,34 +4,15 @@ require 'date'
4
4
  require 'schema_dot_org'
5
5
  require 'schema_dot_org/aggregate_offer'
6
6
 
7
+ #
7
8
  # Model the Schema.org `Thing > Place`. See https://schema.org/Product
8
9
  #
9
10
  module SchemaDotOrg
10
11
  class Product < SchemaType
11
- attr_accessor :name,
12
- :url,
13
- :description,
14
- :image,
15
- :offers
16
-
17
- validates :name, type: String
18
- validates :url, type: String
19
- validates :description, type: String, allow_nil: true
20
- validates :image, type: Array, allow_nil: true
21
- validates :offers, type: SchemaDotOrg::AggregateOffer
22
-
23
- def _to_json_struct
24
- {
25
- "name" => name,
26
- "url" => url,
27
- "description" => description,
28
- "image" => image,
29
- "offers" => offers.to_json_struct
30
- }
31
- end
32
-
33
- def image
34
- @image || []
35
- end
12
+ validated_attr :description, type: String, allow_nil: true
13
+ validated_attr :image, type: Array, allow_nil: true
14
+ validated_attr :name, type: String
15
+ validated_attr :offers, type: SchemaDotOrg::AggregateOffer
16
+ validated_attr :url, type: String
36
17
  end
37
18
  end
@@ -43,7 +43,7 @@ module SchemaDotOrg
43
43
 
44
44
 
45
45
  def _to_json_struct
46
- raise "For subclasses to implement"
46
+ attrs_and_values
47
47
  end
48
48
 
49
49
 
@@ -53,12 +53,55 @@ module SchemaDotOrg
53
53
  Regexp.last_match(1)
54
54
  end
55
55
 
56
+
56
57
  def object_to_json_struct(object)
57
58
  return nil unless object
58
59
  object.to_json_struct
59
60
  end
60
61
 
61
- private
62
+
63
+ def attrs_and_values
64
+ attrs.map do |attr|
65
+ # Clean up and andle the `query-input` attribute, which
66
+ # doesn't follow the normal camelCase convention.
67
+ attr_name = snake_case_to_lower_camel_case(attr.to_s.delete_prefix('@')).sub('queryInput', 'query-input')
68
+ attr_value = instance_variable_get(attr)
69
+
70
+ [attr_name, resolve_value(attr_value)]
71
+ end.to_h
72
+ end
73
+
74
+
75
+ def resolve_value(value)
76
+ if value.is_a?(Array)
77
+ value.map { |v| resolve_value(v) }
78
+
79
+ elsif value.is_a?(Date)
80
+ value.to_s
81
+
82
+ elsif is_schema_type?(value)
83
+ value.to_json_struct
84
+
85
+ else
86
+ value
87
+ end
88
+ end
89
+
90
+
91
+ def snake_case_to_lower_camel_case(snake_case)
92
+ snake_case.to_s.split('_').map.with_index { |s, i| i.zero? ? s : s.capitalize }.join
93
+ end
94
+
95
+
96
+ def attrs
97
+ instance_variables.reject{ |v| [:@validation_context, :@errors].include?(v) }
98
+ end
99
+
100
+
101
+ def is_schema_type?(object)
102
+ object.class.module_parent == SchemaDotOrg
103
+ end
104
+
62
105
 
63
106
  def rails_production?
64
107
  defined?(Rails) && Rails.env.production?
@@ -1,18 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
 
4
+ #
5
+ # Model the Schema.org `Thing > SearchAction`. See http://schema.org/SearchAction
6
+ #
4
7
  module SchemaDotOrg
5
- # Model the Schema.org `Thing > SearchAction`. See http://schema.org/SearchAction
6
8
  class SearchAction < SchemaType
7
- attr_accessor :target, :query_input
8
- validates :target, type: String, presence: true
9
- validates :query_input, type: String, presence: true
10
-
11
- def _to_json_struct
12
- {
13
- 'target' => target,
14
- 'query-input' => query_input ## ! Note the hyphen.
15
- }
16
- end
9
+ validated_attr :query_input, type: String, presence: true
10
+ validated_attr :target, type: String, presence: true
17
11
  end
18
12
  end
@@ -1,21 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
-
3
+ #
4
+ # Model the Schema.org `Thing > CreativeWork > WebSite`.
5
+ # @See http://schema.org/WebSite
6
+ #
4
7
  module SchemaDotOrg
5
- # Model the Schema.org `Thing > CreativeWork > WebSite`.
6
- # @See http://schema.org/WebSite
7
8
  class WebSite < SchemaType
8
- attr_accessor :name, :url, :potential_action
9
- validates :name, type: String, presence: true
10
- validates :url, type: String, presence: true
11
- validates :potential_action, type: SchemaDotOrg::SearchAction, allow_nil: true
12
-
13
- def _to_json_struct
14
- {
15
- 'name' => self.name,
16
- 'url' => self.url,
17
- 'potentialAction' => object_to_json_struct(potential_action)
18
- }
19
- end
9
+ validated_attr :name, type: String, presence: true
10
+ validated_attr :potential_action, type: SchemaDotOrg::SearchAction, allow_nil: true
11
+ validated_attr :url, type: String, presence: true
20
12
  end
21
13
  end
@@ -7,7 +7,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
7
7
  Gem::Specification.new do |spec|
8
8
  spec.required_ruby_version = '>= 2.6'
9
9
  spec.name = 'schema_dot_org'
10
- spec.version = '2.1'
10
+ spec.version = '2.2.1'
11
11
  spec.authors = ['Robb Shecter']
12
12
  spec.email = ['robb@public.law']
13
13
 
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ['lib']
27
27
 
28
- spec.add_dependency 'validated_object', '~> 2.2'
28
+ spec.add_dependency 'validated_object', '~> 2.3'
29
29
 
30
30
  spec.add_development_dependency "bundler", "~> 2.4"
31
31
  spec.add_development_dependency "rake", "~> 13.0"
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: '2.1'
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.2'
19
+ version: '2.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.2'
26
+ version: '2.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement