• Stars
    star
    54
  • Rank 544,902 (Top 11 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy structured data for websites. All validated for syntax and semantics. Simple declarative-style coding.

Gem Version Maintainability

SchemaDotOrg

Easily create Structured Data with correct syntax and semantics. Good structured data helps enhance a website's search result appearance:

Google Search works hard to understand the content of a page. You can help us by providing explicit clues about the meaning of a page…

Usage

Let's say you have a Rails app. First write plain-ruby code in a controller. Just instantiate the structured data object you want in your web page:

@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:            '[email protected]',
  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'
    ]
  )

...and then output it in a template:

<%= @my_org %>

...you'll get this perfectly formatted structured data in your HTML:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Organization",
  "name": "Public.Law",
  "email": "[email protected]",
  "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"
  ]
}
</script>

Principle: No silent failures

We coded the library this way because the data is embedded in the HTML - and it's a pain in the butt to manually check for errors. In my case, I manage 500,000 unique pages in my Rails app. There's no way I could place error-free structured data in them without automatic validation.

SchemaDotOrg will validate your Ruby code, and if it's correct, will generate Schema.org JSON-LD markup when #to_s is called. If you, e.g., didn't add the correct attributes, you'll get a descriptive error message pointing you to the problem.

Notice how the foundingDate is in the required ISO-8601 format. In the same way, the foundingLocation is a Place which adds the proper @type attribute. All Ruby snake-case names have been converted to the Schema.org standard camel-case. Etc., etc.

You are prevented from creating invalid markup

If your page loads, you know your markup is good.

If you use the wrong type or try to set an unknown attribute, SchemaDotOrg will refuse to create the incorrect JSON-LD. Instead, you'll get a message explaining the problem:

Place.new(address: 12345)
# => ArgumentError: Address is class Integer, not String

Place.new(
  address: '12345 Happy Street',
  author:  'Hemmingway'
)
# => NoMethodError: undefined method `author'

In my experience, I never get errors from the lib. I code it once, it works, and then I move on to other things.

This automatic validation comes from my ValidatedObject gem, which in turn, is a thin wrapper around ActiveRecord::Validations. So there's nothing magical going on here.

Supported Schema.org Types

AggregateOffer, ContactPoint, ItemList, ListItem, Offer, Organization, Person, Place, Product, SearchAction, and WebSite.

Here are a few examples. The source code for these is extremely easy to read. Check them out to see all the available attributes.

WebSite

Example with only the required attributes:

WebSite.new(
  name: 'Texas Public Law',
  url:  'https://texas.public.law',
)

With the optional SearchAction to enable a Sitelinks Searchbox:

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'
  )
)

Organization

Example:

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:            '[email protected]',
  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'
  ]
)

Installation

Add this line to your application's Gemfile:

gem 'schema_dot_org'

Development

The coding is as DRY as I could possibly make it. I think it's really easy to create and add to. For example, here's Product:

class Product < SchemaType
  validated_attr :description,  type: String, allow_nil: true
  validated_attr :image,        type: Array,  allow_nil: true
  validated_attr :name,         type: String
  validated_attr :offers,       type: SchemaDotOrg::AggregateOffer
  validated_attr :url,          type: String
end

The attributes are from the Schema.org Product spec.

All Rails validations are available. These are just the attributes we've felt like adding. PR's are welcome if you want to add more. Also for more Schema.org types.

Contributing

Bug reports and pull requests are welcome on GitHub.

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

asset_ram

Reduce Rails allocations by 35%+ and gain a speed boost. Memoizes asset links.
Ruby
177
star
2

phoenix-docker-compose

Docker development environment for Elixir+Phoenix
Shell
155
star
3

naturally

Natural sort algorithm
Ruby
86
star
4

rspec-webservice_matchers

Black-box web app testing
Ruby
79
star
5

repo-health-check

Analyze a project: How are the maintainers doing?
CoffeeScript
75
star
6

validated_object

Self-validating Ruby objects
Ruby
60
star
7

rails-docker-compose

Docker development environment for Ruby on Rails
Shell
51
star
8

raspberry-pi-python-github-runner

Sample configuration for using Raspberry Pi's as Python Github Runners
Python
21
star
9

non_empty_array

An ordered list guaranteed to have at least one element
Ruby
21
star
10

digbang

Safer Ruby hash and array traversal
Ruby
9
star
11

yahboom-raspi-cooling-fan

Code and help for the Yahboom Raspberry Pi Cooling Fan with RGB and OLED display
Python
9
star
12

forkful

An open-content programming cookbook. A responsible use of AI proof of concept. Collaborative, polyglot and multilingual.
Ruby
9
star
13

language-comparison

A GraphQL server prototyped in many languages
Python
5
star
14

ketobit

Fitbit carb tracking
Elixir
5
star
15

mctop

Python CLI Memcached monitor
Python
4
star
16

python-exhaustiveness-adts-monads

Demo code showing off the new true exhaustiveness checks with Python 3.10 + Pyright
Python
4
star
17

Super-Cat-City-Alerts

A city alert notification system
Ruby
3
star
18

safe-to-upgrade

2
star
19

stable_profile

Run rspec --profile several times, averaging the results.
Ruby
2
star
20

HTTP-Assertions

Check web server configurations via Ruby test cases.
Ruby
1
star
21

seed-report

Simple reporting for Ruby on Rails seed files
Ruby
1
star
22

javafind

Unix find implemented in Java. Vintage code from 2004
HTML
1
star
23

beginning-programming-with-python

Jupyter Notebook
1
star
24

victor-hugo

JavaScript
1
star
25

python-enum-import-issue

Python Enums aren't singletons—demo
Python
1
star
26

curl_ex

Simple Elixir HTTP using curl
Elixir
1
star