google_spreadsheet_recorder 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9912e2b49112bbebce74a19fc12e423c9130b9f0
4
+ data.tar.gz: c72dfed155c15d80c00a3ba7efd99f8dc4572713
5
+ SHA512:
6
+ metadata.gz: e3eb9200176e339a6211171af0cca9fd8999c559ad5fd7806c893ab0f053cb5cea2d4451cb242114a5c5ec9ea82b7b33b0c0251cded612c52a38bc72f4b27033
7
+ data.tar.gz: a389473fff09e658d1195e94c1f9417d5eb223d5f2c21a3dca75e7d71877a88baca1f882e0834d3cbf206720c9058cf6f12ca73474bebe501860bdf8c1ef557c
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_spreadsheet_recorder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 saeki
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # GoogleSpreadsheetRecorder
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'google_spreadsheet_recorder'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install google_spreadsheet_recorder
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/google_spreadsheet_recorder/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "google_spreadsheet_recorder"
7
+ spec.version = "0.0.2"
8
+ spec.authors = ["saeki"]
9
+ spec.email = ["nobuo.seaki@gmail.com"]
10
+ spec.summary = %q{Simple google spreadsheet data uploader.}
11
+ spec.description = %q{}
12
+ spec.homepage = "https://github.com/nsaeki/google_spreadsheet_recorder"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "oauth2"
21
+ spec.add_dependency "nokogiri"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,110 @@
1
+ # coding: utf-8
2
+ require 'yaml'
3
+ require 'oauth2'
4
+ require 'nokogiri'
5
+
6
+ class GoogleSpreadsheetRecorder
7
+ SCOPE = 'https://spreadsheets.google.com/feeds'
8
+ REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
9
+
10
+ def initialize(client_id, client_secret, spreadsheet_key, token_file)
11
+ @client_id = client_id
12
+ @client_secret = client_secret
13
+ @token_file = token_file
14
+ @spreadsheet_key = spreadsheet_key
15
+ load_or_refresh_token
16
+ end
17
+
18
+ def worksheets
19
+ url = "https://spreadsheets.google.com/feeds/worksheets/#{@spreadsheet_key}/private/full"
20
+ response = @token.get(url)
21
+ doc = Nokogiri::XML(response.body)
22
+ doc.css('feed entry').map do |e|
23
+ sheet_title = e.css('title').first.content
24
+ sheet_url = e.css('id').first.content
25
+ sheet_id = sheet_url.split('/').last
26
+ [sheet_title, sheet_id]
27
+ end
28
+ end
29
+
30
+ def rows(sheet_id)
31
+ url = "https://spreadsheets.google.com/feeds/list/#{@spreadsheet_key}/#{sheet_id}/private/full"
32
+ response = @token.get(url)
33
+ doc = Nokogiri::XML(response.body)
34
+ doc.css('entry').map do |e|
35
+ convert_to_hash(e)
36
+ end
37
+ end
38
+
39
+ def send_row(row_data, sheet_id)
40
+ url = "https://spreadsheets.google.com/feeds/list/#{@spreadsheet_key}/#{sheet_id}/private/full"
41
+ body = <<-"EOS"
42
+ <entry xmlns="http://www.w3.org/2005/Atom"
43
+ xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">
44
+ #{convert_to_xml(row_data).join}
45
+ </entry>
46
+ EOS
47
+
48
+ @token.post(url, body: body, headers: {'Content-Type' => 'application/atom+xml'})
49
+ end
50
+
51
+ private
52
+
53
+ def load_or_refresh_token
54
+ google_oauth_params = {
55
+ site: 'https://accounts.google.com',
56
+ authorize_url: '/o/oauth2/auth',
57
+ token_url: '/o/oauth2/token',
58
+ }
59
+ client = OAuth2::Client.new(@client_id, @client_secret, google_oauth_params)
60
+
61
+ if File.exists?(@token_file)
62
+ @token = OAuth2::AccessToken.from_hash(client, YAML.load_file(@token_file))
63
+
64
+ if @token.expired?
65
+ puts "Token was expired at #{Time.at(@token.expires_at)}. Refresh one"
66
+ @token = @token.refresh!
67
+ puts "New token will be expired at #{Time.at(@token.expires_at)}."
68
+ save_token
69
+ end
70
+ else
71
+ @token = authorize_with_access_code(client)
72
+ save_token
73
+ end
74
+ end
75
+
76
+ def save_token
77
+ IO.write(@token_file, YAML.dump(@token.to_hash))
78
+ end
79
+
80
+ def authorize_with_access_code(client)
81
+ auth_url = client.auth_code.authorize_url(redirect_uri: REDIRECT_URI, scope: SCOPE)
82
+ print "access this url and paste access code: "
83
+ puts auth_url
84
+ print "input access_code: "
85
+ access_code = gets.strip
86
+ puts "access code is #{access_code}"
87
+
88
+ # returns token
89
+ client.auth_code.get_token(access_code, redirect_uri: REDIRECT_URI)
90
+ end
91
+
92
+ def convert_to_xml(row_hash)
93
+ row_hash.map do |k, v|
94
+ "<gsx:#{k}>#{v}</gsx:#{k}>"
95
+ end
96
+ end
97
+
98
+ def convert_to_hash(row_xml)
99
+ # Remove spreadsheet default columns.
100
+ columns_to_ignore = %w(id updated category title content link)
101
+
102
+ Hash[
103
+ row_xml.children.reject { |e|
104
+ columns_to_ignore.include?(e.name)
105
+ }.map { |e|
106
+ [e.name, e.content]
107
+ }
108
+ ]
109
+ end
110
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_spreadsheet_recorder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - saeki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: ''
70
+ email:
71
+ - nobuo.seaki@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - google_spreadsheet_recorder.gemspec
82
+ - lib/google_spreadsheet_recorder.rb
83
+ homepage: https://github.com/nsaeki/google_spreadsheet_recorder
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.2.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Simple google spreadsheet data uploader.
107
+ test_files: []