module TwitterCards
Constants
- VERSION
Public Class Methods
Extracts Twitter Cards data from a parsed document.
@param uri [Nokogiri::HTML::Document] the nokogiri parsed document extract cards from. @param opts [Hash] options to customize functionality @option :strict [Boolean] If set true, the object is retured only if the data are valid (i.e. not missing required attributes) @return [Hashie Object
, false] a TwitterCards::Object
if there is data to be found or false otherwise.
# File lib/twitter_cards.rb, line 40 def self.extract(doc, opts = {}) opts[:strict] ||= false page = TwitterCards::Object.new doc.css('meta').each do |m| if m.attribute('name') and m.attribute('name').to_s.match(/^twitter:(.+)$/i) property = $1 elsif m.attribute('property') and m.attribute('property').to_s.match(/^twitter:(.+)$/i) property = $1 end if property property.gsub!(/[-:]/,'_') page[property] = m.attribute('content').to_s unless page[property] end end return false if page.keys.empty? return false unless page.valid? if opts[:strict] page end
Fetch Twitter Cards data from the specified URI with a HTTP GET request.
@param uri [String] the uri to fetch and extract cards from. @param opts [Hash] options to customize functionality @option :strict [Boolean] If set true, the object is retured only if the data are valid (i.e. not missing required attributes). @return [Hashie Object
, false] a TwitterCards::Object
if there is data to be found or false otherwise.
# File lib/twitter_cards.rb, line 15 def self.fetch(uri, opts = {}) parse(RestClient.get(uri).body, opts) rescue RestClient::Exception, SocketError false end
Parses HTML and extracts Twitter Cards data.
@param uri [String] the html text to parse and extract cards from. @param opts [Hash] options to customize functionality @option :strict [Boolean] If set true, the object is retured only if the data are valid (i.e. not missing required attributes) @return [Hashie Object
, false] a TwitterCards::Object
if there is data to be found or false otherwise.
# File lib/twitter_cards.rb, line 28 def self.parse(html, opts = {}) doc = Nokogiri::HTML.parse(html) extract(doc, opts) end