class Stockr::Export

Public Class Methods

all() click to toggle source
# File lib/stockr/export.rb, line 15
def all
  @all ||= Part.search("") || []
end
csv() click to toggle source
# File lib/stockr/export.rb, line 49
def csv
  output = "name,qty,price\n"
  for p in all
    output << [p.name, p.qty, p.price].join(",")
  end
  output << "\n"
  write(output, "csv")
end
filename() click to toggle source
# File lib/stockr/export.rb, line 41
def filename
  "my_stockr_" + timestamp
end
format(data = Part.all) click to toggle source
# File lib/stockr/export.rb, line 23
      def format(data = Part.all)
        return "Nada." unless data && !data.empty?
        <<TXT
#
#  Stockr Export
#
#  #{Time.now.strftime("%d/%b/%Y %H:%M:%S")}
#
---------------------------------------------------------------------

#{data.map(&:line).join("\n")}

---------------------------------------------------------------------
Total: #{data.size} Part(s).                                    #{Part.sum(data)}

TXT
      end
html() click to toggle source
# File lib/stockr/export.rb, line 58
def html
end
pdf() click to toggle source
# File lib/stockr/export.rb, line 61
def pdf
end
timestamp() click to toggle source
# File lib/stockr/export.rb, line 19
def timestamp
  @ts ||= Time.now.strftime("%y-%m-%d-%H-%M-%S")
end
to_web() click to toggle source
# File lib/stockr/export.rb, line 74
def to_web
  user, pass = Stockr.get_user unless user
  file = write(format, :txt, "/tmp/")
  puts "Exporting db to techub..."
  url = URI.parse(TECHUB + "/push")
  File.open(file) do |f|
    Net::HTTP.start(url.host, url.port) do |http|
      req = Net::HTTP::Post::Multipart.new url.path, "file" => UploadIO.new(f, "text/plain", file)
      req.basic_auth user, pass if pass
      puts "Sending file..."
      http.request(req)
      puts req.body
    end
    puts "Done."
  end


end
txt() click to toggle source
# File lib/stockr/export.rb, line 45
def txt
  write(format, "txt")
end
write(txt, ext, path = nil) click to toggle source
# File lib/stockr/export.rb, line 67
def write(txt, ext, path = nil)
  fname = filename + ".#{ext}"
  fname = path + fname if path
  File.open(fname, "w") { |f| f << txt }
  fname
end
xml() click to toggle source
# File lib/stockr/export.rb, line 64
def xml
end