Skip to content

Instantly share code, notes, and snippets.

@lukas2
Created January 26, 2013 14:22
Show Gist options
  • Save lukas2/4642666 to your computer and use it in GitHub Desktop.
Save lukas2/4642666 to your computer and use it in GitHub Desktop.
Just a crappy little Rails-helper for Twitter cards and Open Graph metadata.
require 'rubygems'
require 'action_view'
require 'erb'
class MetadataHelper
include ActionView::Helpers::TagHelper
def initialize( opts = {} )
@title = opts[:title]
@url = opts[:url]
@description = opts[:description]
@image_url = opts[:image_url] || opts[:image]
@author = opts[:author]
@type = opts[:type]
end
def twitter_card
Array.new.tap do |arr|
arr << mtag( 'twitter:card', 'summary' )
arr << mtag( 'twitter:url', @url )
arr << mtag( 'twitter:title', @title )
arr << mtag( 'twitter:description', @description )
arr << mtag( 'twitter:image', @image_url )
arr << mtag( 'twitter:author', @author )
end.compact
end
def open_graph
Array.new.tap do |arr|
arr << mtag( 'og:type', @type )
arr << mtag( 'og:url', @url )
arr << mtag( 'og:title', @title )
arr << mtag( 'og:description', @description )
arr << mtag( 'og:image', @image_url )
end.compact
end
def all
[ twitter_card, open_graph ].flatten.join( "\n" )
end
private
def mtag( name, content )
tag( :meta, { name: name, content: content }, false, false ) if name && content
end
end
template = ERB.new <<EOS
<!DOCTYPE HTML>
<html>
<head>
<%= helper.all %>
</head>
<body>
<p>Hello people!</p>
</body>
</html>
EOS
helper = MetadataHelper.new( {
title: 'Test',
url: 'http://www.lukaszielinski.de',
description: 'Just testing',
type: 'article',
author: '@lukaszielinski',
image: 'http://mugs.mugbug.co.uk/500/mb.i_love_html_red_love_heart.coaster.jpg'
} )
puts template.result( binding )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment