File: README.md

package info (click to toggle)
ruby-open-graph-reader 0.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,980 kB
  • ctags: 133
  • sloc: ruby: 1,505; xml: 22; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,991 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# OpenGraphReader [![Gem Version](https://badge.fury.io/rb/open_graph_reader.svg)](http://badge.fury.io/rb/open_graph_reader) [![Build Status](https://travis-ci.org/jhass/open_graph_reader.svg?branch=master)](https://travis-ci.org/jhass/open_graph_reader)

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 2.0 and later are supported.

## Installation

Add this line to your application's Gemfile:

```ruby
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:

* [faraday_middleware](https://github.com/lostisland/faraday_middleware)
* [faraday-cookie_jar](https://github.com/miyagawa/faraday-cookie_jar)

## Usage

```ruby
require 'open_graph_reader'

# Returns nil if anything on the object is invalid
object = OpenGraphReader.fetch("http://examples.opengraphprotocol.us/article.html")

# Raises if anything on the object is invalid
object = OpenGraphReader.fetch!("http://examples.opengraphprotocol.us/article.html")

# Read from string
object = OpenGraphReader.parse(html)
object = OpenGraphReader.parse!(html)

# Access by full property name
object.og.title #=> "5 Held in Plot to Bug Office"

# Optional properties can return nil
object.og.description #=> nil

# Supports properties that are objects themselves
object.og.image.to_s    #=> "http://examples.opengraphprotocol.us/media/images/50.png"
object.og.image.content #=> "http://examples.opengraphprotocol.us/media/images/50.png"
object.og.image.url     #=> "https://examples.opengraphprotocol.us/media/images/50.png"
object.og.image.width   #=> 50

# Supports arrays
object.og.images.first == object.og.image #=> true
object.article.tags #=> ["Watergate"]

# Custom namespace
class MyNamespace
  include OpenGraphReader::Object

  namespace :my, :namespace # my:namespace
  string :name, required: true # my:namespace:name
  url :url, default: "http://example.org/my_namespace"
  integer :pages, collection: true
  # See the shipped definitions for more examples
end
```

[Documentation](http://rubydoc.info/gems/open_graph_reader)

## Contributing

1. Fork it ( https://github.com/jhass/open_graph_reader/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request