class Goodwill::Auction

A ShopGoodwill Auction

Attributes

bids[R]
current[R]
end[R]
href[R]
item[R]
item_page[R]
itemid[R]
seller[R]
shipping[R]
type[R]

Public Class Methods

new(itemid, zipcode = '97222', state = 'OR', country = 'US') click to toggle source
# File lib/goodwill/auction.rb, line 20
def initialize(itemid, zipcode = '97222', state = 'OR', country = 'US')
  @itemid = itemid
  @zipcode = zipcode
  @state = state
  @country = country

  @href = ITEM_SEARCH_URL + itemid.to_s
  @item_page = mechanize.get(@href)

  @type = parse_type
  @bids = item_page.search(BIDS_PATH).text[/\d+/].to_i
  @current = item_page.search(CURRENT_PRICE_PATH).text.tr('$', '').to_f
  @end = parse_end_time
  @item = item_page.search(ITEM_TITLE_PATH).text
  @seller = item_page.search(SELLER_PATH).text
  @bidding = false
  @shipping = calculate_shipping(@itemid, @zipcode, @country)
end

Public Instance Methods

==(other) click to toggle source
# File lib/goodwill/auction.rb, line 39
def ==(other)
  itemid == other.itemid
end
to_hash() click to toggle source
# File lib/goodwill/auction.rb, line 43
def to_hash
  hash = {}
  instance_variables.each do |var|
    next if var == :@item_page

    hash[var.to_s.delete('@')] = if var == :@end
                                   instance_variable_get(var).to_s
                                 else
                                   instance_variable_get(var)
                                 end
  end
  hash
end

Private Instance Methods

calculate_shipping(itemid, zipcode, country) click to toggle source
# File lib/goodwill/auction.rb, line 59
def calculate_shipping(itemid, zipcode, country)
  params = "?ZipCode=#{zipcode}&Country=#{country}&ItemId=#{itemid}&Quantity=1&_=#{DateTime.now.strftime('%s')}"
  page = mechanize.get(SHIPPING_URL + params)
  page.search(SHIPPING_PATH).text.split(': ').last.tr('$', '').to_f
rescue StandardError
  puts "Timeout (#{e}), retrying in 1 second..."
  retry
end
parse_end_time() click to toggle source
# File lib/goodwill/auction.rb, line 68
def parse_end_time
  search_path = type == :auction ? END_TIME_PATH : BIN_END_TIME_PATH
  time = item_page.search(search_path).text.split(': ')[1]
  return nil if time.nil?

  DateTime.strptime(time, '%m/%d/%Y %l:%M:%S %p %Z')
rescue Date::Error
  nil
end
parse_type() click to toggle source
# File lib/goodwill/auction.rb, line 78
def parse_type
  item_page.search(BUY_IT_NOW_PATH).empty? ? :auction : :buyitnow
end