Class: PriceManager

Inherits:
Object
  • Object
show all
Includes:
Concerns::Mergable, Concerns::Printable, Concerns::Searchable, Concerns::Sortable, Concerns::Statistical
Defined in:
lib/price_manager.rb

Constant Summary collapse

["\n",
"Available Actions:",
"----------------------------------------------",
"category -> View and Select Category",
"view     -> View Items in Category",
"item     -> Enter Search Item",
"price    -> View Price Information",
"range    -> View Items in Range",
"pid      -> View Item Advanced Info",
"q        -> Quit",
"----------------------------------------------",
"Please type in your selection",
"----------------------------------------------"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Mergable

#merge_item, #merge_price_manager_attr

Methods included from Concerns::Statistical

#basic_stats, #quartile

Methods included from Concerns::Printable

#print_basic_stats, #print_item_by_pid, #print_items_by_price, #print_items_in_category, #print_items_in_range

Methods included from Concerns::Sortable

#sort_by_location, #sort_by_price, #sort_by_price_in_range

Methods included from Concerns::Searchable

#get_link_from_key, #get_subcategory_info, #items_in_price_range, #items_with_price, #search_by_category, #search_by_pid, #search_by_type, #search_items

Constructor Details

#initialize(url) ⇒ PriceManager

Returns a new instance of PriceManager.



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

def initialize(url)
  @url = url
  puts "OK, we are working with #{@url}"
  sleep 1
  @site = CL_Scraper.new(@url)
  @site.scrape_for_sale_categories
  @stats = {}
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



2
3
4
# File 'lib/price_manager.rb', line 2

def category
  @category
end

#itemObject

Returns the value of attribute item.



2
3
4
# File 'lib/price_manager.rb', line 2

def item
  @item
end

#maxObject

Returns the value of attribute max.



2
3
4
# File 'lib/price_manager.rb', line 2

def max
  @max
end

Returns the value of attribute menu.



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

def menu
  @menu
end

#minObject

Returns the value of attribute min.



2
3
4
# File 'lib/price_manager.rb', line 2

def min
  @min
end

#pidObject

Returns the value of attribute pid.



2
3
4
# File 'lib/price_manager.rb', line 2

def pid
  @pid
end

#siteObject (readonly)

Returns the value of attribute site.



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

def site
  @site
end

#statsObject

Returns the value of attribute stats.



2
3
4
# File 'lib/price_manager.rb', line 2

def stats
  @stats
end

#subcategoryObject

Returns the value of attribute subcategory.



2
3
4
# File 'lib/price_manager.rb', line 2

def subcategory
  @subcategory
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#actions_menuObject



58
59
60
61
# File 'lib/price_manager.rb', line 58

def actions_menu
  MENU.each{|message| puts "#{message}"}
  gets.chomp
end

#callObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/price_manager.rb', line 32

def call
  process_category
  run = true
  while run
    case actions_menu
    when "category"
      process_category
    when "view"
      print_items_in_category
    when "item"
      process_item
    when "price"
      process_price
    when "range"
      process_range
    when "pid"
      process_pid
    when "debug"
      binding.pry #uncomment this line and require 'pry' to allow debug session
    when "q"
      run = false
    end
  end

end

#category_menuObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/price_manager.rb', line 104

def category_menu
  puts "\n"
  puts "Available 'for sale' categories are:"
  puts "----------------------------------------------"
  @site.menu_hash.each_key{|key| puts key}
  puts "----------------------------------------------"
  puts "Enter the category you want to browse"
  puts "----------------------------------------------"
  @category = gets.strip.downcase
  unless @site.menu_hash.has_key?(@category)
    puts "Category: #{@category} not found! Please check the spelling."
    sleep 1
    category_menu
  end
  check_subcategory_menu
end

#check_subcategory_menuObject



121
122
123
124
125
126
127
128
129
# File 'lib/price_manager.rb', line 121

def check_subcategory_menu
  list = ["auto parts", "bikes", "boats", "cars+trucks", "computers", "motorcycles"]
  if list.include?(@category)
    @site.scrape_second_level_menus(get_link_from_key)
    subcategory_menu
  else
    get_link_from_key
  end
end

#process_categoryObject



63
64
65
66
67
68
69
70
# File 'lib/price_manager.rb', line 63

def process_category
  #@site.scrape_category(category_menu)
  @site.scrape_page(category_menu) #Better for testing.
  Item.create_from_collection(@site.all)
  merge_price_manager_attr #Set item category and url if they are not set.
  print_items_in_category
  process_item
end

#process_itemObject



72
73
74
75
76
77
# File 'lib/price_manager.rb', line 72

def process_item
  puts "Please Enter your sale item:"
  @item = gets.chomp.downcase
  search_by_type
  process_price
end

#process_pidObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/price_manager.rb', line 79

def process_pid
  puts "Please Enter the PID:"
  @pid = gets.chomp
  if search_by_pid != []
    Item.merge_item(@pid, @site.scrape_by_pid(search_by_pid.link))
    print_item_by_pid
  else
    puts "PID: #{@pid} unavailable in #{@category}"
    print_item_by_pid
  end
end

#process_priceObject



91
92
93
94
# File 'lib/price_manager.rb', line 91

def process_price
  print_items_by_price
  print_basic_stats
end

#process_rangeObject



96
97
98
99
100
101
102
# File 'lib/price_manager.rb', line 96

def process_range
  puts "Enter a minimum price"
  @min = gets.chomp.to_i
  puts "Enter a maximum price"
  @max = gets.chomp.to_i
  print_items_in_range
end

#subcategory_menuObject



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
# File 'lib/price_manager.rb', line 131

def subcategory_menu
  puts "----------------------------------------------"
  puts "Available categories in #{@category} are:"
  puts "----------------------------------------------"
  @site.submenu_hash.each_key{|key| puts key}
  puts "----------------------------------------------"
  puts "\n"
  puts "Enter the subcategory you want to browse"
  puts "----------------------------------------------"
  @subcategory = gets.strip
  unless @site.submenu_hash.has_key?(@subcategory)
    puts "******************************************************************"
    puts "Subcategory: #{@subcategory} not found! Please check the spelling."
    puts "******************************************************************"
    sleep 1 #pause so user can see warning
    subcategory_menu
  else
    puts "\n"
    puts "Please type in a selection from the following:"
    puts "----------------------------------------------"
    get_subcategory_info.each_key{|key| puts key.downcase}
    puts "----------------------------------------------"
    choice = gets.strip.upcase
    @url + get_subcategory_info.fetch(choice)
  end
end