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
# 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



40
41
42
43
44
45
46
# File 'lib/study_the_map/ski_map.rb', line 40

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



32
33
34
35
36
37
38
# File 'lib/study_the_map/ski_map.rb', line 32

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



54
55
56
57
58
59
60
# File 'lib/study_the_map/ski_map.rb', line 54

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



48
49
50
51
52
# File 'lib/study_the_map/ski_map.rb', line 48

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

#pick_map(year) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/study_the_map/ski_map.rb', line 62

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