Class: Alparser::Branc

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/alparser/branc.rb

Instance Method Summary collapse

Methods included from Parser

included

Instance Method Details

#all_club_climbs(year: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/alparser/branc.rb', line 15

def all_club_climbs year: nil
  out = club_climbs(year: year)
  number_of_pages, i = @number_of_pages, 2

  while i <= number_of_pages
    out = out + club_climbs(page: i, year: year)
    i += 1
  end

  out
end

#climb_from(item) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/alparser/branc.rb', line 41

def climb_from item
  date = Date.parse(item.xpath("./td[position()=1]").text)
  route = item.xpath("./td[position()=2]/a").text.strip
  route ||= item.xpath("./td[position()=2]//b").text.strip

  id = nil
  if item.xpath("./td[position()=2]/a").first.attributes["href"].to_s =~ /pid=(\d+)$/
    id = Integer(Regexp.last_match[1].to_s)
  end

  if id.nil? and item.xpath("./td[position()=2]/a").first.attributes["href"].to_s =~ /rid=(\d+)$/
    id = Integer(Regexp.last_match[1].to_s)
  end

  mountain_range = item.xpath("./td[position()=3]").text.strip
  country = item.xpath("./td[position()=4]").text.lstrip.strip.sub!(/\u00A0/, "")
  country ||= item.xpath("./td[position()=4]").text

  has_notes = item.xpath("./td[position()=2]/img[@src='ikone/opombe.gif']").size == 1
  has_images = item.xpath("./td[position()=2]/img[@src='ikone/fotke.gif']").size == 1

  country_id = nil
  begin
    if item.xpath("./td[position()=4]/img").first.attributes["src"].to_s =~ /\/(\w+)\.gif$/
      country_id = Regexp.last_match[1].to_s
    end
  rescue
    # Noting
  end

  grade = item.xpath("./td[position()=5]").text
  climber = item.xpath("./td[position()=6]/a")

  climber_id = nil
  if climber.first.attributes["href"].to_s =~ /mem=(\d+)/
    climber_id = Regexp.last_match[1].to_i
  end

  climber = climber.text.strip

  conditions = item.xpath("./td[position()=7]").text
  kind = item.xpath("./td[position()=8]").text
  kind = kind == "" ? nil : kind

  climb = Alparser::Climb.new(
    id: id,
    date: date,
    route: route,
    mountain_range: mountain_range,
    country: country,
    country_id: country_id,
    grade: grade,
    conditions: conditions,
    kind: kind,
    has_notes: has_notes,
    has_images: has_images,
    user_name: climber,
    user_id: climber_id
  )

  climb.base_uri = @url

  unless climb.user_id.nil?
    climb.user = Alparser::User.new(
      id: climb.user_id,
      name: climb.user_name,
      base_uri: @url
    )
  end

  climb
end

#club_climbs(page: 1, year: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/alparser/branc.rb', line 4

def club_climbs page:1, year: nil

  response = self.class.get "/vzponi-vse.php", query: {
    page: page,
    leto: (year.nil? ? Time.now.year : year)
  }

  handle_page_number response
  handle_climbs response
end

#handle_climbs(response) ⇒ Object



27
28
29
30
31
# File 'lib/alparser/branc.rb', line 27

def handle_climbs response
  response.parsed_response.xpath("//table[@bgcolor='silver']/tr[position()>1]").to_a.map! do |item|
    climb_from item
  end
end

#handle_page_number(response) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/alparser/branc.rb', line 33

def handle_page_number response
  @number_of_pages ||= 0

  # puts "handle_page_number"
  @number_of_pages = response.parsed_response.xpath("//a[@class='pagebox']/span").to_a.last.text.gsub!(/[^a-z0-9\-_]+/, "").to_i
  # @number_of_pages = ((/^\<span\>.(\d+)/.match(response.parsed_response.xpath("//a[@class='pagebox']/span").to_a.last.to_s))[1].to_i) rescue 1
end