Helper of OpenGraph
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib Bump version to 0.3.0 Jul 3, 2017
.gitignore ignore .DS_Store May 13, 2013
CHANGELOG.markdown Update ChangeLog Jul 3, 2017
Gemfile
README.markdown
Rakefile extract open_graph_helper as module Oct 20, 2011
open_graph_helper.gemspec extract open_graph_helper as module Oct 20, 2011

README.markdown

Open Graph Helper

Common Open Graph meta tag and Facebook Social Plugin tag helpers.

There are only small subsets of tags are supported (for now).

Pull requests are welcome.

The Facebook Social Plugins can be rendered in HTML5 version (default) or <iframe> version. Their default options are tuned for Techbang. Override them by passing a Hash; see below.

Usage

Add this to your Gemfile:

gem 'open_graph_helper'

If you want to use the edge version on Github, specify the :git option.

gem 'open_graph_helper', github: 'techbang/open_graph_helper'

And run

bundle install

to install this plug-in.

For Social Plugins, OpenGraphHelper provides 2 versions for that: the new :html5 and legacy :iframe.

:html5 version is the default option and requires JavaScript SDK. See Facebook JavaScript SDK for more details. If you're using Facebooker2, please include the SDK by <%= fb_connect_async_js %>.

To switch to :iframe version, you need an initializer. Name it, say, open_graph_helper.rb, with one line of content:

OpenGraphHelper::SOCIAL_PLUGIN_VERSION = :iframe # HTML5 + JS SDK doesn't need this

The last thing: don't forget to add XML namespaces to the <html> tag, or <meta> tags won't work.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:og="http://ogp.me/ns#"
      xmlns:fb="http://www.facebook.com/2008/fbml">

Open Graph meta tags

Examples: (Sample output from Facebook Open Graph Documentation)

og_title("The Rock")
#=> <meta property="og:title" content="The Rock"/>

og_type("movie") # default is "article"
#=> <meta property="og:type" content="movie"/>

og_url("http://www.imdb.com/title/tt0117500/")
#=> <meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>

og_image("http://ia.media-imdb.com/rock.jpg")
#=> <meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>

og_site_name("IMDb")
#=> <meta property="og:site_name" content="IMDb"/>

og_description("Lorem Ipsum ...")
#=> <meta property="og:description" cotent="Lorem Ipsum ..."/>

og_fb_admins(%w(123456 789012)) # Accepts an Array for multiple admins
#=> <meta property="fb:admins" content="123456"/>
#   <meta property="fb:admins" content="789012"/>

og_fb_app_id("1234567890")
#=> <meta property="fb:app_id" content="1234567890"/>

Note on Multiple-Value (e.g. fb:admins)

According to Open Graph Protocol, multiple-value should be represented as an Array, not separated by a single comma , (U+002C).

This pitfall was found when assigning admins for Facebook Comments Plug-In. Although the document says:

To add multiple moderators, separate the uids by comma without spaces.

It does actually not working, and a parser error will be warned by Open Graph Debugger. A working example should like this:

<meta property="fb:admins" content="123456">
<meta property="fb:admins" content="789012">

Therefore, OpenGraphHelper's og_fb_admins accepts an array, in which situation it maps each element to a single fb:admins meta tag, and concatenates them in a single line.

See this QA: http://stackoverflow.com/a/12007788

Facebook Social Plugins

There are some default options that was optimized for Techbang sites and may not suits for your needs. Override them by passing a Hash as the last argument.

Like Button

Usage: fb_like(like_url, options={})

The default options are:

layout: "button_count",
action: "like",
share: false,
show_faces: false,
width: 90

For more options, see Facebook Like Button Documentation

Example:

fb_like("http://example.com")
#=> <div class="fb-like" data-href="http://example.com" data-layout="button_count" data-action="like" data-share="false" data-show-faces="false" data-width="90"></div>

fb_like("http://example.com", width: 120, layout: "standard")
#=> <div class="fb-like" data-href="http://example.com" data-layout="standard" data-action="like" data-share="false" data-show-faces="false" data-width="120"></div>

Page

Usage: fb_page(fans_page_url, options={})

The default options are:

small_header: true,
adapt_container_width: true,
hide_cover: false,
show_facepile: false

For more options, see Facebook Page Plugin Documentation

Example:

fb_page("https://www.facebook.com/facebook")
#=> <div class="fb-page" data-href="https://www.facebook.com/facebook" data-small-header="true", data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="false"><blockquote cite="https://www.facebook.com/facebook" class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/facebook">Facebook</a></blockquote></div>

fb_page("https://www.facebook.com/facebook", small_header: false, width: 200, tabs: "timeline", adapt_container_width: false)
#=> <div class="fb-page" data-href="https://www.facebook.com/facebook" data-small-header="false" data-width="200" data-tabs="timeline" data-adapt-container-width="false" data-hide-cover="false" data-show-facepile="false"><blockquote cite="https://www.facebook.com/facebook" class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/facebook">Facebook</a></blockquote></div>

License

This software is released under the MIT License

Copyright (c) 2011- Techbang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.