### Nested Schema Example: Organization with Address
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Illustrates the declarative way to build complex nested schemas, such as an Organization with an Address and ContactPoint.
```ruby
require 'schema_dot_org'
full_schema = SchemaDotOrg::Organization.new do |org|
org.name = "Tech Innovations Inc."
org.url = "https://techinnovations.com"
org.address = SchemaDotOrg::PostalAddress.new do |addr|
addr.street_address = "123 Innovation Drive"
addr.address_locality = "Metropolis"
addr.address_region = "CA"
addr.postal_code = "90210"
addr.address_country = "USA"
end
org.contact_point = SchemaDotOrg::ContactPoint.new do |cp|
cp.contact_type = "Sales Inquiry"
cp.email = "sales@techinnovations.com"
end
end
puts full_schema.to_json_ld
```
--------------------------------
### Rails Controller: Generate Schema.org Object
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Example of instantiating a Schema.org object in a Rails controller using model data. Assumes you have a `Product` model.
```ruby
require 'schema_dot_org'
class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
@schema_object = SchemaDotOrg::Product.new do |schema|
schema.name = @product.name
schema.description = @product.description
schema.sku = @product.sku
# Add other relevant properties
end
end
end
```
--------------------------------
### Render Organization Schema.org Data in HTML
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
Output the structured data object as a JSON-LD script tag within an HTML template. This example shows how to render the previously created Organization object.
```html
```
--------------------------------
### Handle Unknown Schema.org Attributes in Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
Illustrates how the SchemaDotOrg library prevents the creation of invalid markup by raising errors for unknown attributes. This example attempts to set an 'author' attribute on a Place object, which is not a valid attribute.
```ruby
Place.new(
address: '12345 Happy Street',
author: 'Hemmingway'
)
# => NoMethodError: undefined method `author'
```
--------------------------------
### Rails View: Render Schema.org Object
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Example of rendering a Schema.org object in a Rails view. The gem handles HTML-safe output and formatting.
```erb
<%# In your Rails view (e.g., app/views/products/show.html.erb) %>
<%= @schema_object %>
```
--------------------------------
### Handle Invalid Schema.org Data Types in Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
Demonstrates how the SchemaDotOrg library prevents the creation of invalid markup by raising errors for incorrect data types. This example shows an invalid type for the 'address' attribute.
```ruby
Place.new(address: 12345)
# => ArgumentError: Address is class Integer, not String
```
--------------------------------
### Non-Rails Ruby: Generate JSON-LD Schema
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Demonstrates how to use the Schema.org gem in a standalone Ruby script to generate JSON-LD script tags. This example creates an Organization schema.
```ruby
require 'schema_dot_org'
organization_schema = SchemaDotOrg::Organization.new do |org|
org.name = "My Awesome Company"
org.url = "https://example.com"
org.logo = "https://example.com/logo.png"
org.contact_point = SchemaDotOrg::ContactPoint.new do |cp|
cp.contact_type = "Customer Service"
cp.telephone = "+1-800-555-1212"
cp.email = "info@example.com"
end
end
puts organization_schema.to_json_ld
# Or to get the script tag:
puts organization_schema.to_s
```
--------------------------------
### Validate Schema Types and Handle Errors in Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
This snippet demonstrates the type validation performed by the Schema.org gem. It shows examples of errors raised for wrong attribute types, unknown attributes, missing required attributes, invalid nested types, and incorrect array element types. These validations ensure the generated markup is valid.
```ruby
# Wrong attribute type
SchemaDotOrg::Place.new(address: 12345)
# => ArgumentError: Address is class Integer, not String
# Unknown attribute
SchemaDotOrg::Organization.new(
name: 'Test',
logo: 'https://example.com/logo.png',
url: 'https://example.com',
snack_time: 'today'
)
# => NoMethodError: undefined method `snack_time'
# Missing required attribute
SchemaDotOrg::Organization.new(
name: 'Test',
url: 'https://example.com'
)
# => ArgumentError: Logo can't be blank
# Invalid nested type
SchemaDotOrg::Organization.new(
name: 'Test',
logo: 'https://example.com/logo.png',
url: 'https://example.com',
founder: 'Not a Person object'
)
# => ArgumentError: Founder is class String, not Person
# Array validation
SchemaDotOrg::ListItem.new(
position: 1,
name: 'Test',
item: 12345
)
# => ArgumentError: Item must be a URL string or Schema.org object, but was: Integer
```
--------------------------------
### Create Basic WebSite Schema - Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
This Ruby code snippet demonstrates how to instantiate a `WebSite` object with only the required attributes: `name` and `url`. This forms the basic structure for representing a website in Schema.org.
```ruby
WebSite.new(
name: 'Texas Public Law',
url: 'https://texas.public.law',
)
```
--------------------------------
### Create WebSite Schema with SearchAction - Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
This Ruby code illustrates creating a `WebSite` schema that includes an optional `SearchAction`. This enhances the website's representation by enabling a Sitelinks Searchbox in search results, providing users with a direct search capability from the SERP. It requires specifying the `target` URL for the search and the `query_input` parameter.
```ruby
WebSite.new(
name: 'Texas Public Law',
url: 'https://texas.public.law',
potential_action: SearchAction.new(
target: 'https://texas.public.law/?search={search_term_string}',
query_input: 'required name=search_term_string'
)
)
```
--------------------------------
### Create Product Schema with Offers in Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Generates Schema.org Product structured data, including pricing information and aggregate offers. This Ruby code utilizes the 'schema_dot_org' gem to define products with single or multiple offers, specifying price, currency, availability, and URLs.
```ruby
# Product with single offer
product = SchemaDotOrg::Product.new(
name: 'Professional Service Package',
description: 'Comprehensive legal research and analysis',
url: 'https://www.public.law/products/pro',
image: [
'https://www.public.law/images/pro-package.jpg',
'https://www.public.law/images/pro-package-alt.jpg'
],
offers: SchemaDotOrg::AggregateOffer.new(
lowPrice: 99.00,
highPrice: 299.00,
priceCurrency: 'USD',
offerCount: '3',
offers: [
SchemaDotOrg::Offer.new(
price: 99.00,
priceCurrency: 'USD',
availability: 'https://schema.org/InStock',
url: 'https://www.public.law/products/pro/basic'
),
SchemaDotOrg::Offer.new(
price: 199.00,
priceCurrency: 'USD',
availability: 'https://schema.org/InStock',
url: 'https://www.public.law/products/pro/standard'
),
SchemaDotOrg::Offer.new(
price: 299.00,
priceCurrency: 'USD',
availability: 'https://schema.org/InStock',
url: 'https://www.public.law/products/pro/premium'
)
]
)
)
product.to_json_struct
# => {
# "@type" => "Product",
# "name" => "Professional Service Package",
# "description" => "Comprehensive legal research and analysis",
# "url" => "https://www.public.law/products/pro",
# "image" => [...],
# "offers" => {
# "@type" => "AggregateOffer",
# "lowPrice" => 99.0,
# "highPrice" => 299.0,
# "priceCurrency" => "USD",
# ...
# }
# }
```
--------------------------------
### Create WebSite Schema with SearchAction using Schema.org Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Creates Schema.org WebSite structured data, including an optional SearchAction for Google Sitelinks Searchbox integration. The `target` parameter in SearchAction specifies the URL pattern for search queries.
```ruby
# Basic website
website = SchemaDotOrg::WebSite.new(
name: 'Texas Public Law',
url: 'https://texas.public.law'
)
# Website with search functionality
website = SchemaDotOrg::WebSite.new(
name: 'Texas Public Law',
url: 'https://texas.public.law',
potential_action: SchemaDotOrg::SearchAction.new(
target: 'https://texas.public.law/?search={search_term_string}',
query_input: 'required name=search_term_string'
)
)
website.to_json_struct
```
--------------------------------
### Declare Product Schema Type with Validated Attributes (Ruby)
Source: https://github.com/public-law/schema-dot-org/blob/master/CLAUDE.md
Demonstrates how to define a 'Product' Schema.org type using the SchemaType base class. It utilizes validated_attr for type-safe attribute declarations, including required String types and optional String types. The 'offers' attribute is typed as an AggregateOffer, showcasing nested schema types. This pattern ensures data validation upon object instantiation.
```Ruby
class Product < SchemaType
validated_attr :name, type: String
validated_attr :description, type: String, allow_nil: true
validated_attr :offers, type: SchemaDotOrg::AggregateOffer
end
```
--------------------------------
### Create ItemList Schema using Schema.org Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Generates Schema.org ItemList structured data for lists of items, such as articles. Supports specifying the order, number of items, and associated URL and image. Each item in the list is represented by a ListItem object.
```ruby
# Article list
item_list = SchemaDotOrg::ItemList.new(
itemListElement: [
SchemaDotOrg::ListItem.new(
position: 1,
url: 'https://example.com/articles/introduction',
name: 'Introduction to Schema.org'
),
SchemaDotOrg::ListItem.new(
position: 2,
url: 'https://example.com/articles/structured-data',
name: 'Understanding Structured Data'
),
SchemaDotOrg::ListItem.new(
position: 3,
url: 'https://example.com/articles/seo-benefits',
name: 'SEO Benefits of Schema Markup'
)
],
itemListOrder: 'https://schema.org/ItemListOrderAscending',
numberOfItems: 3,
url: 'https://example.com/articles',
image: 'https://example.com/images/articles-banner.jpg'
)
item_list.to_json_struct
```
--------------------------------
### Create Person Schema with Profile Details in Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Generates Schema.org Person structured data, allowing for basic names to comprehensive profiles including honors, awards, and educational institutions. It requires the 'schema_dot_org' gem and enforces a mandatory 'name' attribute, raising an error if omitted.
```ruby
# Basic person
person = SchemaDotOrg::Person.new(
name: 'Jane Doe'
)
# Person with full attributes
person = SchemaDotOrg::Person.new(
name: 'Dr. Jane Doe',
honorific_suffix: 'PhD',
url: 'https://janedoe.com',
award: 'Best Researcher 2023',
alumni_of: SchemaDotOrg::CollegeOrUniversity.new(
name: 'Stanford University',
url: 'https://stanford.edu'
),
same_as: [
'https://twitter.com/janedoe',
'https://linkedin.com/in/janedoe'
]
)
# Name is required
SchemaDotOrg::Person.new(url: 'https://example.com')
# => ArgumentError: Name can't be blank
```
--------------------------------
### Create Breadcrumbs using Schema.org Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Generates Schema.org BreadcrumbList structured data. Supports automatic creation from a list of hashes and manual creation using BreadcrumbList and ListItem objects. Invalid URLs will raise an ArgumentError.
```ruby
breadcrumbs = SchemaDotOrg.make_breadcrumbs([
{ name: 'Home', url: 'https://example.com' },
{ name: 'Books', url: 'https://example.com/books' },
{ name: 'Science Fiction', url: 'https://example.com/books/sci-fi' },
{ name: 'Award Winners' } # Last item typically has no URL
])
breadcrumbs.to_json_struct
```
```ruby
breadcrumbs = SchemaDotOrg::BreadcrumbList.new(
itemListElement: [
SchemaDotOrg::ListItem.new(position: 1, name: 'Home', item: 'https://example.com'),
SchemaDotOrg::ListItem.new(position: 2, name: 'Products', item: 'https://example.com/products'),
SchemaDotOrg::ListItem.new(position: 3, name: 'Current Page')
]
)
```
--------------------------------
### Create Place and PostalAddress Schemas using Schema.org Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Generates Schema.org Place and PostalAddress structured data for location information. Supports simple string addresses or detailed structured addresses with fields like streetAddress, locality, region, postalCode, and country.
```ruby
# Simple place with string address
place = SchemaDotOrg::Place.new(
address: 'Portland, OR'
)
# Place with structured postal address
place = SchemaDotOrg::Place.new(
address: SchemaDotOrg::PostalAddress.new(
streetAddress: '1600 Amphitheatre Parkway',
addressLocality: 'Mountain View',
addressRegion: 'CA',
postalCode: '94043',
addressCountry: 'US'
)
)
place.to_json_struct
# Address is required
SchemaDotOrg::Place.new
```
--------------------------------
### Create Organization Schema.org Data in Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
Instantiate a structured data object for an Organization in Ruby. This object can then be rendered as JSON-LD in a template. It handles the conversion of Ruby snake_case to Schema.org camelCase and validates attribute types and formats.
```ruby
@my_org = Organization.new(
name: 'Public.Law',
founder: Person.new(name: 'Robb Shecter'),
founding_date: Date.new(2009, 3, 6),
founding_location: Place.new(address: 'Portland, OR'),
email: 'support@public.law',
telephone: '+1 123 456 7890',
url: 'https://www.public.law',
logo: 'https://www.public.law/favicon-196x196.png',
same_as: [
'https://twitter.com/law_is_code',
'https://www.facebook.com/PublicDotLaw'
]
)
```
--------------------------------
### Create ContactPoint Schema in Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
This snippet shows how to create a ContactPoint schema object for customer service. It demonstrates setting various attributes like contact type, telephone, language, and area served. It can be used independently or within an Organization schema.
```ruby
# Single contact point
contact = SchemaDotOrg::ContactPoint.new(
contact_type: 'customer service',
telephone: '+1-800-555-1212',
contact_option: 'TollFree',
available_language: ['English', 'Spanish'],
area_served: ['US', 'CA', 'MX']
)
contact.to_json_struct
# => {
# "@type" => "ContactPoint",
# "contactType" => "customer service",
# "telephone" => "+1-800-555-1212",
# "contactOption" => "TollFree",
# "availableLanguage" => ["English", "Spanish"],
# "areaServed" => ["US", "CA", "MX"]
# }
# Use with Organization
organization = SchemaDotOrg::Organization.new(
name: 'Example Corp',
logo: 'https://example.com/logo.png',
url: 'https://example.com',
contact_points: [
SchemaDotOrg::ContactPoint.new(
contact_type: 'customer service',
telephone: '+1-800-555-1212'
),
SchemaDotOrg::ContactPoint.new(
contact_type: 'technical support',
telephone: '+1-800-555-1213'
)
]
)
```
--------------------------------
### Generate BreadcrumbList from Array - Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
The `make_breadcrumbs` method generates a Schema.org BreadcrumbList JSON-LD from a given array of hashes. Each hash should represent a link with optional `name` and `url` properties. The method automatically assigns positions and validates URLs, raising an `ArgumentError` for invalid ones. This is useful for improving site navigation in search results.
```ruby
links = [
{ name: 'Home', url: 'https://example.com' },
{ name: 'Books', url: 'https://example.com/books' },
{ name: 'Science Fiction', url: 'https://example.com/books/sci-fi' },
{ name: 'Award Winners' } # Last item typically has no URL
]
SchemaDotOrg
.make_breadcrumbs(links)
.to_s
```
--------------------------------
### Add schema_dot_org Gem to Gemfile - Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
This snippet shows how to add the `schema_dot_org` gem to your application's `Gemfile`. Including this line allows you to use the gem's functionalities for generating Schema.org structured data within your Ruby project.
```ruby
gem 'schema_dot_org'
```
--------------------------------
### Output Schema Objects in Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
This snippet illustrates different methods for outputting schema objects in Ruby. It covers conversion to an HTML script tag (default for Rails), a pretty-printed JSON-LD script tag, a minified JSON string, and a Ruby hash structure. It also mentions environment-based formatting.
```ruby
organization = SchemaDotOrg::Organization.new(
name: 'Example',
logo: 'https://example.com/logo.png',
url: 'https://example.com'
)
# Output as HTML script tag (default, for Rails views)
organization.to_s
# => ""
# Output as JSON-LD script tag with pretty formatting
organization.to_json_ld(pretty: true)
# => ""
# Output as JSON string only
organization.to_json(as_root: true, pretty: false)
# => "{\"@context\":\"https://schema.org\",\"@type\":\"Organization\",...}"
# Output as Ruby hash structure
organization.to_json_struct
# => {"@type"=>"Organization", "name"=>"Example", ...}
```
--------------------------------
### Create Comment Schema using Schema.org Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Generates Schema.org Comment structured data, supporting nested comments and interaction statistics like likes. Required fields like 'author' and 'text' are enforced, raising an ArgumentError if omitted.
```ruby
comment = SchemaDotOrg::Comment.new(
author: SchemaDotOrg::Person.new(name: 'John Smith'),
datePublished: Date.new(2024, 1, 15),
text: 'This is an insightful analysis of the legal framework.',
url: 'https://example.com/article#comment-123',
inLanguage: ['en-US'],
interactionStatistic: [
SchemaDotOrg::InteractionCounter.new(
interactionType: 'https://schema.org/LikeAction',
userInteractionCount: 42
)
],
comment: [
SchemaDotOrg::Comment.new(
author: SchemaDotOrg::Person.new(name: 'Jane Doe'),
datePublished: Date.new(2024, 1, 16),
text: 'I agree with this perspective.'
)
]
)
# Required fields enforced
SchemaDotOrg::Comment.new(text: 'Comment text')
```
--------------------------------
### Extend with Custom Schema Types in Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
This snippet illustrates how to define and use custom Schema.org types in Ruby. It shows the creation of a `CustomType` class inheriting from `SchemaType`, defining various attribute types including optional, required, nested, union, array, and numeric. It also indicates where to require the custom type in the main gem file.
```ruby
# lib/schema_dot_org/custom_type.rb
module SchemaDotOrg
class CustomType < SchemaType
# Optional attributes
validated_attr :optional_string, type: String, allow_nil: true
validated_attr :optional_date, type: Date, allow_nil: true
# Required attributes
validated_attr :required_name, type: String, presence: true
validated_attr :required_url, type: String
# Nested schema types
validated_attr :author, type: Person, allow_nil: true
# Union types (string or array)
validated_attr :same_as, type: union(String, [String]), allow_nil: true
# Arrays
validated_attr :images, type: Array, allow_nil: true
# Numeric types
validated_attr :rating, type: Numeric, allow_nil: true
end
end
# Then add to lib/schema_dot_org.rb
require 'schema_dot_org/custom_type'
```
--------------------------------
### BreadcrumbList JSON-LD Output - HTML
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
This HTML snippet demonstrates the JSON-LD output generated for a Schema.org BreadcrumbList. It structures navigation links with their respective positions, names, and items (URLs). This format is recognized by search engines to display rich snippets for website navigation.
```html
```
--------------------------------
### Define Schema.org Organization Type in Ruby
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
This Ruby code defines the Schema.org Organization type using a declarative style. It includes various validated attributes such as address, contact points, email, founder, founding date, and legal name. It also specifies required attributes like logo, name, and URL, adhering to the Schema.org Organization specification.
```ruby
require 'date'
require_relative 'person'
require_relative 'place'
require_relative 'postal_address'
module SchemaDotOrg
##
# See https://schema.org/Organization
#
class Organization < SchemaType
validated_attr :address, type: PostalAddress, allow_nil: true
validated_attr :contact_points, type: union(ContactPoint, [ContactPoint]), allow_nil: true
validated_attr :email, type: String, allow_nil: true
validated_attr :founder, type: Person, allow_nil: true
validated_attr :founding_date, type: Date, allow_nil: true
validated_attr :founding_location, type: Place, allow_nil: true
validated_attr :legal_name, type: String, allow_nil: true
validated_attr :same_as, type: union(String, [String]), allow_nil: true
validated_attr :slogan, type: String, allow_nil: true
validated_attr :telephone, type: String, allow_nil: true
########################################
# Attributes that are required by Google
########################################
validated_attr :logo, type: String
validated_attr :name, type: String
validated_attr :url, type: String
end
end
```
--------------------------------
### Create Organization Schema with Nested Types in Ruby
Source: https://context7.com/public-law/schema-dot-org/llms.txt
Generates Schema.org Organization structured data with nested Person, Place, and ContactPoint types. It requires the 'schema_dot_org' gem and outputs JSON-LD either as an HTML-safe script tag or a JSON structure. Validation errors are raised immediately for invalid data.
```ruby
require 'schema_dot_org'
# Create organization with all common attributes
organization = SchemaDotOrg::Organization.new(
name: 'Public.Law',
logo: 'https://www.public.law/favicon-196x196.png',
url: 'https://www.public.law',
email: 'support@public.law',
telephone: '+1 123 456 7890',
founder: SchemaDotOrg::Person.new(name: 'Robb Shecter'),
founding_date: Date.new(2009, 3, 6),
founding_location: SchemaDotOrg::Place.new(address: 'Portland, OR'),
address: SchemaDotOrg::PostalAddress.new(
streetAddress: '123 Main St',
addressLocality: 'Denver',
addressRegion: 'CO',
postalCode: '80202',
addressCountry: 'US'
),
same_as: [
'https://twitter.com/law_is_code',
'https://www.facebook.com/PublicDotLaw'
],
contact_points: SchemaDotOrg::ContactPoint.new(
contact_type: 'customer service',
telephone: '+1-800-555-1212',
contact_option: 'TollFree',
available_language: ['English', 'Spanish']
)
)
# Output as JSON-LD script tag (use in Rails views)
organization.to_s
# =>
# Output as JSON object
organization.to_json_struct
# => {"@type"=>"Organization", "name"=>"Public.Law", ...}
# Validation errors are immediate
SchemaDotOrg::Organization.new(name: 'Test', url: 'https://example.com')
# => ArgumentError: Logo can't be blank
```
--------------------------------
### Generated Organization Schema.org JSON-LD
Source: https://github.com/public-law/schema-dot-org/blob/master/README.md
The resulting perfectly formatted structured data in JSON-LD format for an Organization. This output is automatically generated from the Ruby object.
```json
{
"@context": "http://schema.org",
"@type": "Organization",
"name": "Public.Law",
"email": "support@public.law",
"telephone": "+1 123 456 7890",
"url": "https://www.public.law",
"logo": "https://www.public.law/favicon-196x196.png",
"foundingDate": "2009-03-06",
"founder": {
"@type": "Person",
"name": "Robb Shecter"
},
"foundingLocation": {
"@type": "Place",
"address": "Portland, OR"
},
"sameAs": [
"https://twitter.com/law_is_code",
"https://www.facebook.com/PublicDotLaw"
]
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.