Class: SkiMaps

Inherits:
Object
  • Object
show all
Defined in:
lib/study_the_map/ski_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(area_name) ⇒ SkiMaps

Returns a new instance of SkiMaps.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/study_the_map/ski_map.rb', line 7

def initialize(area_name)

  @area_name = area_name
  area_doc = Nokogiri::XML(open("https://skimap.org/SkiAreas/view/#{LookupIDS.find_ski_area_id(area_name)}.xml"))
  
  @area_info = area_doc
  map_count = area_doc.search("skiMaps").attr('count').text

  puts "There are #{map_count} maps for this ski area."

  if map_count == "0"

    StudyTheMap::CLI.new.call

  elsif map_count == "1"

    self.pick_map(self.get_map_years.join)

  else

    puts "Please wait while we fetch the maps' years..."
    puts ""

    self.list_map_years
    map_years = self.get_map_years

    input = nil

    until map_years.include?(input)
      puts ""
      puts "Please pick a year that is listed."
      input = gets.strip
    end

    self.pick_map(input)

  end
end

Instance Attribute Details

#area_infoObject

Returns the value of attribute area_info.



5
6
7
# File 'lib/study_the_map/ski_map.rb', line 5

def area_info
  @area_info
end

#area_nameObject

Returns the value of attribute area_name.



5
6
7
# File 'lib/study_the_map/ski_map.rb', line 5

def area_name
  @area_name
end

Instance Method Details

#get_map_dataObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/study_the_map/ski_map.rb', line 58

def get_map_data

  map_data_array = []

  self.get_map_ids.each do |id|
    map_data_array << Nokogiri::XML(open("https://skimap.org/SkiMaps/view/#{id}.xml"))
  end

  map_data_array

end

#get_map_idsObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/study_the_map/ski_map.rb', line 46

def get_map_ids

  id_array = []

  @area_info.search("skiMaps skiMap").each do |map|
    id_array << map.attr('id')
  end

  id_array

end

#get_map_yearsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/study_the_map/ski_map.rb', line 78

def get_map_years

  map_years_array = []

  self.get_map_data.each do |map|
    map_years_array << map.search("yearPublished").text
  end

  map_years_array

end

#list_map_yearsObject



70
71
72
73
74
75
76
# File 'lib/study_the_map/ski_map.rb', line 70

def list_map_years  

  self.get_map_data.each do |map|
    puts map.search("yearPublished").text
  end

end

#pick_map(year) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/study_the_map/ski_map.rb', line 90

def pick_map(year)

  map_data = self.get_map_data.detect do |map|
    map.search("yearPublished").text == year
  end

  begin
    url = map_data.search("render").attr('url').text
  rescue Exception
    url = map_data.search("unprocessed").attr('url').text
  end

  puts "Would you like to download the Map to your working directory or see it in your browser?"
  puts "Type 'browser', 'download', or 'exit'"

  input = gets.strip

  case input 
    
  when "download"

    exec "curl -O #{url}"
    puts "Enjoy your map!"

  when "browser"

    Launchy.open("#{url}")
    puts "Enjoy your map!"

  end

end