Class: StudyTheMap::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



10
11
12
13
14
15
16
# File 'lib/study_the_map/cli.rb', line 10

def call

  get_areas

  goodbye

end

#get_areasObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/study_the_map/cli.rb', line 124

def get_areas
  
  input = nil
  while input != 'exit'

    puts "1. Select a region (type 'region') or Ski Resort (type 'resort') to study associated ski trail maps!"
    puts '-----------------------------------------------------------------------'
    puts "2. Or check out the world map to find resorts and trails: (type 'world')"
    puts '-----------------------------------------------------------------------'
    puts "3. Enter the area you would like to search:"
    puts '-->'
    
    input = gets.strip.downcase

    case input
    when "region"

      region

    when "world"

      Launchy.open("http://openskimap.org/")

    when "resort"

      get_map

    else 

      puts "Please enter 'region', 'resort', or 'world'"
      puts '-----------------------------------------------------------------------'
    end
  end
end

#get_mapObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/study_the_map/cli.rb', line 26

def get_map

  resort_name = nil
  while resort_name != 'back'

    puts "1. Please enter the name of the ski area you would like the map for:"
    puts '-----------------------------------------------------------------------'
    puts "2. Or type 'back' to go back to the main menu."
    puts '-->'

    resort_name = gets.strip

    if resort_name == 'back'

      get_areas

    elsif RegionScraper.ski_area_list.include?(resort_name)

      @skimaps = SkiMaps.new(resort_name)
      self.map_count_and_pick(@skimaps.map_count)


    else

      puts "Invalid ski area name"
    end
  end
end

#goodbyeObject



205
206
207
208
# File 'lib/study_the_map/cli.rb', line 205

def goodbye

  puts "Come back anytime for more maps!"
end

#list_map_yearsObject



18
19
20
21
22
23
24
# File 'lib/study_the_map/cli.rb', line 18

def list_map_years  

  @skimaps.area_info.scrape_map_data.each do |map|
    puts map.search("yearPublished").text
  end

end

#map_count_and_pick(map_count) ⇒ Object



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
# File 'lib/study_the_map/cli.rb', line 55

def map_count_and_pick(map_count)

  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 = @skimaps.area_info.scrape_map_years

    input = nil

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

    pick_map(input)

  end

end

#pick_map(year) ⇒ Object



89
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
# File 'lib/study_the_map/cli.rb', line 89

def pick_map(year)

  map_data = @skimaps.area_info.scrape_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

#regionObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/study_the_map/cli.rb', line 159

def region

  region_name = nil
  while region_name != "back"

    puts "-----------------------------------------------------------------------"
    puts "1. Input a region for a list of ski resorts, or"
    puts '-----------------------------------------------------------------------'
    puts "2. Request a list of regions by typing the letter the region you're looking for starts with,"
    puts '-----------------------------------------------------------------------'
    puts "3. Or 'back' to return to the main menu."
    puts '-->'
    
    region_name = gets.strip

    if region_name == "back"
      call
    end
    
    if region_name.size == 1

      Region.starts_with(region_name)
      region

    else

      begin

        region_object = Region.new(region_name)
        region_object.full_list

      rescue Exception

        if region_name != "back"
          puts '-----------------------------------------------------------------------'
          puts "Not a valid region."
          puts '-----------------------------------------------------------------------'
        end

        region

      end      
    end
  end
end