4 ms·
Here's a quick example. Put this in a Rails template: <%= WebSite.new( name: 'Texas Public Law', url: 'https://texas.public.law', )%>
by dogweather 8y ago
Here's a quick example. Put this in a Rails template:
<%= WebSite.new(
name: 'Texas Public Law',
url: 'https://texas.public.law',
)%>
...and you'll see this in the HTML:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"name": "Texas Public Law",
"url": "https://texas.public.law"
}
</script>
This DRY's up my code - I just encode the right way of forming JSON-LD and the correct objects and attributes in one place, the library. (That JSON-LD is from my live site, https://texas.public.law https://texas.public.law)
Details:
First I created a small library which wraps Rails ActiveModel Validations so they can be used in any Ruby object. And I supply a new validator, TypeValidator.
https://github.com/public-law/validated_object https://github.com/public-law/validated_object
With this building block, I can create classes for each Schema type I care about. And they strictly enforce their attributes:
class Organization < SchemaType
attr_accessor founding_date
validates founding_date, type: Date
end
That's a little redundant, but to a Rails developer, it's immediately clear what's going on. The full Organization code: https://github.com/public-law/schema-dot-org/blob/master/lib/schema_dot_org/organization.rb https://github.com/public-law/schema-dot-org/blob/master/lib...
I'm not using Ruby-RDF at all. I looked into the current Ruby meta-data and strong typing work, and found it all too complex for my simpler use case:
I'm creating websites, and I want to embed correct JSON-LD-formatted schema.org in a rock-solid way: (1) Correct syntax, and (2) correct semantics. I've got the information as Rails models and objects, and I need to convert this to HTML/JSON-LD.
So all of these objects, like Organization above, recursively creates proper markup in response to #to_s:
<%= @an_organization %>
...instantiated from that class above, will result in this in the generated HTML page:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
etc.
https://github.com/public-law/schema-dot-org#usage https://github.com/public-law/schema-dot-org#usage
- igravious 8y agoThanks for the in-depth explanation. Sounds really great. I'll add it to my Ruby RDF toolkit. What do you think of Spira https://github.com/ruby-rdf/spira https://github.com/ruby-rdf/spira , does it complement what you are doing in any way?
- dogweather 8y agoThat's pretty cool! But yeah, I don't have any RDF data sources. I parse lots of plain text and PDFs, and import some databases directly. And for generating the website metadata, that info is coming from the Rails models.