class Storexplore::WalkerPage
Wrapper around Mechanize::Page providing strict one liners to select elements.
Public Class Methods
new(mechanize_page)
click to toggle source
# File lib/storexplore/walker_page.rb, line 73 def initialize(mechanize_page) @mechanize_page = mechanize_page end
open(agent, uri)
click to toggle source
A new lazy proxy on the page. (Internal Usage)
# File lib/storexplore/walker_page.rb, line 30 def self.open(agent, uri) Getter.new(agent, uri) end
Public Instance Methods
get_all(selector, separator)
click to toggle source
String with the text of all matching elements, separated by separator.
# File lib/storexplore/walker_page.rb, line 57 def get_all(selector, separator) elements = @mechanize_page.search(selector) throw_if_empty(elements, "elements", selector) (elements.map &:text).join(separator) end
get_image(selector)
click to toggle source
Unique image matching selector Throws Storexplore::WalkerPageError
if there is not exactly one matching image
# File lib/storexplore/walker_page.rb, line 67 def get_image(selector) first_or_throw(@mechanize_page.images_with(search: selector), "images", selector) end
get_one(selector)
click to toggle source
Unique element matching selector Throws Storexplore::WalkerPageError
if there is not exactly one matching element
# File lib/storexplore/walker_page.rb, line 52 def get_one(selector) first_or_throw(@mechanize_page.search(selector), "elements", selector) end
search_links(selector)
click to toggle source
Collection of proxies on pages accessible through the matching links (Internal Usage)
# File lib/storexplore/walker_page.rb, line 39 def search_links(selector) uri2links = {} search_all_links(selector).each do |link| target_uri = link.uri uri2links[target_uri.to_s] = link if same_domain? uri, target_uri end # enforcing deterministicity for testing and debugging uri2links.values.sort_by {|link| link.uri.to_s } end
Private Instance Methods
first_or_throw(elements, name, selector)
click to toggle source
# File lib/storexplore/walker_page.rb, line 85 def first_or_throw(elements, name, selector) throw_if_empty(elements, name, selector) elements.first end
same_domain?(source_uri, target_uri)
click to toggle source
# File lib/storexplore/walker_page.rb, line 77 def same_domain?(source_uri, target_uri) target_uri.relative? || (UriUtils.domain(source_uri) == UriUtils.domain(target_uri)) end
search_all_links(selector)
click to toggle source
# File lib/storexplore/walker_page.rb, line 81 def search_all_links(selector) @mechanize_page.links_with(search: selector).map { |link| Link.new(link) } end
throw_if_empty(elements, name, selector)
click to toggle source
# File lib/storexplore/walker_page.rb, line 90 def throw_if_empty(elements, name, selector) if elements.empty? raise WalkerPageError.new("Page \"#{uri}\" does not contain any #{name} like \"#{selector}\"") end end