Module: SDL::Util::NokogiriUtils
- Defined in:
- lib/sdl/util/nokogiri.rb
Instance Method Summary collapse
- #fetch_from_io(io, base_url, *search) ⇒ Object
-
#fetch_from_url(url, *search) ⇒ Object
Fetches an Nokogiri::XML::NodeSet from the webpage at url by performing *search.
-
#process_result(result, base_url) ⇒ Object
It converts all relative href URLs to absolute URLs and adds a ‘target’ attribute to all links, so that they open in a new browser window and not the current broker.
Instance Method Details
#fetch_from_io(io, base_url, *search) ⇒ Object
19 20 21 |
# File 'lib/sdl/util/nokogiri.rb', line 19 def fetch_from_io(io, base_url, *search) process_result Nokogiri::HTML(io).search(*search), base_url end |
#fetch_from_url(url, *search) ⇒ Object
Fetches an Nokogiri::XML::NodeSet from the webpage at url by performing *search
11 12 13 14 15 16 17 |
# File 'lib/sdl/util/nokogiri.rb', line 11 def fetch_from_url(url, *search) begin fetch_from_io open(url), url, *search rescue SocketError => e [] end end |
#process_result(result, base_url) ⇒ Object
It converts all relative href URLs to absolute URLs and adds a ‘target’ attribute to all links, so that they open in a new browser window and not the current broker.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sdl/util/nokogiri.rb', line 25 def process_result(result, base_url) result.search('//body//@href').each do |attribute| begin attribute.content = URI.join(base_url, attribute.value.gsub(/\s/, '')).to_s rescue URI::InvalidURIError next end if attribute.parent.name.eql? 'a' attribute.parent['target'] = '_new' end end result end |