OpengraphParser

OpengraphParser is a simple Ruby library for parsing Open Graph protocol information from a website. Learn more about the protocol at: http://ogp.me

Installation

  gem install opengraph_parser

or add to Gemfile

  gem "opengraph_parser"

Usage

Parsing an URL

og = OpenGraph.new("http://ogp.me")
og.title # => "Open Graph protocol"
og.type # => "website"
og.url # => "http://ogp.me/"
og.description # => "The Open Graph protocol enables any web page to become a rich object in a social graph."
og.images # => ["http://ogp.me/logo.png"]

You can also get other Open Graph metadata as:

og. # => {"og:image:type"=>"image/png", "og:image:width"=>"300", "og:image:height"=>"300"}

Parsing a HTML document

og = OpenGraph.new(html_string)

Custom header fields

In some cases you may need to change fields in HTTP request header for an URL ruby og = OpenGraph.new("http://opg.me", { :headers => {'User-Agent' => 'Custom User Agent'} })

Fallback

If you try to parse Open Graph information for a website that doesn’t have any Open Graph metadata, the library will try to find other information in the website as the following rules:

for title <meta name="description"> for description <link rel="image_src"> or all <img> tags for images</p> <p>You can disable this fallback lookup by passing false to init method:</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_og'>og</span> <span class='op'>=</span> <span class='const'>OpenGraph</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>http://ogp.me</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span> </code></pre> <h2>Contributing to opengraph_parser</h2> <ul> <li>Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.</li> <li>Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.</li> <li>Fork the project.</li> <li>Start a feature/bugfix branch.</li> <li>Commit and push until you are happy with your contribution.</li> <li>Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.</li> <li>Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.</li> </ul> <h2>Copyright</h2> <p>Copyright (c) 2013 Huy Ha. See LICENSE.txt for further details.</p> </div></div> <div id="footer"> Generated on Mon Jan 26 22:37:40 2015 by <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a> 0.8.7.6 (ruby-2.1.4). </div> <script src="http://static.getclicky.com/js" type="text/javascript"></script> <script type="text/javascript">clicky.init(246206);</script> <noscript><p><img alt="Clicky" width="1" height="1" src="http://static.getclicky.com/246206ns.gif" /></p></noscript> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-7172246-5', 'auto'); ga('send', 'pageview'); </script> </div> </body> </html>