Class: Robads

Inherits:
Object
  • Object
show all
Defined in:
lib/robads/base.rb

Defined Under Namespace

Classes: Object

Class Method Summary collapse

Class Method Details

.fetch_json(uri) ⇒ Object

Fetch Robads JSON file from the specified URI. Makes an HTTP GET request and returns an Robads::Object if there is data to be found or false if there isn’t.

Pass false for the second argument if you want to see invalid (i.e. missing a required attribute) data.



13
14
15
16
17
# File 'lib/robads/base.rb', line 13

def self.fetch_json(uri)
  JSON.parse RestClient.get(uri).body
rescue RestClient::Exception, SocketError
  false
end

.fetch_meta(uri, strict = true) ⇒ Object

HTTP GET request and returns an Robads::Object if there is data to be found or false if there isn’t.

Pass false for the second argument if you want to see invalid (i.e. missing a required attribute) data.



25
26
27
28
29
# File 'lib/robads/base.rb', line 25

def self.fetch_meta(uri, strict = true)
  parse(RestClient.get(uri).body, strict)
rescue RestClient::Exception, SocketError
  false
end

.parse(html, strict = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/robads/base.rb', line 31

def self.parse(html, strict = false)
  doc = Nokogiri::HTML.parse(html)
  page = Robads::Object.new
  doc.css('meta').each do |m|
    if m.attribute('name') && m.attribute('name').to_s.match(/^robads:(.+)$/i)
      page[$1.gsub('-','_')] = m.attribute('content').to_s
    end
  end
  return false if page.keys.empty?
  return false unless page.valid? if strict
  page
end