Class: Stockr::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/stockr/export.rb

Class Method Summary collapse

Class Method Details

.allObject



15
16
17
# File 'lib/stockr/export.rb', line 15

def all
  @all ||= Part.search("") || []
end

.csvObject



49
50
51
52
53
54
55
56
# 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

.filenameObject



41
42
43
# File 'lib/stockr/export.rb', line 41

def filename
  "my_stockr_" + timestamp
end

.format(data = Part.all) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

.htmlObject



58
59
# File 'lib/stockr/export.rb', line 58

def html
end

.pdfObject



61
62
# File 'lib/stockr/export.rb', line 61

def pdf
end

.timestampObject



19
20
21
# File 'lib/stockr/export.rb', line 19

def timestamp
  @ts ||= Time.now.strftime("%y-%m-%d-%H-%M-%S")
end

.to_webObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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

.txtObject



45
46
47
# File 'lib/stockr/export.rb', line 45

def txt
  write(format, "txt")
end

.write(txt, ext, path = nil) ⇒ Object



67
68
69
70
71
72
# 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

.xmlObject



64
65
# File 'lib/stockr/export.rb', line 64

def xml
end