Sha256: 65e71661fa15e7de6351572545c18c471eb1275d2e783d14b20c547c8c8a2657
Contents?: true
Size: 1.05 KB
Versions: 2
Compression:
Stored size: 1.05 KB
Contents
require 'nokogiri' require 'open-uri' require 'uri' require_relative './user_agents.rb' module XP def to_nokogiri source = url? ? self.page_source : self Nokogiri(source) end def page_source(user_agent_alias: :mac_firefox, user_agent: nil) user_agent ||= USER_AGENTS[user_agent_alias] open(self, "User-Agent" => user_agent).read end def download(location: 'downloads', name: nil) FileUtils.mkdir_p location filename = (name || basename).to_s + extension File.open("#{location}/#{filename}", 'wb') do |f| f.write open(self).read end end def css(selector) self.to_nokogiri.css(selector).to_html.to_nokogiri end def xpath(selector) self.to_nokogiri.xpath(selector).to_html.to_nokogiri end private def url? self.length < 200 && !(self =~ /\A#{URI::regexp}\z/).nil? end def basename self.match(regex)[:basename] if url? end def extension self.match(regex)[:extension] if url? end def regex /.*\/(?<basename>.*)(?<extension>\.\w+)(\?.*)?/ end end String.send :include, XP
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
xp-2.0.1 | lib/xp.rb |
xp-2.0.0 | lib/xp.rb |