Class: Beway::EbayData

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/beway/ebay_data.rb

Overview

EbayData

Singleton class to handle ebay queries that are not auction-related.

Constant Summary collapse

EBAY_OFFICIAL_TIME_URL =
'http://viv.ebay.com/ws/eBayISAPI.dll?EbayTime'

Instance Method Summary collapse

Constructor Details

#initializeEbayData

Returns a new instance of EbayData.



18
19
20
21
# File 'lib/beway/ebay_data.rb', line 18

def initialize
  @time_offset = nil
  @last_time_offset = nil
end

Instance Method Details

#calc_time_offsetObject

Calculate the ebay time offset



37
38
39
40
# File 'lib/beway/ebay_data.rb', line 37

def calc_time_offset
  @last_time_offset = Time.now
  @time_offset = official_time - Time.now.localtime 
end

#official_timeObject

Retrieve the official ebay time

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/beway/ebay_data.rb', line 43

def official_time
  doc = Nokogiri::HTML(open(EBAY_OFFICIAL_TIME_URL))

  time_label = doc.at_xpath('//p[contains(text(), "The official eBay Time is now:")]')

  raise EbayDataParseError, "Couldn't find time label" unless time_label

  time_node = time_label.next_sibling.next_sibling
  raise EbayDataParseError, "Couldn't find time node" unless time_node

  time_str = time_node.inner_text
  time_re = /(Sun|Mon|Tues|Wednes|Thurs|Fri|Satur)day, (January|February|March|April|May|June|July|August|September|October|November|December) \d\d, 20\d\d \d\d:\d\d:\d\d P[SD]T/
  raise EbayDataParseError, "Time in unexpected format: #{time_str}" unless time_re.match(time_str)

  Time.parse(time_str).localtime
end

#seconds_to(some_ebay_time) ⇒ Object

Returns the number of seconds to some_ebay_time



61
62
63
# File 'lib/beway/ebay_data.rb', line 61

def seconds_to(some_ebay_time)
  some_ebay_time - time
end

#timeObject

The current ebay time as calculated by an offset from localtime.



24
25
26
# File 'lib/beway/ebay_data.rb', line 24

def time
  Time.now.localtime + self.time_offset
end

#time_offsetObject

The localtime offset from ebay time.

add this offset to localtime to get an estimated ebay time



31
32
33
34
# File 'lib/beway/ebay_data.rb', line 31

def time_offset
  calc_time_offset unless @time_offset
  @time_offset
end