Class: Stocker::Generator
- Inherits:
-
Thor
- Object
- Thor
- Stocker::Generator
- Defined in:
- lib/stocker.rb
Overview
This is the “God” object that interacts with Thor’s methods.
Instance Method Summary collapse
-
#buy(item) ⇒ Object
Open URL of item.
-
#check ⇒ Object
Checks the total number of items on hand against their acceptable minimum values and opens the URLs of any items running low in stock.
-
#config ⇒ Hash
Configure Stocker’s default settings If the –url param is not passed, no configurations will be changed.
-
#count ⇒ Object
Do a count update for all inventory items interactively.
-
#csv ⇒ String
Output a CSV dump of the entire inventory.
-
#delete(item) ⇒ Hash
Deletes an item from the inventory.
-
#list ⇒ Object
Print a list of all inventory items.
-
#min(item, min) ⇒ Object
Set minimum acceptable amount of existing inventory item.
-
#new(item, total) ⇒ Hash
Creates a new item in the inventory.
-
#total(item, total) ⇒ Object
Set total of existing item in Stocker’s inventory.
-
#url(item, url) ⇒ Object
Set URL of existing item in Stocker’s inventory.
Instance Method Details
#buy(item) ⇒ Object
Open URL of item
84 85 86 87 |
# File 'lib/stocker.rb', line 84 def buy(item) match_name(item) `open -a Safari #{read_file[@@item]["url"]}` end |
#check ⇒ Object
Checks the total number of items on hand against their acceptable minimum values and opens the URLs of any items running low in stock.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/stocker.rb', line 44 def check links = [] read_file.each do |key, value| value["checked"] = Time.now if value["total"] < value["min"] puts "You're running low on #{key}!" links << key end end links.uniq! links.each { |link| buy(link)} end |
#config ⇒ Hash
Configure Stocker’s default settings If the –url param is not passed, no configurations will be changed.
174 175 176 177 178 |
# File 'lib/stocker.rb', line 174 def config settings = read_config settings['url'] = [:url] || 'http://amazon.com' write_config(settings) end |
#count ⇒ Object
Do a count update for all inventory items interactively. Stocker#count will loop through the inventory and ask you for the total on hand count for each item. When the counting is finished, Stocker will run Stocker#check and open the URLs of any low stock items.
104 105 106 107 108 109 110 111 112 |
# File 'lib/stocker.rb', line 104 def count values = read_file values.each do |key, value| value["checked"] = Time.now value["total"] = ask("#{key.titlecase}:").to_i end write_file(values) invoke :check end |
#csv ⇒ String
Output a CSV dump of the entire inventory. This is useful with ‘awk` and `sed`.
117 118 119 |
# File 'lib/stocker.rb', line 117 def csv read_file.each { |key, value| puts "#{key.titlecase},#{value['total']},#{value['min']},#{value['url']},#{value['checked']}" } end |
#delete(item) ⇒ Hash
Deletes an item from the inventory. Stocker will attempt a “fuzzy match” of the item name.
34 35 36 37 38 39 |
# File 'lib/stocker.rb', line 34 def delete(item) data = read_file match_name(item) data.delete(@@item) write_file(data) end |
#list ⇒ Object
Print a list of all inventory items. Green items are well stocked. Yellow items are at minimum acceptable total. Red items are below minimum acceptable total.
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 157 158 159 160 161 162 163 164 |
# File 'lib/stocker.rb', line 132 def list begin @header = [["", ""]] # @header = [[set_color("Item", :white), set_color("Total", :white)], [set_color("=================", :white), set_color("=====", :white)]] @green = [] @yellow = [] @yellow2 = [] @green2 = [] @red = [] @red2 = [] read_file.each do |key, value| if value['total'] > value['min'] @green += [[key.titlecase,value['total'], value['total']-value['min']]] elsif value['total'] == value['min'] @yellow += [[key.titlecase,value['total'], value['total']-value['min']]] else @red += [[key.titlecase,value['total'], value['total']-value['min']]] end end @green.sort_by! { |a,b,c| c } @yellow.sort_by! { |a,b,c| c } @red.sort_by! { |a,b,c| c } @green.reverse! @yellow.reverse! @red.reverse! @green.each { |a,b| @green2 += [[set_color(a, :green), set_color(b, :green)]] } @yellow.each { |a,b| @yellow2 += [[set_color(a, :yellow), set_color(b, :yellow)]] } @red.each { |a,b| @red2 += [[set_color(a, :red), set_color(b, :red)]] } print_table(@header + @green2 + @yellow2 + @red2,{indent: 2}) rescue Exception => e puts "Inventory empty" end end |
#min(item, min) ⇒ Object
Set minimum acceptable amount of existing inventory item
93 94 95 96 97 98 |
# File 'lib/stocker.rb', line 93 def min(item, min) data = read_file match_name(item) data[@@item]["min"] = min.to_i write_file(data) end |
#new(item, total) ⇒ Hash
Creates a new item in the inventory
23 24 25 26 27 |
# File 'lib/stocker.rb', line 23 def new(item, total) data = read_file data[item] = {'total' => total.to_i, 'min' => [:minimum].to_i, 'url' => [:url] || read_config['url'], 'checked' => Time.now} write_file(data) end |
#total(item, total) ⇒ Object
Set total of existing item in Stocker’s inventory
61 62 63 64 65 66 67 |
# File 'lib/stocker.rb', line 61 def total(item, total) data = read_file match_name(item) data[@@item]["total"] = total.to_i time(item) write_file(data) end |
#url(item, url) ⇒ Object
Set URL of existing item in Stocker’s inventory
73 74 75 76 77 78 79 |
# File 'lib/stocker.rb', line 73 def url(item, url) data = read_file match_name(item) data[@@item]["url"] = url time(item) write_file(data) end |