-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipperz2csv.rb
92 lines (65 loc) · 1.45 KB
/
clipperz2csv.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'csv'
require 'pp'
JSON_FILE = 'c.json'
CSV_FILE = 'passwords.csv'
json = File.read(JSON_FILE)
ary = JSON.parse(json)
def get_values(c_hash)
# Get the array of hashes where all particular values are stored.
cv_hash = c_hash['currentVersion']
f_hash = cv_hash['fields']
v_ary = f_hash.values
end
def get_by_key(c_hash, key)
# Get the array of particular keys from the array of hashes.
k_ary = []
v_ary = get_values(c_hash)
v_ary.each do |value|
k_ary << value[key]
end
k_ary.map { |str| str == '"' ? '' : str }
end
def get_titles(c_hash)
# Get fields titles to populate the table.
t_ary = []
t_ary << 'label'
t_ary << get_by_key(c_hash, 'label')
t_ary << 'notes'
t_ary.flatten!
end
def parse_a_card(c_hash)
# Return values of all fields.
csv_row = []
csv_row << c_hash['label'].strip
csv_row << get_by_key(c_hash, 'value')
csv_row << c_hash['data']['notes'].strip
csv_row.flatten!
end
def _debug
# Printf debugging.
c_hash = ary[100]
pp get_values(c_hash)
pp get_titles(c_hash)
end
def get_my_passwords(ary)
# Get the array of all the rows.
r_ary = []
ary.each do |hash|
r_ary << parse_a_card(hash)
end
r_ary
end
def to_csv(ary)
# Finally.
r_ary = get_my_passwords(ary)
CSV.open(CSV_FILE, "w") do |csv|
csv << get_titles(ary[0])
r_ary.each do |row|
csv << row
end
end
end
to_csv(ary)