Module: Pyradise

Defined in:
lib/pyradise.rb

Constant Summary collapse

CONF =
YAML.load(File.new(HOME + "/conf.yml"))
RATE =
CONF[:rate] || 2.0
TAX =
CONF[:tax] || 1.3

Class Method Summary collapse

Class Method Details

.clearObject



78
79
80
# File 'lib/pyradise.rb', line 78

def clear
  Product.dataset.delete
end

.fetchObject



40
41
42
# File 'lib/pyradise.rb', line 40

def fetch
  create from_stores
end

.info(sid = nil) ⇒ Object Also known as: show



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pyradise.rb', line 61

def info(sid=nil)
  if !sid
    puts red("Use: pyradise view <ID>")
  elsif !prod = Product.filter(:sid => sid.to_i).first
    puts yellow("Product not found.")
  else
    w = terminal_size[0] - 20
    prices = prod.prices
    max = prices.values.max.to_f
    prices.keys.sort.each do |k|
      rel = "=" * (prices[k]  / max * w)
      puts "#{Time.at(k).strftime('%M/%d')} #{rel}| #{prices[k]}"
    end
  end
end

.list(*query) ⇒ Object Also known as: search



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pyradise.rb', line 44

def list(*query)
  t = Time.now
  w = terminal_size[0]
  s = w - 35
  puts "Searching #{'"' + query[0] + '"' if query[0]}... Order by: #{query[1] || 'name'}"
  puts "_" * w
  prods = Product.filter(:name.like("%#{query[0]}%")).order(query[1] ? query[1].to_sym : :name)
  prods.each_with_index do |prod, i|
    name = prod.name.length > s ? prod.name[0..s] + ".." : prod.name
    out = sprintf("%-6s | %-5s | %-#{w-38}s %-3d |  R$ %d", prod.store, prod.sid,  name,  prod.price, prod.price * RATE * TAX)
    puts i % 2 == 0 ? bold(out) : out
  end
  puts "_" * w
  puts green("Total: #{prods.all.length} (#{Time.now - t}s)")
end

.run!(comm, opts) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/pyradise.rb', line 31

def run! comm, opts
  if respond_to? comm[0]
    send(*comm)
  else
    puts "Can't do that..."
    exit
  end
end