schema_dot_org 2.2.1 → 2.2.2

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: e51372213bf0a5e8a9330e21695ef2da7953777627d38df1e5fc3381ea01c446
4
- data.tar.gz: 9d8faef75d7ef9461d40af04900cbeed15f9afb3f1eab7278e70b49e775d2228
3
+ metadata.gz: 146e2c23c546ada2f45452f78d5e609e0730256b24b72811c834306ef69b5409
4
+ data.tar.gz: c1dbd6ee1c3af15b07b3188eea8decf036e3bc52c97379d78bea1f7ca16a25ea
5
5
  SHA512:
6
- metadata.gz: df934ac4daf68337bed44ff56b99763ea350f0d9a09f476f2bae1a9d7231b7b1583696fe0dd1c6c74786e59d55d39ccec9a51807a89f77ad69e6c70208ad6a72
7
- data.tar.gz: 68a81e34cb06baccf9743fe25ac20f674cf9e2c3972b252f4a80b212fb12b232555288c7dd2e3833d4505cefaefde5e2097ce4ba8a4e46cfaf7d3e301dbf69d5
6
+ metadata.gz: ea5016b6e3075a5aa21d82faf8a7e00b42f920637b6e8dd97c7a54998eae6fcfb8ae4f98a15a6e45cd471b1614598d0f007f54f67545043b63af89ff1ec85fff
7
+ data.tar.gz: ae7aaf1fbcea2b10918d224c89c26fa71725cf6dac52d27f5960a7096726ca7d3b9c1866477651084d20e71fe118049de3fa4b646fbc26c0da671db2d064b168
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema_dot_org (2.2.1)
4
+ schema_dot_org (2.2.2)
5
5
  validated_object (~> 2.3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -93,7 +93,7 @@ This type safety comes from the [ValidatedObject gem](https://github.com/dogweat
93
93
  AggregateOffer, ContactPoint, ItemList, ListItem, Offer, Organization, Person, Place,
94
94
  Product, SearchAction, and WebSite.
95
95
 
96
- Here are a few example. The source code for these is **extremely easy** to read. Check them out to see
96
+ Here are a few examples. The [source code](https://github.com/dogweather/schema-dot-org/tree/master/lib/schema_dot_org) for these is **extremely easy** to read. Check them out to see
97
97
  all the available attributes.
98
98
 
99
99
  ### WebSite
@@ -1,9 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'date'
4
- require 'schema_dot_org'
5
- require 'schema_dot_org/offer'
6
-
3
+ #
7
4
  # Model the Schema.org `Thing > Place`. See https://schema.org/Offer
8
5
  #
9
6
  module SchemaDotOrg
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'schema_dot_org'
4
-
5
3
 
6
4
  #
7
5
  # Model the Schema.org `ContactPoint`. See http://schema.org/ContactPoint
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'schema_dot_org'
4
3
 
5
4
  #
6
5
  # Model the Schema.org `ItemList`. See https://schema.org/ItemList
@@ -1,16 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'schema_dot_org'
4
- require 'schema_dot_org/product'
5
3
 
4
+ require_relative 'product'
6
5
 
6
+ #
7
+ # Model the Schema.org `ItemListElement`. See https://schema.org/ItemListElement
8
+ #
7
9
  module SchemaDotOrg
8
- # Model the Schema.org `ItemListElement`. See https://schema.org/ItemListElement
9
10
  class ListItem < SchemaType
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
11
+ validated_attr :image, type: String, allow_nil: true
12
+ validated_attr :item, type: SchemaDotOrg::Product, allow_nil: true
13
+ validated_attr :name, type: String, allow_nil: true
14
+ validated_attr :position, type: Integer, presence: true
15
+ validated_attr :url, type: String, allow_nil: true
15
16
  end
16
17
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'date'
4
- require 'schema_dot_org'
5
3
 
6
4
  #
7
5
  # Model the Schema.org `Thing > Place`. See https://schema.org/Offer
@@ -1,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'date'
4
- require 'schema_dot_org'
5
- require 'schema_dot_org/person'
6
- require 'schema_dot_org/place'
7
- require 'schema_dot_org/contact_point'
8
4
 
5
+ require_relative 'person'
6
+ require_relative 'place'
9
7
 
10
8
  module SchemaDotOrg
11
9
  class Organization < SchemaType
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+
3
4
  #
4
5
  # Model the Schema.org **Person**. See http://schema.org/Person
5
6
  #
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+
3
4
  #
4
5
  # Model the Schema.org `Thing > Place`. See http://schema.org/Place
5
6
  #
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'date'
4
- require 'schema_dot_org'
5
- require 'schema_dot_org/aggregate_offer'
6
3
 
7
4
  #
8
5
  # Model the Schema.org `Thing > Place`. See https://schema.org/Product
@@ -5,7 +5,7 @@
5
5
  # Model the Schema.org `Thing > SearchAction`. See http://schema.org/SearchAction
6
6
  #
7
7
  module SchemaDotOrg
8
- class SearchAction < SchemaType
8
+ class SearchAction < SchemaDotOrg::SchemaType
9
9
  validated_attr :query_input, type: String, presence: true
10
10
  validated_attr :target, type: String, presence: true
11
11
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+
3
4
  #
4
5
  # Model the Schema.org `Thing > CreativeWork > WebSite`.
5
6
  # @See http://schema.org/WebSite
@@ -1,16 +1,127 @@
1
1
  # frozen_string_literal: true
2
- require 'schema_dot_org/schema_type'
3
- require 'schema_dot_org/search_action'
4
- require 'schema_dot_org/web_site'
5
- require 'schema_dot_org/organization'
6
- require 'schema_dot_org/product'
2
+ require 'json'
3
+ require 'validated_object'
4
+
5
+ #
6
+ # Abstract base class for all the Schema.org types.
7
+ #
8
+ module SchemaDotOrg
9
+ class SchemaType < ValidatedObject::Base
10
+ ROOT_ATTR = { "@context" => "https://schema.org" }.freeze
11
+ UNQUALIFIED_CLASS_NAME_REGEX = /([^:]+)$/
12
+
13
+
14
+ def to_s
15
+ json_string = to_json_ld(pretty: (!rails_production? && !ENV['SCHEMA_DOT_ORG_MINIFIED_JSON']))
16
+
17
+ # Mark as safe if we're in Rails
18
+ if json_string.respond_to?(:html_safe)
19
+ json_string.html_safe
20
+ else
21
+ json_string
22
+ end
23
+ end
24
+
25
+
26
+ def to_json_ld(pretty: false)
27
+ "<script type=\"application/ld+json\">\n" + to_json(pretty: pretty, as_root: true) + "\n</script>"
28
+ end
29
+
30
+
31
+ def to_json(pretty: false, as_root: false)
32
+ structure = as_root ? ROOT_ATTR.merge(to_json_struct) : to_json_struct
33
+
34
+ if pretty
35
+ JSON.pretty_generate(structure)
36
+ else
37
+ structure.to_json
38
+ end
39
+ end
40
+
41
+
42
+ # Use the class name to create the "@type" attribute.
43
+ # @return a hash structure representing json.
44
+ def to_json_struct
45
+ { "@type" => un_namespaced_classname }.merge(_to_json_struct.reject { |_, v| v.blank? })
46
+ end
47
+
48
+
49
+ def _to_json_struct
50
+ attrs_and_values
51
+ end
52
+
53
+
54
+ # @return the classname without the module namespace.
55
+ def un_namespaced_classname
56
+ self.class.name =~ UNQUALIFIED_CLASS_NAME_REGEX
57
+ Regexp.last_match(1)
58
+ end
59
+
60
+
61
+ def object_to_json_struct(object)
62
+ return nil unless object
63
+ object.to_json_struct
64
+ end
65
+
66
+
67
+ def attrs_and_values
68
+ attrs.map do |attr|
69
+ # Clean up and andle the `query-input` attribute, which
70
+ # doesn't follow the normal camelCase convention.
71
+ attr_name = snake_case_to_lower_camel_case(attr.to_s.delete_prefix('@')).sub('queryInput', 'query-input')
72
+ attr_value = instance_variable_get(attr)
73
+
74
+ [attr_name, resolve_value(attr_value)]
75
+ end.to_h
76
+ end
77
+
78
+
79
+ def resolve_value(value)
80
+ if value.is_a?(Array)
81
+ value.map { |v| resolve_value(v) }
82
+
83
+ elsif value.is_a?(Date)
84
+ value.to_s
85
+
86
+ elsif is_schema_type?(value)
87
+ value.to_json_struct
88
+
89
+ else
90
+ value
91
+ end
92
+ end
93
+
94
+
95
+ def snake_case_to_lower_camel_case(snake_case)
96
+ snake_case.to_s.split('_').map.with_index { |s, i| i.zero? ? s : s.capitalize }.join
97
+ end
98
+
99
+
100
+ def attrs
101
+ instance_variables.reject{ |v| [:@validation_context, :@errors].include?(v) }
102
+ end
103
+
104
+
105
+ def is_schema_type?(object)
106
+ object.class.module_parent == SchemaDotOrg
107
+ end
108
+
109
+
110
+ def rails_production?
111
+ defined?(Rails) && Rails.env.production?
112
+ end
113
+ end
114
+ end
115
+
116
+
117
+ require 'schema_dot_org/aggregate_offer'
118
+ require 'schema_dot_org/contact_point'
7
119
  require 'schema_dot_org/item_list'
8
120
  require 'schema_dot_org/list_item'
121
+ require 'schema_dot_org/organization'
9
122
  require 'schema_dot_org/person'
10
123
  require 'schema_dot_org/place'
11
- require 'schema_dot_org/aggregate_offer'
124
+ require 'schema_dot_org/product'
12
125
  require 'schema_dot_org/offer'
13
- require 'schema_dot_org/contact_point'
14
-
15
- module SchemaDotOrg
16
- end
126
+ require 'schema_dot_org/search_action'
127
+ require 'schema_dot_org/web_site'
@@ -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.1'
10
+ spec.version = '2.2.2'
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.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
@@ -95,7 +95,6 @@ files:
95
95
  - lib/schema_dot_org/person.rb
96
96
  - lib/schema_dot_org/place.rb
97
97
  - lib/schema_dot_org/product.rb
98
- - lib/schema_dot_org/schema_type.rb
99
98
  - lib/schema_dot_org/search_action.rb
100
99
  - lib/schema_dot_org/web_site.rb
101
100
  - schema_dot_org.gemspec
@@ -1,110 +0,0 @@
1
- require 'json'
2
- require 'validated_object'
3
-
4
- module SchemaDotOrg
5
- class SchemaType < ValidatedObject::Base
6
- ROOT_ATTR = { "@context" => "http://schema.org" }.freeze
7
- UNQUALIFIED_CLASS_NAME_REGEX = /([^:]+)$/
8
-
9
-
10
- def to_s
11
- json_string = to_json_ld(pretty: (!rails_production? && !ENV['SCHEMA_DOT_ORG_MINIFIED_JSON']))
12
-
13
- # Mark as safe if we're in Rails
14
- if json_string.respond_to?(:html_safe)
15
- json_string.html_safe
16
- else
17
- json_string
18
- end
19
- end
20
-
21
-
22
- def to_json_ld(pretty: false)
23
- "<script type=\"application/ld+json\">\n" + to_json(pretty: pretty, as_root: true) + "\n</script>"
24
- end
25
-
26
-
27
- def to_json(pretty: false, as_root: false)
28
- structure = as_root ? ROOT_ATTR.merge(to_json_struct) : to_json_struct
29
-
30
- if pretty
31
- JSON.pretty_generate(structure)
32
- else
33
- structure.to_json
34
- end
35
- end
36
-
37
-
38
- # Use the class name to create the "@type" attribute.
39
- # @return a hash structure representing json.
40
- def to_json_struct
41
- { "@type" => un_namespaced_classname }.merge(_to_json_struct.reject { |_, v| v.blank? })
42
- end
43
-
44
-
45
- def _to_json_struct
46
- attrs_and_values
47
- end
48
-
49
-
50
- # @return the classname without the module namespace.
51
- def un_namespaced_classname
52
- self.class.name =~ UNQUALIFIED_CLASS_NAME_REGEX
53
- Regexp.last_match(1)
54
- end
55
-
56
-
57
- def object_to_json_struct(object)
58
- return nil unless object
59
- object.to_json_struct
60
- end
61
-
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
-
105
-
106
- def rails_production?
107
- defined?(Rails) && Rails.env.production?
108
- end
109
- end
110
- end