Class: AutoServiceCLI::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



8
9
10
# File 'lib/auto_service_cli/cli.rb', line 8

def initialize
  self.scraper = AutoServiceCLI::Scraper.new
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
# File 'lib/auto_service_cli/cli.rb', line 12

def call
  welcome
  prompt_zip
  scrape_main_page
  list_centers
  menu
end

#get_detailsObject



81
82
83
84
85
86
87
88
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
# File 'lib/auto_service_cli/cli.rb', line 81

def get_details
  puts "\n\tEnter the number of center:".green
  input = gets.strip

  if input.to_i >= 1 && input.to_i <= AutoServiceCLI::ServiceCenter.all.size
    center = AutoServiceCLI::ServiceCenter.all[input.to_i - 1]
    unless center.int_url.nil?
      puts "\nObtaining data..."
      scraper.scrape_center_details(center)
      puts "Done"
    end

    puts "----------------------------------------------------------------------------------------------------------------"
    puts "\n\t#{center.name.upcase}\n".red
    puts "\tRating:\n#{center.rating}\n".cyan unless center.rating.nil?
    puts "\tCategory:\n#{center.main_category}\n".cyan unless center.main_category.nil?
    puts "\tAddress:\n#{center.address}\n".cyan unless center.address.nil?
    puts"\tPhone number:\n#{center.phone_number}\n".cyan unless center.phone_number.nil?

    unless center.int_url.nil?
      puts "\tStatus:\n#{center.open_status}\n".cyan unless center.open_status.nil?
      puts "\tSlogan:\n#{center.slogan}\n".cyan unless center.slogan.nil?
      puts "\tWorking hours:\n#{center.working_hours}".cyan unless center.working_hours.nil?
      puts "\tDescription:\n#{center.description}\n".cyan unless center.description.nil?
      puts "\tServices:\n#{center.services}\n".cyan unless center.services.nil?
      puts "\tBrands:\n#{center.brands}\n".cyan unless center.brands.nil?
      puts "\tPayment methods:\n#{center.payment}\n".cyan unless center.payment.nil?
    end

    puts "\tSee more at:\n#{center.ext_url}\n".cyan unless center.ext_url.nil?
    puts "----------------------------------------------------------------------------------------------------------------"
  end
end

#goodbyeObject



147
148
149
# File 'lib/auto_service_cli/cli.rb', line 147

def goodbye
  puts "\n\tThank you for using this application!".blue
end

#help_menuObject



132
133
134
135
136
137
138
# File 'lib/auto_service_cli/cli.rb', line 132

def help_menu
  puts "\n1. List centers".green
  puts "2. Show details about service center".green
  puts "3. Change sorting type".green
  puts "4. Reload centers".green
  puts "10. Exit".green
end

#help_sortObject



140
141
142
143
144
145
# File 'lib/auto_service_cli/cli.rb', line 140

def help_sort
  puts "\n\t1. Default".green
  puts "\t2. Sort by distance".green
  puts "\t3. Sort by rating".green
  puts "\t4. Sort by name".green
end

#list_centersObject



53
54
55
56
57
58
59
60
61
# File 'lib/auto_service_cli/cli.rb', line 53

def list_centers
  puts "-----------------------------------------------------------------------------"
  puts "\tZIP: #{scraper.zip}\n\tSorting type: #{scraper.sort_type}\n".cyan
  AutoServiceCLI::ServiceCenter.all.each.with_index(1) do |center,i|
    print "#{i}.".cyan; print " #{center.name}"
    puts center.rating.nil? ? "" : ", rating: #{center.rating}"
  end
  puts "-----------------------------------------------------------------------------"
end

Menu methods



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/auto_service_cli/cli.rb', line 31

def menu
  loop do
    help_menu
    puts "\nMake a choice:".magenta
    input = gets.strip
    case input
    when "1"
      list_centers
    when "2"
      get_details
    when "3"
      sort
    when "4"
      scrape_main_page
      list_centers
    when "10"
      goodbye
      break
    end
  end
end

#prompt_zipObject



20
21
22
23
24
25
26
# File 'lib/auto_service_cli/cli.rb', line 20

def prompt_zip
  begin
    puts "\tEnter you zip code:".magenta
    zip = gets.strip
  end until AutoServiceCLI::Scraper.valid_zip?(zip)
  scraper.zip = zip
end

#scrape_main_pageObject



115
116
117
118
119
120
121
122
123
# File 'lib/auto_service_cli/cli.rb', line 115

def scrape_main_page
  puts "Obtaining data for you... It will take a few seconds"

  # clean up all records.
  AutoServiceCLI::ServiceCenter.reset_all!

  scraper.scrape_centers
  puts "Done"
end

#sortObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/auto_service_cli/cli.rb', line 63

def sort
  help_sort
  puts "\n\tChoose type of sorting:".magenta
  input = gets.strip
  case input
  when "1"
    scraper.sort_type = AutoServiceCLI::Scraper::SORT_TYPES[:DAFAULT]
  when "2"
    scraper.sort_type = AutoServiceCLI::Scraper::SORT_TYPES[:DISTANCE]
  when "3"
    scraper.sort_type = AutoServiceCLI::Scraper::SORT_TYPES[:AVERAGE_RATING]
  when "4"
    scraper.sort_type = AutoServiceCLI::Scraper::SORT_TYPES[:NAME]
  end
  scrape_main_page
  list_centers
end

#welcomeObject


Helper methods



128
129
130
# File 'lib/auto_service_cli/cli.rb', line 128

def welcome
  puts "\n\tWelcome to auto service centers searching CLI!\n\tby Aleksandr Rogachev © #{Time.new.year}\n".green
end