class WeddingRegistryScraper::Registry
Constants
- PRICE_TYPES
Attributes
display_name[R]
domain[R]
Public Class Methods
new(url, params={})
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 15 def initialize(url, params={}) params.symbolize_keys! @url = url @debug = params[:debug] == true end
Public Instance Methods
get_items()
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 21 def get_items doc = get_registry get_products(doc).reduce({}) do |products, product| sku = get_sku(product) details = { :name => get_name(product), :remaining => get_remaining(product), :desired => get_desired(product), :url => get_url(product), :image_url => get_image_url(product), :registry_name => self.class.display_name, :fulfilled => fulfilled?(product), :price_type => price_type(product), :price_value => get_price(product), } products.merge! sku => details end end
Private Instance Methods
fulfilled?(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 59 def fulfilled?(product) get_remaining(product) == 0 end
get_desired(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 52 def get_desired(product) ; raise NotImplementedError; end
get_image_url(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 50 def get_image_url(product) ; raise NotImplementedError; end
get_name(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 47 def get_name(product) ; raise NotImplementedError; end
get_price(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 53 def get_price(product) ; raise NotImplementedError; end
get_products(doc)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 45 def get_products(doc) ; raise NotImplementedError; end
get_registry()
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 63 def get_registry result, _ = make_request(:get, @url) Nokogiri::HTML(result.body) end
get_remaining(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 51 def get_remaining(product) ; raise NotImplementedError; end
get_sku(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 48 def get_sku(product) ; raise NotImplementedError; end
get_url(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 49 def get_url(product) ; raise NotImplementedError; end
make_request(method, url, params={})
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 68 def make_request(method, url, params={}) json = params.delete(:json) request_params = {} if cookies = params.delete(:cookies) request_params[:headers] = { 'Cookie' => dump_cookies(cookies) } end if json request_params[:headers] = { 'Accept' => "application/json" } end request_params[:parameters] = json ? params.to_json : params puts "#{method.to_s.upcase} #{url} with params #{request_params.to_json}" if @debug result = Unirest.send(method, url, request_params) puts "RESULT #{result.code} with headers #{result.headers.inspect}" if @debug open_result_in_browser(result) if @debug cookies = load_cookies(result.headers[:set_cookie]) [ result, cookies ] end
open_result_in_browser(result)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 98 def open_result_in_browser(result) file = Tempfile.new(['weddding-scraper','.html']) file << result.body file.close `open "#{file.path}"` sleep 1 file.unlink end
price_type(product)
click to toggle source
# File lib/wedding_registry_scraper/registry.rb, line 55 def price_type(product) FIXED_PRICE end