Class: Tyt::Bachelor

Inherits:
Object
  • Object
show all
Defined in:
lib/tyt/bachelor.rb

Constant Summary collapse

DEFAULT_SEASON =
'13-14'
MTB_ENDPOINT =
"http://track.mtbachelor.com/mobile/tyt.asp"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bachelor

Returns a new instance of Bachelor.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
# File 'lib/tyt/bachelor.rb', line 9

def initialize(options={})
  raise(ArgumentError, "pass is required!") if !options[:pass] || !options[:pass].instance_of?(String)
  raise(ArgumentError, "Invalid Season Format!") if options[:season] && !(options[:season] =~ /\d{2}\-\d{2}/)
  @pass = options[:pass]
  @season = options[:season] ? options[:season] : DEFAULT_SEASON
  @currentday = nil
  @mtb_endpoint = MTB_ENDPOINT
end

Instance Attribute Details

#currentdayObject

Returns the value of attribute currentday.



8
9
10
# File 'lib/tyt/bachelor.rb', line 8

def currentday
  @currentday
end

#docObject

Returns the value of attribute doc.



8
9
10
# File 'lib/tyt/bachelor.rb', line 8

def doc
  @doc
end

#mtb_endpointObject

Returns the value of attribute mtb_endpoint.



8
9
10
# File 'lib/tyt/bachelor.rb', line 8

def mtb_endpoint
  @mtb_endpoint
end

#passObject

Returns the value of attribute pass.



8
9
10
# File 'lib/tyt/bachelor.rb', line 8

def pass
  @pass
end

#seasonObject

Returns the value of attribute season.



8
9
10
# File 'lib/tyt/bachelor.rb', line 8

def season
  @season
end

Instance Method Details

#data_endpointObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tyt/bachelor.rb', line 60

def data_endpoint
  args = [
    "passmediacode=#{@pass}",
    "season=#{@season}"
  ]
  if @currentday
    args << "currentday=#{@currentday}"
  else
    args << "currentday=null"
  end
  return [@mtb_endpoint,args.join('&')].join('?')
end

#date_data(date) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tyt/bachelor.rb', line 39

def date_data(date)
  begin
    @currentday = date.strftime('%m/%d/%Y')
    get_doc
    ride_rows = doc.css('td').select{|n| n.text =~ /\d{1,2}\/\d{1,2}\/\d{4}\s{1}\d{1,2}\:\d{2}\:\d{2}/ }.collect{|a| a.parent() }
    ride_data = []
    ride_rows.each do |row|
      cells = row.children.css('td')
      date = cells[0] ? DateTime.strptime(cells[0].text,'%m/%d/%Y %l:%M:%S %p') : nil
      chair = cells[1] ? cells[1].text : nil
      vertical_feet = cells[2] ? cells[2].text : nil
      vertical_meters = cells[3] ? cells[3].text : nil
      run_detail = Tyt::RunDetail.new(datetime: date, chair: chair, vertical_feet: vertical_feet, vertical_meters: vertical_meters)
      ride_data << run_detail
    end
    return ride_data
  rescue OpenURI::HTTPError
    return nil
  end
end

#get_docObject



18
19
20
# File 'lib/tyt/bachelor.rb', line 18

def get_doc
  @doc = Nokogiri::HTML(open(data_endpoint))
end

#season_dataObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tyt/bachelor.rb', line 22

def season_data
  @currentday = nil
  get_doc
  date_rows = doc.css('a[href]').select{|e| e['href'] =~ /currentday\=\d{2}\/\d{2}\/\d{4}/ }.collect{|a| a.parent().parent() }
  season = Tyt::Season.new
  date_rows.each do |row|
    cells = row.children.css('td')
    date = cells[0] ? Date.strptime(cells[0].text, '%m/%d/%Y') : nil
    runs = cells[1] ? cells[1].text : nil
    vertical_feet = cells[2] ? cells[2].text : nil
    vertical_meters = cells[3] ? cells[3].text : nil
    ski_day = Tyt::SkiDay.new(date: date, runs: runs, vertical_feet: vertical_feet, vertical_meters: vertical_meters)
    season.days << ski_day
  end
  return season
end