SchemaOrg is a library for working with Schema.org objects, allowing you to create, manipulate, and generate structured data in JSON-LD format. The main purpose of the library is to support Schema.org types and attributes for integrating structured data into web pages.
- Generate valid JSON-LD data for structured objects.
- Support for standard Schema.org types (e.g.,
Offer,ItemList,ListItem). - Built on
Dry::Structfor statically typed data. - Easy data manipulation and seamless web application integration.
To install the library, add the following to your Gemfile:
bundle add schema_orgIf you are not using bundler, install the gem manually:
gem install schema_orgExample: Creating an Offer object.
require 'schema_org'
offer = SchemaOrg::Offer.new(
priceCurrency: 'USD',
price: 100,
availability: 'InStock',
url: 'https://example.com/offer'
)
# Generate JSON-LD
puts offerOutput:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Offer",
"priceCurrency": "USD",
"price": 100,
"availability": "InStock",
"url": "https://example.com/offer"
}
</script>For describing a list of items, use the ItemList class:
list_item1 = SchemaOrg::ListItem.new(
position: 1,
name: 'First Item',
url: 'https://example.com/item1'
)
list_item2 = SchemaOrg::ListItem.new(
position: 2,
name: 'Second Item',
url: 'https://example.com/item2'
)
item_list = SchemaOrg::ItemList.new(
itemListElement: [list_item1, list_item2],
itemListOrder: 'Ascending',
numberOfItems: 2
)
puts item_listThe base class from which all Schema.org classes derive. Implements common methods for JSON-LD generation.
Key Points:
- All subclasses automatically get the
@typeattribute, representing the Schema.org object type. - The
to_json_ldmethod converts an object into JSON-LD format.
Represents the Schema.org Offer entity.
priceCurrency(required): The currency of the price (e.g.,"USD").price(required): The price of the offer.availability(optional): Availability status (e.g.,"InStock"or"OutOfStock").url(optional): A URL linking to the offer's page.
offer = SchemaOrg::Offer.new(priceCurrency: 'USD', price: 300, availability: 'InStock')
puts offerRepresents the Schema.org ItemList, used for describing lists of objects.
itemListElement(optional): List items, an array ofListItemobjects.itemListOrder(optional): The order of the list ("Ascending","Descending").numberOfItems(optional): Total number of items in the list.
See the Advanced Features section.
Represents an item in a Schema.org ItemList.
image(optional): A URL of the item's image.item(optional): The item itself (can be a URL or another Schema.org object).name(optional): The name of the item.position(optional): The position of the item in the list.url(optional): A URL linking to the item.
JSON-LD (JavaScript Object Notation for Linked Data) is used to structure data for better indexing by search engines. The library automatically generates valid data according to Schema.org requirements.
Example of embedding JSON-LD in HTML:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Offer",
"priceCurrency": "USD",
"price": 100,
"availability": "InStock"
}
</script>To get started with the project, follow these steps:
- Install dependencies:
bin/setup- Run tests to ensure functionality:
rake spec- Use the interactive console for experiments:
bin/console- To release a new gem version, update
version.rband run:
bundle exec rake releaseThis project is open source and available under the terms of the MIT License.