Class: SmartphoneFinder::CLI

Inherits:
Object
  • Object
show all
Includes:
HelperMethods
Defined in:
lib/smartphone_finder/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods

#list_all, #obj_exist?

Instance Attribute Details

#search_resultsObject

Returns the value of attribute search_results.



7
8
9
# File 'lib/smartphone_finder/cli.rb', line 7

def search_results
  @search_results
end

Instance Method Details

#listObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/smartphone_finder/cli.rb', line 17

def list
	puts ""
	puts "-----------------------Main Menu------------------------------------------------------------#"
	puts "1- Search by brand   #Enter '1' to browse brands and related devices online on gsmarena.com |"
	puts "2- Search by keyword #Enter '2' to search for specifed keyword online on gsmarena.com       |" 
	puts "3- Exit              #Enter '3' to End application.                                         |"
	puts "--------------------------------------------------------------------------------------------#"
	puts ""
  puts "choose an option please"

end


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/smartphone_finder/cli.rb', line 141

def menu
	list
	input=nil
	while(input !="3")
		puts ""
		input=gets.strip
        if input =="1"
           search_by_brand
        elsif input =="2"
           search_by_keyword
        elsif input=="3"
        	  puts ""
           puts "Thank you for using Smartphone Finder" 
           puts ""
           break

        else
         puts "invalid input"
         list
       end
	end
end

#search_by_brandObject



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

def search_by_brand

#get and display brands
   show_brands
   #ask user to choose brand index to list relative devices
   puts""
   puts"Enter brand index to list relative devices OR Enter 'menu' to list the main menu"
   input=gets.strip.to_i
   if input>0 && input <= SmartphoneFinder::Brand.all.size
   	#get and display devices
       size=show_devices(input-1)#returns devices number
       #choose a device and get/display device specifications
       puts ""
   	  show_device_spec(size,"1",input-1)
   elsif input > SmartphoneFinder::Brand.all.size
       puts ""
       puts "invalid brand index" 
       search_by_brand     
   else
   	#puts "invalid option" 
   	list 
   	end

end

#search_by_keywordObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/smartphone_finder/cli.rb', line 80

def search_by_keyword
   puts "-------------------------------------------------"
   puts "Enter your desired keyword here  OR Enter 'menu' to list the main menu"
   keyword = gets.strip
   if keyword=="menu"
        list
   else
       self.search_results= SmartphoneFinder::Scraper.get_by_keyword(keyword)
 if self.search_results.size>0
          show_search_results
          show_device_spec(self.search_results.size,"2")
   else 
        puts "No result meet your search , please try different keyword. Would you like to try again ? Y(es) or N(o)"
        response=gets.strip
        if response.downcase=="y" || response.downcase=="yes" 
          search_by_keyword
        else 
         list
         end 
      end
   end
end

#show_brandsObject



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

def show_brands
 	#call scraper to extract brands list
puts "Geting Brands ..."
SmartphoneFinder::Scraper.get_brands
puts ""
puts "Listing all brands ..."
puts ""
 SmartphoneFinder::Brand.list_brands

end

#show_device_spec(size, option, index = nil) ⇒ Object

displaying device specifications



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/smartphone_finder/cli.rb', line 102

def show_device_spec(size,option,index=nil)#displaying device specifications
     if option=="1"
         puts "Enter device index to show related speceifications , Enter '(B)ack' to go to brevious menu , OR Enter 'menu' to list the main menu"
      else 
        puts "Enter device index to show related speceifications , Enter '(R)etry' to try different keyword , OR Enter 'menu' to list the main menu"
       end
    input_ = gets.strip
      index_=input_.to_i-1

    if input_.to_i >0 && input_.to_i <= size
          SmartphoneFinder::Scraper.get_device_spec(SmartphoneFinder::Device.all[index_])
          puts ""
          puts "Displaying specifications for #{(SmartphoneFinder::Device.all[index_]).name} devices ........"
          SmartphoneFinder::Device.all[index_].specifications.display
          puts ""            
          #show navigation options
          puts " Enter 'menu' for main menu , Enter '(B)ack' to go to brevious list "
          response=gets.strip
          if response.downcase=="b" || response.downcase=="back" 
             #calling the correct method according to the chosen option
            option=="2" ? show_search_results : show_devices(index)
            show_device_spec(size,option, index )
          else 
           list
          end
      elsif input_.to_i > size
        puts ""
        puts "invalid device index"  
        show_device_spec(size,option,index)

      elsif (input_.downcase=="r" || input_.downcase=="retry") && option=="2" 
           search_by_keyword 
      elsif (input_.downcase=="b" || input_.downcase=="back") && option=="1" 
           search_by_brand
      else
        list			   
     end
end

#show_devices(index) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/smartphone_finder/cli.rb', line 62

def show_devices(index)
	  #call scraper to extract devices list
    SmartphoneFinder::Scraper.get_devices_by_brand(SmartphoneFinder::Brand.all[index])
    #listing devices .....
    brand=SmartphoneFinder::Brand.all[index]
    puts ""
    puts "Listing #{brand.name} devices ........"
    puts ""
    (SmartphoneFinder::Brand.all[index]).list_devices  
    return brand.devices.size
end

#show_search_resultsObject



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

def show_search_results 
  puts "Listing search results  ........"
   puts ""
       self.list_all(self.search_results)
	
end

#startObject



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

def start
    welcome
    menu
end

#welcomeObject

welcom user and display the current version



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

def welcome#welcom user and display the current version
 puts ""
 puts "welcome to Smartphone Finder version #{SmartphoneFinder::VERSION}"
end