google_sheets 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -2
  3. data/lib/google_sheets/sheet.rb +28 -0
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 598703000a2529218f78b0bb7f0c049aec30d79ce9f3e309ebeab51a5c6d3451
4
- data.tar.gz: 0fed844fbb50b5cb4275f647d4b8704ddec1ba90e7a69194498b0180ad98da22
3
+ metadata.gz: a0513b5f7ab0fcb68e08c1d80130fc6462c2905553ade3289c77c326c2f5fcc1
4
+ data.tar.gz: e50f4db63195185d320d6106d5a82fcaf77585f161284099d8d93b8ac2ab53c2
5
5
  SHA512:
6
- metadata.gz: c439fe3506db60ae0cfd37f26822a7525ed120b55a237be5eff4aea121d9d2f083d7bbfe897444a92ad5735d0550bdde3096c9a5d36754eb42d311749f922bc2
7
- data.tar.gz: 69a0e9ef52693cf4dfd6011a1404288f16db25d285a60a1ad53c57b6c3cfe3f51f5b4542bc4f343387006c199dfa219e8ebd3c324efea295c20e2e08143330ab
6
+ metadata.gz: 160336d9e34ebe9e3c4c00c5425cf7a170305d451d3d942d0593f984a481449354542aba2d4243f2e9fd6d0f499f60f1a27fc4858d27c4390923302981b28d90
7
+ data.tar.gz: 60eb1a779b362f57ab79d2424485118e502d39331339e90d1ed222a4edd34c45a6f9a3f38d3cbc6f54749b3bcd12423b008cfc76b075efdbd307af8045fd5370
data/README.md CHANGED
@@ -70,7 +70,7 @@ spreadsheet.sheets.map &:title
70
70
  sheet1 = spreadsheet.sheets[0]
71
71
 
72
72
  sheet1.values
73
- # => [['one','two'], ['three', 'four']
73
+ # => [['first, 'last', 'age'], ['bob', 'jones', '92'], ['steve', 'johnson', '22']]
74
74
 
75
75
  values = [[1,2],[3,4]]
76
76
 
@@ -80,10 +80,29 @@ spreadsheet.sheets.map &:title
80
80
  # => ['Sheet1', 'yoyo1', 'what']
81
81
 
82
82
  # this will delete the sheet!!!
83
- sheet.delete!
83
+ sheet2.delete!
84
84
 
85
85
  spreadsheet.sheets.map &:title
86
86
  # => ['Sheet1', 'yoyo1']
87
+
88
+ # Sheet#to_json converts the csv to a json array
89
+ # it uses the top row as the keys
90
+ # fyi, this will also convert the values to UTF-8
91
+ # sometimes gsheets values come in as ASCII
92
+ sheet1.to_json
93
+ # =>
94
+ # [
95
+ # {
96
+ # first: 'bob',
97
+ # last: 'jones',
98
+ # age: '92'
99
+ # },
100
+ # {
101
+ # first: 'steve',
102
+ # last: 'johnson',
103
+ # age: '22'
104
+ # }
105
+ # ]
87
106
  ```
88
107
 
89
108
  Or just look at [the spec](spec/test_all_the_things_spec.rb) to see it in action.
@@ -35,5 +35,33 @@ module GoogleSheets
35
35
 
36
36
  self
37
37
  end
38
+
39
+ def to_json
40
+ top_row = values[0].map &:to_sym
41
+ hashify_data(values[1..-1], top_row)
42
+ end
43
+
44
+ private
45
+
46
+ def hashify_data csv, top_row
47
+ csv.map do |arr|
48
+ hash = {}
49
+
50
+ top_row.each_with_index do |attr, index|
51
+ hash[attr.to_sym] = utf8ify(arr[index])
52
+ end
53
+
54
+ hash
55
+ end
56
+ end
57
+
58
+ def utf8ify val
59
+ if val.class == String && val.encoding.to_s != 'UTF-8'
60
+ val = val.dup.force_encoding('utf-8')
61
+ end
62
+
63
+ val
64
+ end
65
+
38
66
  end
39
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_sheets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Murphy