Class: Gyms::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#zipObject

Returns the value of attribute zip.



3
4
5
# File 'lib/gyms/cli.rb', line 3

def zip
  @zip
end

Instance Method Details

#callObject



5
6
7
8
9
10
11
# File 'lib/gyms/cli.rb', line 5

def call
  title
  zip = get_zip
  get_gyms(zip)
  menu
  goodbye
end

#display_detail(input) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/gyms/cli.rb', line 72

def display_detail(input)
    puts ""
    puts @gyms[input.to_i - 1].name
    puts @gyms[input.to_i - 1].address1
    puts @gyms[input.to_i - 1].address2
    puts @gyms[input.to_i - 1].phone
    puts ""
end

#get_detail_inputObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gyms/cli.rb', line 51

def get_detail_input
  puts "Type number for gym details or type exit:"
  input = gets.strip

  if input == "exit"
    goodbye
  elsif input == nil || input.to_i < 0 || input.to_i > 8 || input.count("a-zA-Z") > 0
    puts "* * * Invalid entry. Try again. * * * "
    puts ""
    get_detail_input
  else
    input
  end
end

#get_gyms(zip) ⇒ Object



19
20
21
# File 'lib/gyms/cli.rb', line 19

def get_gyms(zip)
  @gyms = Gyms::Gym.get_local_gyms(zip)
end

#get_zipObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gyms/cli.rb', line 33

def get_zip
  puts "Enter zip code: "
  input = gets.strip

  if input.length != 5 || !string_only_numbers?(input) || input == nil
    puts "* * * Invalid entry. Try again. * * * "
    puts ""

    get_zip
  end

  input
end

#goodbyeObject



81
82
83
84
85
86
# File 'lib/gyms/cli.rb', line 81

def goodbye
  puts ""
  puts "Enjoy your workout!"
  puts ""
  exit
end

#list_gymsObject



66
67
68
69
70
# File 'lib/gyms/cli.rb', line 66

def list_gyms
  @gyms.each.with_index(1) do |gym, i|
    puts "#{i}. #{gym.name}"
  end
end


23
24
25
26
27
28
29
30
31
# File 'lib/gyms/cli.rb', line 23

def menu
  input = nil
  list_gyms

  while input != "exit"
    input = get_detail_input
    display_detail(input)
  end
end

#string_only_numbers?(string) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/gyms/cli.rb', line 47

def string_only_numbers?(string)
  string.scan(/\D/).empty?
end

#titleObject



13
14
15
16
17
# File 'lib/gyms/cli.rb', line 13

def title
  puts ""
  puts "Gym Locator"
  puts ""
end