OpenGraphReader
A library to fetch and parse OpenGraph properties from an URL or a given string.
Features
- Strives to be robust and complete.
- Intuitive method based API.
- Supports custom prefixes.
- Validates properties according to their type.
Anti-features
- Can ignore the complete object if anything is invalid.
- Does not fall back to guess the basic attributes from regular tags.
- Only supports the latest OpenGraph protocol as defined at http://ogp.me out of the box.
- Objects and their properties are defined in code, not by parsing the response at the namespace identifier.
Ruby 3.1 and later are supported. Compatibility with earlier versions might exist but is not verified.
Installation
Add this line to your application's Gemfile:
gem "open_graph_reader"
And then execute:
$ bundle
Or install it yourself as:
$ gem install open_graph_reader
Install the following gems the same way for a higher success rate at fetching websites:
Usage
require "open_graph_reader"
object = OpenGraphReader.fetch("http://examples.opengraphprotocol.us/article.html")
object = OpenGraphReader.fetch!("http://examples.opengraphprotocol.us/article.html")
object = OpenGraphReader.parse(html)
object = OpenGraphReader.parse!(html)
object.og.title
object.og.description
object.og.image.to_s
object.og.image.content
object.og.image.url
object.og.image.width
object.og.images.first == object.og.image
object.article.tags
class MyNamespace
include OpenGraphReader::Object
namespace :my, :namespace
string :name, required: true
url :url, default: "http://example.org/my_namespace"
integer :pages, collection: true
end
Documentation
Contributing
- Fork it ( https://github.com/jhass/open_graph_reader/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request