schema_dot_org 2.2.0 → 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: 39339b796fe48d129c05873ab0a10b6d4ebd683b982723049e25e1bb98b1b893
4
- data.tar.gz: 8e3c46e25e0e00849db2e0817c141f563209fb2ebbf9ec67a10fe8bc8241b9c2
3
+ metadata.gz: e51372213bf0a5e8a9330e21695ef2da7953777627d38df1e5fc3381ea01c446
4
+ data.tar.gz: 9d8faef75d7ef9461d40af04900cbeed15f9afb3f1eab7278e70b49e775d2228
5
5
  SHA512:
6
- metadata.gz: 243792759980ee75f52dbc022aa59bb211b7a250a1af1078e5d167cd1700ae858d1588aafc9fd9a5ab92a7c89166fc437442ab176e9502744fbd6e7ff1bb2cd8
7
- data.tar.gz: 17abef45bb90add2f3cb699720950a2402cc6c84fad5e37672710caf26ddf9b4556204481e418726322642245172b18b8d634e58088a083296a119f168c25799
6
+ metadata.gz: df934ac4daf68337bed44ff56b99763ea350f0d9a09f476f2bae1a9d7231b7b1583696fe0dd1c6c74786e59d55d39ccec9a51807a89f77ad69e6c70208ad6a72
7
+ data.tar.gz: 68a81e34cb06baccf9743fe25ac20f674cf9e2c3972b252f4a80b212fb12b232555288c7dd2e3833d4505cefaefde5e2097ce4ba8a4e46cfaf7d3e301dbf69d5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema_dot_org (2.2.0)
4
+ schema_dot_org (2.2.1)
5
5
  validated_object (~> 2.3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -90,6 +90,12 @@ This type safety comes from the [ValidatedObject gem](https://github.com/dogweat
90
90
 
91
91
  ## Supported Schema.org Types
92
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
+
93
99
  ### WebSite
94
100
 
95
101
  Example with only the required attributes:
@@ -146,19 +152,26 @@ Add this line to your application's Gemfile:
146
152
  gem 'schema_dot_org'
147
153
  ```
148
154
 
149
- And then execute:
150
-
151
- $ bundle
155
+ ## Development
152
156
 
153
- 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`:
154
159
 
155
- $ 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
+ ```
156
169
 
157
- ## Development
170
+ The attributes are from the [Schema.org Product spec](https://schema.org/Product).
158
171
 
159
- 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.
160
174
 
161
- To install this gem onto your local machine, run `bundle exec rake install`.
162
175
 
163
176
  ## Contributing
164
177
 
@@ -14,19 +14,5 @@ module SchemaDotOrg
14
14
  validated_attr :highPrice, type: Numeric, allow_nil: true
15
15
  validated_attr :offerCount, type: String, allow_nil: true
16
16
  validated_attr :offers, type: Array, allow_nil: true
17
-
18
- def _to_json_struct
19
- {
20
- "priceCurrency" => priceCurrency,
21
- "lowPrice" => lowPrice,
22
- "highPrice" => highPrice,
23
- "offerCount" => offerCount,
24
- "offers" => offers.map(&:to_json_struct)
25
- }
26
- end
27
-
28
- def offers
29
- @offers || []
30
- end
31
17
  end
32
18
  end
@@ -3,23 +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
- validated_attr :telephone, type: String, presence: true
10
- validated_attr :contact_type, type: String, presence: true
11
- validated_attr :contact_option, type: String, allow_nil: true
12
11
  validated_attr :area_served, type: Array, allow_nil: true
13
12
  validated_attr :available_language, type: Array, allow_nil: true
14
-
15
- def _to_json_struct
16
- {
17
- 'telephone' => telephone,
18
- 'contactType' => contact_type,
19
- 'contactOption' => contact_option,
20
- 'areaServed' => area_served,
21
- 'availableLanguage' => available_language
22
- }
23
- end
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
24
16
  end
25
17
  end
@@ -2,8 +2,10 @@
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
10
  validated_attr :itemListElement, type: Array, presence: true
9
11
  validated_attr :itemListOrder, type: String, allow_nil: true
@@ -11,20 +13,5 @@ module SchemaDotOrg
11
13
 
12
14
  validated_attr :url, type: String, allow_nil: true
13
15
  validated_attr :image, type: String, allow_nil: true
14
-
15
-
16
- def _to_json_struct
17
- {
18
- 'itemListOrder' => itemListOrder,
19
- 'numberOfItems' => numberOfItems,
20
- 'url' => url,
21
- 'image' => image,
22
- 'itemListElement' => itemListElement.map(&:to_json_struct)
23
- }
24
- end
25
-
26
- def itemListElement
27
- @itemListElement || []
28
- end
29
16
  end
30
17
  end
@@ -7,21 +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
- validated_attr :position, type: Integer, presence: true
11
- validated_attr :url, type: String, allow_nil: true
12
- validated_attr :name, type: String, allow_nil: true
13
- validated_attr :image, type: String, allow_nil: true
14
-
15
- validated_attr :item, type: Product, allow_nil: true
16
-
17
- def _to_json_struct
18
- {
19
- 'position' => position,
20
- 'url' => url,
21
- 'name' => name,
22
- 'image' => image,
23
- 'item' => object_to_json_struct(item)
24
- }
25
- 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
26
15
  end
27
16
  end
@@ -3,22 +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
- validated_attr :priceCurrency, type: String
11
- validated_attr :price, type: Numeric
12
- validated_attr :availability, type: String, allow_nil: true
13
- validated_attr :url, type: String, allow_nil: true
14
-
15
- def _to_json_struct
16
- {
17
- "price" => price,
18
- "priceCurrency" => priceCurrency,
19
- "availability" => availability,
20
- "url" => url
21
- }
22
- 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
23
15
  end
24
16
  end
@@ -9,34 +9,15 @@ require 'schema_dot_org/contact_point'
9
9
 
10
10
  module SchemaDotOrg
11
11
  class Organization < SchemaType
12
+ validated_attr :contact_points, type: Array, allow_nil: true
12
13
  validated_attr :email, type: String, allow_nil: true
13
- validated_attr :telephone, type: String, allow_nil: true
14
14
  validated_attr :founder, type: SchemaDotOrg::Person, allow_nil: true
15
15
  validated_attr :founding_date, type: Date, allow_nil: true
16
16
  validated_attr :founding_location, type: SchemaDotOrg::Place, allow_nil: true
17
17
  validated_attr :logo, type: String
18
18
  validated_attr :name, type: String
19
- validated_attr :url, type: String
20
19
  validated_attr :same_as, type: Array, allow_nil: true
21
- validated_attr :contact_points, type: Array, allow_nil: true
22
-
23
- def _to_json_struct
24
- {
25
- "name" => name,
26
- "email" => email,
27
- "telephone" => telephone,
28
- "url" => url,
29
- "logo" => logo,
30
- "founder" => object_to_json_struct(founder),
31
- "foundingDate" => founding_date.to_s,
32
- "foundingLocation" => object_to_json_struct(founding_location),
33
- "sameAs" => same_as,
34
- "contactPoint" => contact_points.map(&:to_json_struct)
35
- }
36
- end
37
-
38
- def contact_points
39
- @contact_points || []
40
- end
20
+ validated_attr :telephone, type: String, allow_nil: true
21
+ validated_attr :url, type: String
41
22
  end
42
23
  end
@@ -1,20 +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
8
  validated_attr :name, type: String, presence: true
8
9
  validated_attr :same_as, type: Array, allow_nil: true
9
10
  validated_attr :url, type: String, allow_nil: true
10
-
11
-
12
- def _to_json_struct
13
- {
14
- 'name' => name,
15
- 'url' => url,
16
- 'same_as' => same_as
17
- }
18
- end
19
11
  end
20
12
  end
@@ -1,15 +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
8
  validated_attr :address, type: String, presence: true
8
-
9
- def _to_json_struct
10
- {
11
- 'address' => address
12
- }
13
- end
14
9
  end
15
10
  end
@@ -4,28 +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
- validated_attr :name, type: String
12
- validated_attr :url, type: String
13
12
  validated_attr :description, type: String, allow_nil: true
14
13
  validated_attr :image, type: Array, allow_nil: true
14
+ validated_attr :name, type: String
15
15
  validated_attr :offers, type: SchemaDotOrg::AggregateOffer
16
-
17
- def _to_json_struct
18
- {
19
- "name" => name,
20
- "url" => url,
21
- "description" => description,
22
- "image" => image,
23
- "offers" => offers.to_json_struct
24
- }
25
- end
26
-
27
- def image
28
- @image || []
29
- end
16
+ validated_attr :url, type: String
30
17
  end
31
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,17 +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
- validated_attr :target, type: String, presence: true
8
9
  validated_attr :query_input, type: String, presence: true
9
-
10
- def _to_json_struct
11
- {
12
- 'target' => target,
13
- 'query-input' => query_input ## ! Note the hyphen.
14
- }
15
- end
10
+ validated_attr :target, type: String, presence: true
16
11
  end
17
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
9
  validated_attr :name, type: String, presence: true
9
- validated_attr :url, type: String, presence: true
10
10
  validated_attr :potential_action, type: SchemaDotOrg::SearchAction, allow_nil: true
11
-
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
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.2.0'
10
+ spec.version = '2.2.1'
11
11
  spec.authors = ['Robb Shecter']
12
12
  spec.email = ['robb@public.law']
13
13
 
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.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter