Class: DTVTournaments::Tournament

Inherits:
Object
  • Object
show all
Defined in:
lib/dtv_tournaments/tournament.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, shouldCall = true) ⇒ Tournament

Returns a new instance of Tournament.



7
8
9
10
# File 'lib/dtv_tournaments/tournament.rb', line 7

def initialize number, shouldCall=true
  @number = number
  call if shouldCall
end

Instance Attribute Details

#cityObject

Returns the value of attribute city.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def city
  @city
end

#dateObject

Returns the value of attribute date.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def date
  @date
end

#datetimeObject

Returns the value of attribute datetime.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def datetime
  @datetime
end

#kindObject

Returns the value of attribute kind.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def kind
  @kind
end

#notesObject

Returns the value of attribute notes.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def notes
  @notes
end

#numberObject

Returns the value of attribute number.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def number
  @number
end

#pageObject

Returns the value of attribute page.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def page
  @page
end

#streetObject

Returns the value of attribute street.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def street
  @street
end

#timeObject

Returns the value of attribute time.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def time
  @time
end

#zipObject

Returns the value of attribute zip.



5
6
7
# File 'lib/dtv_tournaments/tournament.rb', line 5

def zip
  @zip
end

Class Method Details

.deserialize(a) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/dtv_tournaments/tournament.rb', line 103

def self.deserialize(a)
  t = Tournament.new(a[0].to_i, false)
  t.notes = a[1]
  t.timeFromString(a[2])
  t.street = a[3]
  t.zip = a[4]
  t.city = a[5]
  t.kind = a[6]
  t
end

Instance Method Details

#callObject



12
13
14
15
16
# File 'lib/dtv_tournaments/tournament.rb', line 12

def call
  get_result_page
  extract_results
  save_to_cache
end

#extract_dateObject



66
67
68
# File 'lib/dtv_tournaments/tournament.rb', line 66

def extract_date
  page.search(".kategorie").text.scan(/^\d{1,2}.\d{1,2}.\d{4}/).first
end

#extract_kindObject



43
44
45
# File 'lib/dtv_tournaments/tournament.rb', line 43

def extract_kind
  @kind = DTVTournaments::convertToText(page.search(".markierung .turnier"))
end

#extract_locationObject



51
52
53
54
55
# File 'lib/dtv_tournaments/tournament.rb', line 51

def extract_location
  @zip = @page.search('.ort strong').text.gsub!(/\D|\s/, "").to_i
  @city = @page.search('.ort strong').text.gsub!(/\d|\s/, "")
  @street = @page.search('.ort').to_html.split('<br>')[1].strip
end

#extract_notesObject



47
48
49
# File 'lib/dtv_tournaments/tournament.rb', line 47

def extract_notes
  @notes = page.search(".turniere tr .bemerkung").to_a.collect(&:text).reject{|x| x.nil? || x.empty?}.join("\n")
end

#extract_resultsObject



36
37
38
39
40
41
# File 'lib/dtv_tournaments/tournament.rb', line 36

def extract_results
  extract_kind
  extract_notes
  extract_location
  parse_time(extract_date, extract_time)
end

#extract_timeObject



57
58
59
60
61
62
63
64
# File 'lib/dtv_tournaments/tournament.rb', line 57

def extract_time
  if page.search(".markierung .uhrzeit").first.text.empty?
    # This tournament is a big one
    get_time_from_big_tournament(page.search(".turniere tr"))
  else
    page.search(".markierung .uhrzeit").text.scan(/\d{1,2}:\d{2}/).first
  end
end

#get_first_time_until(times, index) ⇒ Object



82
83
84
85
86
# File 'lib/dtv_tournaments/tournament.rb', line 82

def get_first_time_until times, index
    for i in (0..index).to_a.reverse
      return times[i] unless times[i].nil? || times[i].empty?
    end
end

#get_index_of_marked_tournament(tournaments) ⇒ Object



76
77
78
79
80
# File 'lib/dtv_tournaments/tournament.rb', line 76

def get_index_of_marked_tournament tournaments
  tournaments.each_with_index do |single_tournament, index|
    return index if single_tournament.attributes().has_key?('class')
  end
end

#get_result_pageObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dtv_tournaments/tournament.rb', line 22

def get_result_page
  mechanize = Mechanize.new { |agent|
    agent.read_timeout = 3
  }

  mechanize.get(DTVTournaments::FETCHINGURL)
  search_form = mechanize.page.forms.last

  search_form.nr = @number
  search_form.submit

  @page = mechanize.page
end

#get_time_from_big_tournament(tournaments) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dtv_tournaments/tournament.rb', line 88

def get_time_from_big_tournament tournaments
  times = tournaments.map do |single_tournament|
    next_time = DTVTournaments::get_subelement_if_available(single_tournament, ".uhrzeit")

    if next_time.nil? || next_time.empty?
      nil
    else
      next_time.scan(/\d{1,2}:\d{2}/).first
    end
  end

  index = get_index_of_marked_tournament tournaments
  get_first_time_until(times, index)
end

#parse_time(date, time) ⇒ Object



70
71
72
73
74
# File 'lib/dtv_tournaments/tournament.rb', line 70

def parse_time date, time
  @datetime = DateTime.parse("#{date} #{time}")
  @date     = Date.parse(date)
  @time     = Time.parse("#{date} #{time}")
end

#save_to_cacheObject



18
19
20
# File 'lib/dtv_tournaments/tournament.rb', line 18

def save_to_cache
  DTVTournaments.get_cache.set(self)
end

#serializeObject



120
121
122
# File 'lib/dtv_tournaments/tournament.rb', line 120

def serialize
  "#{@number}|#{@notes}|#{@datetime.to_s}|#{@street}|#{@zip}|#{@city}|#{@kind}"
end

#timeFromString(datetime) ⇒ Object



114
115
116
117
118
# File 'lib/dtv_tournaments/tournament.rb', line 114

def timeFromString(datetime)
  self.datetime = DateTime.parse(datetime)
  self.time = Time.parse(datetime) - Time.zone_offset(Time.parse(datetime).zone)
  self.date = Date.parse(datetime)
end