Class: Stockr::Import

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

Class Method Summary collapse

Class Method Details

.from_file(file) ⇒ Object



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

def from_file(file)
  File.open(file).each_line { |l| import(l) }
  "Loaded"
end

.from_web(user = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stockr/import.rb', line 23

def from_web(user = nil)
  user, pass = Stockr.get_user unless user
  puts "Downloading dump file...#{TECHUB}/#{user}.txt"
  puts url = URI.parse("#{TECHUB}/#{user}.txt")
  Net::HTTP.start(url.host, url.port) { |http|
    req = Net::HTTP::Get.new("/#{user}.txt")
    req.basic_auth user, pass if pass
    res = http.request(req)
    puts "File downloaded, parsing..."
    res.body.each_line { |l| import(l) }
  }
  puts "Done"
  # puts res.body


end

.import(l) ⇒ Object



11
12
13
14
15
16
# File 'lib/stockr/import.rb', line 11

def import(l)
  puts "Importing #{l}" #if Debug
  _, qty, name, price = l.match(/^\s*([0-9]*)x\s*([^\.\s]*)\s*\.*\s*(\d*\.?\d*)/).to_a
  return unless qty || name
  Part.find_or_create(qty, name, price)
end