-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep1.rb
47 lines (33 loc) · 824 Bytes
/
step1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Couchbase Ruby SDK quick-start part 1
require 'csv'
require 'couchbase'
class StockList
def initialize
@list = { }
end
def import_csv(filename, db)
CSV.foreach(filename, headers: true) do |row|
@list[row[0].strip] = [row[1].strip, row[2].to_i] unless row.empty?
end
end
def all
@list.each { |key, value| puts key, value }
end
def find(key)
puts key
puts @list[key]
end
def save(db)
@list.each do |key, array|
doc = {"Description" => array[0], "Quantity" => array[1]}
db.set(key, doc)
end
end
end
db = Couchbase.connect(:hostname => "localhost", :key_prefix => "swag_")
stock_list = StockList.new
stock_list.import_csv("stock.csv", db)
stock_list.all
stock_list.save(db)
item = db.get("STICKERCBLOGO")
puts "#{item['Description']}, Quantity: #{item['Quantity']}"