Module: OpenGraph::Parser
- Defined in:
- lib/kog/parser.rb
Constant Summary collapse
- REQUIRED_ATTRIBUTES =
Attributes that must be present according to the protocol
['image', 'title', 'type', 'url']
Class Method Summary collapse
-
.parse(url) ⇒ Object
Parse.
Class Method Details
.parse(url) ⇒ Object
Parse
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kog/parser.rb', line 10 def self.parse(url) # Get the document begin body = HTTParty.get(url).body doc = Nokogiri::HTML.parse(body) rescue HTTParty::ResponseError, HTTParty::UnsupportedURIScheme, HTTParty::UnsupportedFormat, URI::InvalidURIError, SocketError return nil end # Initialize the temporary hash type = Hash.new # Parse the document and append to the temporary hash doc.css('meta').each do || if !.attribute('property').nil? && .attribute('property').to_s.match(/^og:(.+)$/i) type[$1.gsub('-','_')] = .attribute('content').to_s end end # None of the required attributes were found REQUIRED_ATTRIBUTES.each { |key| return nil if !type.has_key?(key) } # Return the representative OpenGraph object return OpenGraph::Object.new(type) end |