Class: CommandLineInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/Brew_Finder/command_line_interface.rb

Constant Summary collapse

BASE_PATH =

change this to base api query

"http://beermapping.com/webservice/"
KEY =
"21110efff66df69d91ec3909c0a38eed"

Instance Method Summary collapse

Instance Method Details

#add_scores_to_brewery(brewery_id, score_hash) ⇒ Object



157
158
159
160
161
# File 'lib/Brew_Finder/command_line_interface.rb', line 157

def add_scores_to_brewery(brewery_id, score_hash)
  scored_brewery = Brewery.all.detect{|brewery| brewery.id = brewery_id}
  scored_brewery.add_score_info(score_hash)
  scored_brewery
end

#display_breweriesObject



163
164
165
166
167
168
169
170
# File 'lib/Brew_Finder/command_line_interface.rb', line 163

def display_breweries
  Brewery.all.each do |brewery|
    puts "#{brewery.name}".colorize(:purple)
    puts "#{brewery.id}".colorize(:green)
    puts "#{brewery.street_address}".colorize(:blue)
    puts "#{brewery.phone}".colorize(:orange)
  end
end

#display_score(scored_brewery) ⇒ Object

display additional requested brewery info



173
174
175
176
177
178
179
180
181
# File 'lib/Brew_Finder/command_line_interface.rb', line 173

def display_score(scored_brewery)
  puts "#{scored_brewery.name}".colorize(:purple)
  puts "Overall score: #{scored_brewery.overall_score}".colorize(:red)
  puts "Selection: #{scored_brewery.selection}"
  puts "Service: #{scored_brewery.service}"
  puts "Atmosphere: #{scored_brewery.atmosphere}"
  puts "Number of reviews: #{scored_brewery.review_count}"
  puts "Food: #{scored_brewery.food}"
end

#end_menuObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/Brew_Finder/command_line_interface.rb', line 83

def end_menu
  puts "Enter 'list' to show the previous search, 'search' to start a new search, or 'exit' to quit."
  input = nil
  while input != "exit"
    input = gets.chomp
    case input
    when /list/
      display_breweries
      score_menu
    when /search/
      return_search
      score_menu
    when /exit/
      puts "Goodbye!"
      exit
    else
      puts "Sorry, what did you want to do?"
    end
  end
end

#format_location_query(location) ⇒ Object



125
126
127
128
# File 'lib/Brew_Finder/command_line_interface.rb', line 125

def format_location_query(location)
  location_query = BASE_PATH + 'loccity/' + KEY + location.downcase
  location_query
end

#format_score_query(brewery_id) ⇒ Object



139
140
141
# File 'lib/Brew_Finder/command_line_interface.rb', line 139

def format_score_query(brewery_id)
  score_query = BASE_PATH + 'locscore/' + KEY + '/' + brewery_id
end

#get_breweries(formatted_location) ⇒ Object

grab brewery objects from API



143
144
145
146
147
148
# File 'lib/Brew_Finder/command_line_interface.rb', line 143

def get_breweries(formatted_location)
  brewery_array = Brewery_Fetcher.query_api(formatted_location)
  #create instances of breweries from each brewery fetched
  Brewery.create_from_collection(brewery_array)
  #puts "#{brewery_array}"
end

#get_brewery_score(formatted_score) ⇒ Object

take the requested brewery and add additional info



151
152
153
154
155
# File 'lib/Brew_Finder/command_line_interface.rb', line 151

def get_brewery_score(formatted_score)
  #take the brewery instance and give it more attributes
  scores = Brewery_Fetcher.fetch_score_info(score_query)
  scores
end

#get_location_queryObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/Brew_Finder/command_line_interface.rb', line 110

def get_location_query
  puts "Please enter the initials of a state you would like to search in:"
  begin
    puts "A valid state abbreviation is two letters."
    state = gets.chomp
  end until state.match(/^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]))$/)

  puts "Now enter a city:"
  begin
    puts "Any city in #{state}"
    city = gets.chomp.downcase
  end until city.match(/^[a-zA-Z]+(?:[\s-][a-zA-Z]+)*$/)
  city_state_formatted = '/' + city + ',' + state
end

#get_score_queryObject



130
131
132
133
134
135
136
137
# File 'lib/Brew_Finder/command_line_interface.rb', line 130

def get_score_query
  puts "Which brewery would you like to learn more about?"
  begin
    puts "A valid brewery id is between 4 and 6 digits:"
    id = gets.chomp
  end until id.match(/(\d{4,6})/)
  id
end

#return_scoresObject



104
105
106
107
108
# File 'lib/Brew_Finder/command_line_interface.rb', line 104

def return_scores
  scores = get_brewery_score(formatted_score)
  scored_brewery = add_scores_to_brewery(first_score_query, scores)
  display_score(scored_brewery)
end

#return_searchObject



53
54
55
56
57
58
59
# File 'lib/Brew_Finder/command_line_interface.rb', line 53

def return_search
  user_location_query = get_location_query
  #puts "#{user_location_query}"
  formatted_query = format_location_query(user_location_query)
  #puts "#{formatted_query}"
  get_breweries(formatted_query)
end

#root_menuObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/Brew_Finder/command_line_interface.rb', line 34

def root_menu
  puts "Type 'search' to look for breweries, or 'exit' to quit."
  input = nil
  while input != "exit"
    input = gets.chomp
    case input
    when /search/
      return_search
      display_breweries
      score_menu
    when /exit/
      puts "Goodbye!"
      exit
    else
      puts "Sorry, what did you want to do?"
    end
  end
end

#score_menuObject



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

def score_menu
  puts "You can enter 'scores' if you would like to see how people have rated a brewery"
  puts "You can also enter 'search' to start a new search, or enter 'exit' to quit."
  input = nil
  while input != "exit"
    input = gets.chomp
    case input
    when /scores/
      return_scores
      end_menu
    when /search/
      return_search
      scores_menu
    when /exit/
      puts "Goodbye!"
      exit
    else
      puts "Sorry, what did you want to do?"
    end
  end
end

#welcomeObject

define welcome



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/Brew_Finder/command_line_interface.rb', line 12

def welcome
  #first welcome user
  puts "\e[H\e[2J"
  puts <<-DOC
                    o©ºº©oo©oº°©
                    /           \
                    |___________|____
                    |            |____)
                    |  WELCOME   |  | |
                    |            |  | |
                    |    TO      |  | |
                    |            |  | |
                    |  B R E W   |  | |
                    |            |__|_|
                    |   FINDER!  |____)
                    |____________|
                   (______________)
  DOC
  root_menu
end