Ruby Open Graph Example

The following Ruby example shows how a user might use Opengraph.io to get a website’s title, description, and icon given a URL. This example makes use of ExpressJS to proxy the request from a web user onto the OpenGraph.io API. This approach is often more appropriate in applications than sending traffic directly to OpenGraph.io’s server in that you have the ability to hide your API token and you can also perform any pre/post processing on the server rather than bogging down the client’s browser. The need for a function like this is very common when creating an application that posts content onto social media sites such as Facebook (see screenshot to the right) or Linkedin where the user wants to control how the link is displayed. If you would like more examples of how to use this the OpenGraph API, please check out the README for our Ruby module.

Open Graph Results

Ruby Code

require 'opengraph-io'

opengraph = OpenGraphIO.new({app_id: '!!!!YOUR_APP_ID!!!'})

ogData = opengraph.get_site_info('https://github.com')

siteDescription = ogData["hybridGraph"]["description"]

print siteDescription

check

To get the code above working, create a new directory and change into that directory from the command line (Linux or OSX)

mkdir example & cd example

check

Next, install the Ruby opengraph-io package

gem install -user-install opengraph-io

check

Finally, run the script which outputs the hybrid description of the site as gathered by OpenGraph.io

ruby test.rb