class Unobtainium::Nokogiri::Driver
Driver
implementation using nokogiri (and open-uri).
Attributes
content[R]
@return (String) The raw page content, or nil
meta[R]
@return (Hash) Any page metadata, or an empty Hash
options[RW]
@return (::Nokogiri::XML::ParseOptions) parser options for all subsequent
requests.
parse_method[R]
@return (Symbol) one of :HTML, :XML
Public Class Methods
create(_, _)
click to toggle source
Create and return a driver instance
# File lib/unobtainium-nokogiri/driver.rb, line 41 def create(_, _) driver = ::Unobtainium::Nokogiri::Driver.new return driver end
ensure_preconditions(_, _)
click to toggle source
Ensure that the driver's preconditions are fulfilled.
# File lib/unobtainium-nokogiri/driver.rb, line 30 def ensure_preconditions(_, _) require 'nokogiri' require 'open-uri' rescue LoadError => err raise LoadError, "#{err.message}: you need to add "\ "'nokogiri' to your Gemfile to use this driver!", err.backtrace end
matches?(label)
click to toggle source
Return true if the given label matches this driver implementation, false otherwise.
# File lib/unobtainium-nokogiri/driver.rb, line 24 def matches?(label) return :nokogiri == label.to_sym end
new()
click to toggle source
Private initialize to force use of Driver#create.
# File lib/unobtainium-nokogiri/driver.rb, line 123 def initialize reset @parse_method = :HTML @options = ::Nokogiri::XML::ParseOptions::DEFAULT_HTML end
Public Instance Methods
goto(uri)
click to toggle source
“Go to” the given URI, i.e. open it and retain contents.
# File lib/unobtainium-nokogiri/driver.rb, line 49 def goto(uri) reset # Fetch content open(uri) do |f| @meta[:uri] = uri.dup if f.respond_to?(:meta) @meta[:headers] = f.meta.dup @meta[:status] = f.status.dup @meta[:base_uri] = f.base_uri.dup end if f.respond_to?(:metas) @meta[:split_headers] = f.metas.dup end @content = f.read end # Pass content to Nokigiri @parsed = ::Nokogiri.send(@parse_method, @content) do |config| config.options = @options end end
method_missing(meth, *args, &block)
click to toggle source
Map any missing method to nokogiri
Calls superclass method
# File lib/unobtainium-nokogiri/driver.rb, line 112 def method_missing(meth, *args, &block) if not @parsed.nil? and @parsed.respond_to?(meth) return @parsed.send(meth.to_s, *args, &block) end return super end
parse_method=(method)
click to toggle source
Set one of :XML or :HTML, deciding which parsing method nokogiri is to use.
# File lib/unobtainium-nokogiri/driver.rb, line 90 def parse_method=(method) if not [:XML, :HTML].include?(method) raise ArgumentError, "parse_method must be one of :XML, :HTML!" end @parse_method = method end
respond_to_missing?(meth, include_private = false)
click to toggle source
Map any missing method to nokogiri
Calls superclass method
# File lib/unobtainium-nokogiri/driver.rb, line 103 def respond_to_missing?(meth, include_private = false) if not @parsed.nil? and @parsed.respond_to?(meth, include_private) return true end return super end
Private Instance Methods
reset()
click to toggle source
Clear all data
# File lib/unobtainium-nokogiri/driver.rb, line 132 def reset @content = nil @meta = {} @parsed = nil end