spreadsheet_to_json 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18657291c808206aca46ce741a081cf1ecdc704c
4
- data.tar.gz: d98ef0e39f714f8ba6bccf2507608c5ebe0f5372
3
+ metadata.gz: a42faa6b5d9367ed8f14491b01e595507ff58ed5
4
+ data.tar.gz: 8f70bf37a7a2dfd709183b3bff44852583fc1c27
5
5
  SHA512:
6
- metadata.gz: 1e408913b1861712639a8649797661dda88cc403eb478d48cd6e2c8ffe8f12c0fe5a92c0868aaa71b3a581943a2a92f87b176b4f1ea59807ba189fb8542d593b
7
- data.tar.gz: 430fa204dcceb6a6f741c56fbcfc4f91c6a462ed1b90ca36c8710515dda9c8bdfa817ad332b1d739533df5c53250d525e710fc145527f68bd16360c6e2fd585d
6
+ metadata.gz: 6868a8ef44a9bca74de6aa9525cc0dff40a91d3032284280d4fb3870b22c719aa32a720968b052538aa652262499ddedc8ace45d4a889cd2e24c6b13eab20e6f
7
+ data.tar.gz: 4c4e0ebb74f77d2b29924194b9741e39cbf4b68767a99f5c71d57758d818c6fb075bc57fb3f382cb2bdb150103d8d13fc63f305948d5721ca8cc9112baf04cf8
@@ -0,0 +1,30 @@
1
+ require 'google_drive'
2
+ require 'yaml'
3
+ require 'json'
4
+ require './lib/spreadsheet'
5
+
6
+ class JsonToSpreadsheet
7
+ def initialize(spreadsheet)
8
+ @spreadsheet = spreadsheet
9
+ end
10
+
11
+ def convert_json_to_sheet(json_file_path, sheet_name)
12
+ arrays = json_to_hasharray(File.read(json_file_path))
13
+ puts (arrays.class)
14
+ if arrays.count <= 0
15
+ puts "The JSON file is empty"
16
+ exit
17
+ end
18
+ worksheet = @spreadsheet.get_sheet_by_name(sheet_name)
19
+ worksheet.list.keys = arrays[0].keys
20
+ arrays.each do |each|
21
+ worksheet.list.push(each)
22
+ end
23
+ worksheet.save
24
+ end
25
+
26
+ private
27
+ def json_to_hasharray(json_content)
28
+ JSON.parse(json_content)
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ require 'google_drive'
2
+ require './lib/util/auth'
3
+
4
+ class Spreadsheet
5
+ def initialize(config_path)
6
+ session = GoogleDrive.login_with_oauth(Auth.get_auth_token(config_path))
7
+ @instance = session.spreadsheet_by_key(Auth.get_settings_from_yml(config_path)['spreadsheet_key'])
8
+ end
9
+
10
+ def get_sheet_by_id(worksheet_id=0)
11
+ @instance.worksheets[worksheet_id]
12
+ end
13
+
14
+ def get_sheet_by_name(worksheet_name)
15
+ @instance.worksheet_by_title(worksheet_name)
16
+ end
17
+ end
18
+
@@ -2,103 +2,72 @@
2
2
  require 'google_drive'
3
3
  require 'yaml'
4
4
  require 'json'
5
+ require './lib/spreadsheet'
5
6
 
6
- module SpreadsheetToJson
7
- class << self
8
- def convert_rows_by_sheet_id(config_path, worksheet_id, rows, keys_row_num)
9
- rows_to_json(
10
- get_sheet_by_id(worksheet_id, config_path),
11
- rows,
12
- keys_row_num
13
- )
14
- end
15
-
16
- def convert_rows_by_sheet_name(config_path, worksheet_name, rows, keys_row_num)
17
- rows_to_json(
18
- get_sheet_by_name(worksheet_name, config_path),
19
- rows,
20
- keys_row_num
21
- )
22
- end
23
-
24
- def convert_a_worksheet_by_name(config_path, worksheet_name, keys_row_num)
25
- worksheet = get_sheet_by_name(worksheet_name, config_path)
26
- rows_to_json(
27
- worksheet,
28
- [*keys_row_num+1..worksheet.num_rows],
29
- keys_row_num
30
- )
31
- end
7
+ class SpreadsheetToJson
8
+ def initialize(spreadsheet)
9
+ @spreadsheet = spreadsheet
10
+ end
32
11
 
33
- def convert_a_worksheet_by_id(config_path, worksheet_id, keys_row_num)
34
- worksheet = get_sheet_by_id(worksheet_id, config_path)
35
- rows_to_json(
36
- worksheet,
37
- [*keys_row_num+1..worksheet.num_rows],
38
- keys_row_num
39
- )
40
- end
12
+ def convert_rows_by_sheet_id(worksheet_id, rows, keys_row_num)
13
+ rows_to_json(
14
+ @spreadsheet.get_sheet_by_id(worksheet_id),
15
+ rows,
16
+ keys_row_num
17
+ )
18
+ end
41
19
 
42
- def output_json_file(content, output_path)
43
- unless File.exist?(output_path)
44
- File.new(output_path, 'w+')
45
- end
46
- File.open(output_path, 'w+') {|file| file.write(content) }
47
- end
20
+ def convert_rows_by_sheet_name(worksheet_name, rows, keys_row_num)
21
+ rows_to_json(
22
+ @spreadsheet.get_sheet_by_name(worksheet_name),
23
+ rows,
24
+ keys_row_num
25
+ )
26
+ end
48
27
 
49
- private
50
- def rows_to_json(spreadsheet, rows, keys_row_num)
51
- rows.collect {|row_num| construct_hash(spreadsheet, keys_row_num-2, row_num-2)}.to_json
52
- end
28
+ def convert_a_worksheet_by_name(worksheet_name, keys_row_num)
29
+ worksheet = @spreadsheet.get_sheet_by_name(worksheet_name)
30
+ rows_to_json(
31
+ worksheet,
32
+ [*keys_row_num+1..worksheet.num_rows],
33
+ keys_row_num
34
+ )
35
+ end
53
36
 
54
- def get_sheet_by_id(worksheet_id=0, config_path)
55
- session = GoogleDrive.login_with_oauth(init_auth_token(config_path))
56
- session.spreadsheet_by_key(get_settings_from_yml(config_path)['spreadsheet_key']).
57
- worksheets[worksheet_id]
58
- end
37
+ def convert_a_worksheet_by_id(worksheet_id, keys_row_num)
38
+ worksheet = @spreadsheet.get_sheet_by_id(worksheet_id)
39
+ rows_to_json(
40
+ worksheet,
41
+ [*keys_row_num+1..worksheet.num_rows],
42
+ keys_row_num
43
+ )
44
+ end
59
45
 
60
- def get_sheet_by_name(worksheet_name, config_path)
61
- session = GoogleDrive.login_with_oauth(init_auth_token(config_path))
62
- session.spreadsheet_by_key(get_settings_from_yml(config_path)['spreadsheet_key']).
63
- worksheet_by_title(worksheet_name)
46
+ def output_json_file(content, output_path)
47
+ unless File.exist?(output_path)
48
+ File.new(output_path, 'w+')
64
49
  end
50
+ File.open(output_path, 'w+') {|file| file.write(content) }
51
+ end
65
52
 
66
- def init_auth_token(config_path)
67
- config = get_settings_from_yml(config_path)
68
- client = OAuth2::Client.new(
69
- config['auth']['client_id'],
70
- config['auth']['client_secret'],
71
- site: config['auth']['site'],
72
- token_url: config['auth']['token_url'],
73
- authorize_url: config['auth']['token_url']
74
- )
75
- auth_token = OAuth2::AccessToken.from_hash(
76
- client,
77
- {
78
- :refresh_token => config['auth']['refresh_token'],
79
- :expires_at => 3600
80
- }
81
- )
82
- auth_token.refresh!.token
83
- end
53
+ private
54
+ def rows_to_json(worksheet, rows, keys_row_num)
55
+ rows.collect {|row_num| construct_hash(worksheet, keys_row_num-2, row_num-2)}.to_json
56
+ end
84
57
 
85
- def get_settings_from_yml(path)
86
- File.open(path) { |file| YAML.load(file) }
58
+ def construct_hash(worksheet, keys_row_num, row=1)
59
+ if row < 1
60
+ puts "illeagal row num"
61
+ exit
87
62
  end
88
63
 
89
- def construct_hash(spreadsheet, keys_row_num, row=1)
90
- if row < 1
91
- puts "illeagal row num"
92
- end
93
-
94
- begin
95
- json_keys = spreadsheet.list[keys_row_num].to_hash.values
96
- rescue NoMethodError
97
- puts "worksheet_id is not exist"
98
- exit
99
- end
100
- json_values = spreadsheet.list[row].to_hash.values
101
- Hash[json_keys.zip(json_values)]
64
+ begin
65
+ json_keys = worksheet.list[keys_row_num].to_hash.values
66
+ rescue NoMethodError
67
+ puts "worksheet_id is not exist"
68
+ exit
102
69
  end
70
+ json_values = worksheet.list[row].to_hash.values
71
+ Hash[json_keys.zip(json_values)]
103
72
  end
104
73
  end
data/lib/util/auth.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'google_drive'
2
+
3
+ class Auth
4
+ class << self
5
+ def get_auth_token(config_path)
6
+ config = get_settings_from_yml(config_path)
7
+ client = OAuth2::Client.new(
8
+ config['auth']['client_id'],
9
+ config['auth']['client_secret'],
10
+ site: config['auth']['site'],
11
+ token_url: config['auth']['token_url'],
12
+ authorize_url: config['auth']['token_url']
13
+ )
14
+ auth_token = OAuth2::AccessToken.from_hash(
15
+ client,
16
+ {
17
+ :refresh_token => config['auth']['refresh_token'],
18
+ :expires_at => 3600
19
+ }
20
+ )
21
+ auth_token.refresh!.token
22
+ end
23
+
24
+ def get_settings_from_yml(path)
25
+ File.open(path) { |file| YAML.load(file) }
26
+ end
27
+ end
28
+ end
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.3
4
+ version: 0.0.4
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-19 00:00:00.000000000 Z
11
+ date: 2015-06-20 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
@@ -18,7 +18,10 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - lib/json_to_spreadsheet.rb
22
+ - lib/spreadsheet.rb
21
23
  - lib/spreadsheet_to_json.rb
24
+ - lib/util/auth.rb
22
25
  homepage: https://github.com/jackxu/google_spreadsheet_to_json
23
26
  licenses:
24
27
  - MIT