class Stocker::Generator

This is the “God” object that interacts with Thor's methods.

Public Instance Methods

buy(item) click to toggle source

Open URL of item @param item [String] existing item in inventory

# File lib/stocker.rb, line 84
def buy(item)
  match_name(item)
  `open -a Safari #{read_file[@@item]["url"]}`
end
check() click to toggle source

Checks the total number of items on hand against their acceptable minimum values and opens the URLs of any items running low in stock. @return Opens a link in default web browser if URL is set for low stock item

# 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() click to toggle source

Configure Stocker's default settings If the –url param is not passed, no configurations will be changed. @param –url [String] Sets the default URL that is set for newly created inventory items @return [Hash] Hash of configuration settings saved to a YAML file @example

"nikki config --url 'http://amazon.com'"
# File lib/stocker.rb, line 174
def config
  settings = read_config
  settings['url'] = options[:url] || 'http://amazon.com'
  write_config(settings)
end
config_file() click to toggle source
# File lib/stocker.rb, line 185
def config_file
  path.join(".stocker.config.yaml")
end
config_file_exist?() click to toggle source
# File lib/stocker.rb, line 214
def config_file_exist?
  return true if config_file.exist?
  create_config_file
  return true
end
count() click to toggle source

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.

# 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
create_config_file() click to toggle source
# File lib/stocker.rb, line 226
def create_config_file
  FileUtils.touch(config_file)
  settings = {}
  settings['url'] = 'http://amazon.com'
  write_config(settings)
end
create_file() click to toggle source
# File lib/stocker.rb, line 233
def create_file
  FileUtils.touch(file)
end
csv() click to toggle source

Output a CSV dump of the entire inventory. This is useful with `awk` and `sed`. @return [String] CSV of entire inventory

# 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) click to toggle source

Deletes an item from the inventory. Stocker will attempt a “fuzzy match” of the item name. @param item [String] The item to delete from the inventory @return [Hash] Returns a hash of the updated inventory and writes YAML to .stocker.yaml

# File lib/stocker.rb, line 34
def delete(item)
  data = read_file
  match_name(item)
  data.delete(@@item)
  write_file(data)
end
editor() click to toggle source
# File lib/stocker.rb, line 202
def editor
  read_config[:editor]
end
file() click to toggle source
# File lib/stocker.rb, line 206
def file
  path.join("#{ENV['HOME']}/.stocker.yaml")
end
file_exist?() click to toggle source
# File lib/stocker.rb, line 220
def file_exist?
  return true if file.exist?
  create_file
  return true
end
list() click to toggle source
# 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
match_name(item) click to toggle source
# File lib/stocker.rb, line 248
def match_name(item)
  read_file.each_key { |key| @@item = key if key =~ /#{item}/ }
end
min(item, min) click to toggle source

Set minimum acceptable amount of existing inventory item @param item [String] item to update @param min [String] acceptable minimum amount of item to always have on hand

# 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) click to toggle source

Creates a new item in the inventory @param item [String] The item to add to the inventory @param total [String] How many of the new item on hand @return [Hash] Returns a hash of the updated inventory and writes YAML to .stocker.yaml

# File lib/stocker.rb, line 23
def new(item, total)
    data = read_file
    data[item] = {'total' => total.to_i, 'min' => options[:minimum].to_i, 'url' => options[:url] || read_config['url'], 'checked' => Time.now}
    write_file(data)
end
path() click to toggle source
# File lib/stocker.rb, line 181
def path
  Pathname.new("#{ENV['HOME']}/")
end
read_config() click to toggle source
# File lib/stocker.rb, line 189
def read_config
  config_file_exist?
  YAML.load(config_file.read)
end
read_file() click to toggle source
# File lib/stocker.rb, line 210
def read_file
  YAML.load(file.read)
end
time(item) click to toggle source
# File lib/stocker.rb, line 241
def time(item)
  data = read_file
  match_name(item)
  data[@@item]["checked"] = Time.now
  write_file(data)
end
today() click to toggle source
# File lib/stocker.rb, line 237
def today
  Date.today
end
total(item, total) click to toggle source

Set total of existing item in Stocker's inventory @param item [String] item to update @param total [String] total on hand

# 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) click to toggle source

Set URL of existing item in Stocker's inventory @param item [String] item to update @param url [String] URL of item

# 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
write_config(hash) click to toggle source
# File lib/stocker.rb, line 198
def write_config(hash)
  config_file.open('w') { |t| t << hash.to_yaml }
end
write_file(hash) click to toggle source
# File lib/stocker.rb, line 194
def write_file(hash)
  file.open('w') { |t| t << hash.to_yaml }
end