Class: Stocker::Generator

Inherits:
Thor
  • Object
show all
Defined in:
lib/stocker.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#buy(item) ⇒ Object

Open URL of item

Parameters:

  • item (String)

    existing item in inventory



84
85
86
87
# File 'lib/stocker.rb', line 84

def buy(item)
  match_name(item)
  `open -a Safari #{read_file[@@item]["url"]}`
end

#checkObject

Checks the total number of items on hand against their acceptable minimum values and opens the URLs of any items running low in stock.

Returns:

  • Opens a link in default web browser if URL is set for low stock item



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

#configHash

Configure Stocker’s default settings If the –url param is not passed, no configurations will be changed.

Examples:

"nikki config --url 'http://amazon.com'"

Parameters:

  • --url (String)

    Sets the default URL that is set for newly created inventory items

Returns:

  • (Hash)

    Hash of configuration settings saved to a YAML file



174
175
176
177
178
# File 'lib/stocker.rb', line 174

def config
  settings = read_config
  settings['url'] = options[:url] || 'http://amazon.com'
  write_config(settings)
end

#countObject

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

#csvString

Output a CSV dump of the entire inventory. This is useful with ‘awk` and `sed`.

Returns:

  • (String)

    CSV of entire inventory



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.

Parameters:

  • item (String)

    The item to delete from the inventory

Returns:

  • (Hash)

    Returns a hash of the updated inventory and writes YAML to .stocker.yaml



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

#listObject

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

Parameters:

  • item (String)

    item to update

  • min (String)

    acceptable minimum amount of item to always have on hand



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

Parameters:

  • item (String)

    The item to add to the inventory

  • total (String)

    How many of the new item on hand

Returns:

  • (Hash)

    Returns a hash of the updated inventory and writes YAML to .stocker.yaml



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' => options[:minimum].to_i, 'url' => options[:url] || read_config['url'], 'checked' => Time.now}
    write_file(data)
end

#total(item, total) ⇒ Object

Set total of existing item in Stocker’s inventory

Parameters:

  • item (String)

    item to update

  • total (String)

    total on hand



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

Parameters:

  • item (String)

    item to update

  • url (String)

    URL of item



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