spreadsheet_to_json 0.0.1 → 0.0.2

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/spreadsheet_to_json.rb +17 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6ea1f08ddb691d4358cc326c599e938c7156ee8
4
- data.tar.gz: a73b2304166583529583afbeac2eb2187d1d9a40
3
+ metadata.gz: 20579b86b447e68124afe0d5c2b155565022174d
4
+ data.tar.gz: 0d8a1b0c58519ddf3c0ed77b97fe168932e8d622
5
5
  SHA512:
6
- metadata.gz: 52b084a9cd9ef1d174c6c2d5dbc2fc7335fc91d017a38079912865621e02c69cd572d2ba362897687abf5c2e228ebcf0bd815edbb4ce62580b86871c6d393a9e
7
- data.tar.gz: a908ba349004bc4413965c3219ae660a4f7445a7d3a2baefb520f4017d902408d714ecdf9c2723999e3a22e181d39a1fa7e8aaaf82ad8c955825db01abca0034
6
+ metadata.gz: 3bab23c043dfa357b2f6a58367cd44fc0745377e4f9af7774a32c8682aa2d2e89fcac49d0c87d7d753b56cd63f3d151cff0ff3d87e8584543a319b7996572da7
7
+ data.tar.gz: 3692f977b977f922b24aea7ffe5d5796d62841c8a22b14eb2dd2f6e2ad1079f3574c443af1934e18a0bcbb613fa4531c2ba11ca873b2d6372390f4d705fafd33
@@ -5,19 +5,27 @@ require 'json'
5
5
 
6
6
  module SpreadsheetToJson
7
7
  class << self
8
- def convert_to_json(worksheet_id=0, rows=[*2..get_spreadsheet.num_rows-2], keys_row_num)
9
- spreadsheet = get_spreadsheet(worksheet_id)
8
+ def convert_to_json(config_path, worksheet_id=0, rows=[*2..get_spreadsheet.num_rows-2], keys_row_num)
9
+ spreadsheet = get_spreadsheet(worksheet_id, config_path)
10
10
  rows.collect {|row_num| construct_hash(spreadsheet, keys_row_num-2, row_num-2)}.to_json
11
11
  end
12
12
 
13
+ def output_json_file(content, output_path)
14
+ unless File.exist?(output_path)
15
+ File.new(output_path, 'w+')
16
+ end
17
+ File.open(output_path, 'w+') {|file| file.write(content) }
18
+ end
19
+
13
20
  private
14
- def get_spreadsheet(worksheet_id=0)
15
- session = GoogleDrive.login_with_oauth(init_auth_token)
16
- session.spreadsheet_by_key(get_settings_from_yml['spreadsheet_key']).worksheets[worksheet_id]
21
+ def get_spreadsheet(worksheet_id=0, config_path)
22
+ session = GoogleDrive.login_with_oauth(init_auth_token(config_path))
23
+ session.spreadsheet_by_key(get_settings_from_yml(config_path)['spreadsheet_key']).
24
+ worksheets[worksheet_id]
17
25
  end
18
26
 
19
- def init_auth_token
20
- config = get_settings_from_yml
27
+ def init_auth_token(config_path)
28
+ config = get_settings_from_yml(config_path)
21
29
  client = OAuth2::Client.new(
22
30
  config['auth']['client_id'],
23
31
  config['auth']['client_secret'],
@@ -35,8 +43,8 @@ module SpreadsheetToJson
35
43
  auth_token.refresh!.token
36
44
  end
37
45
 
38
- def get_settings_from_yml
39
- File.open("./spreadsheet_access.yml") { |file| YAML.load(file) }
46
+ def get_settings_from_yml(path)
47
+ File.open(path) { |file| YAML.load(file) }
40
48
  end
41
49
 
42
50
  def construct_hash(spreadsheet, keys_row_num, row=1)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spreadsheet_to_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xu Jianyong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-16 00:00:00.000000000 Z
11
+ date: 2015-06-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  A gem to help you convert your google spreadsheet data to json format. With this gem, you can