gspush 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/gspush +1 -1
- data/bin/gspush_generate +7 -0
- data/gspush.gemspec +4 -1
- data/lib/gspush.rb +19 -5
- data/lib/gspush/oauth2.rb +109 -0
- data/lib/gspush/version.rb +1 -1
- data/spec/gspush_spec.rb +1 -1
- data/spec/spec_helper.rb +10 -0
- metadata +23 -2
data/bin/gspush
CHANGED
data/bin/gspush_generate
ADDED
data/gspush.gemspec
CHANGED
data/lib/gspush.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
require 'optparse'
|
2
2
|
require 'google_drive'
|
3
|
+
require 'oauth2'
|
4
|
+
require 'yaml'
|
5
|
+
require 'highline'
|
3
6
|
|
4
7
|
require_relative './gspush/version'
|
8
|
+
require_relative './gspush/oauth2'
|
5
9
|
|
6
10
|
class Gspush
|
7
11
|
class WorksheetNotFound < StandardError; end
|
@@ -16,6 +20,9 @@ class Gspush
|
|
16
20
|
@username = options[:username]
|
17
21
|
@password = options[:password]
|
18
22
|
|
23
|
+
@oauth2 = options[:oauth2]
|
24
|
+
@oauth_token_file = options[:oauth_token_file]
|
25
|
+
|
19
26
|
@prepend_timestamp = options[:prepend_timestamp]
|
20
27
|
@sheet_title = options[:sheet_title]
|
21
28
|
|
@@ -40,7 +47,7 @@ class Gspush
|
|
40
47
|
end
|
41
48
|
|
42
49
|
def save
|
43
|
-
spreadsheet = open(@url
|
50
|
+
spreadsheet = open(@url)
|
44
51
|
|
45
52
|
sheet = select_worksheet(spreadsheet)
|
46
53
|
raise WorksheetNotFound if sheet.nil?
|
@@ -53,9 +60,14 @@ class Gspush
|
|
53
60
|
|
54
61
|
private
|
55
62
|
|
56
|
-
def open(url
|
57
|
-
session
|
58
|
-
|
63
|
+
def open(url)
|
64
|
+
session = if @oauth2
|
65
|
+
oauth = Gspush::Oauth2.new(@oauth_token_file)
|
66
|
+
GoogleDrive.login_with_oauth(oauth.token)
|
67
|
+
else
|
68
|
+
GoogleDrive.login(username, password)
|
69
|
+
end
|
70
|
+
session.spreadsheet_by_url(url)
|
59
71
|
end
|
60
72
|
|
61
73
|
def select_worksheet(spreadsheet)
|
@@ -89,6 +101,8 @@ class Gspush
|
|
89
101
|
opt.on('-d delim', 'input delimiter') {|v| options[:delimiter] = v }
|
90
102
|
opt.on('-u username', 'Google Drive username(email)') {|v| options[:username] = v }
|
91
103
|
opt.on('-p password', 'user password') {|v| options[:password] = v } # XXX how should i get this?
|
104
|
+
opt.on('-o', 'using oauth2 authorize') {|v| options[:oauth2] = v }
|
105
|
+
opt.on('-f oauth_token_file', 'oauth2 token file path') {|v| options[:oauth_token_file] = v }
|
92
106
|
opt.on('-s sheet_title', 'worksheet title (default: first worksheet)') {|v| options[:sheet_title] = v }
|
93
107
|
opt.on('-t', 'prepend timestamp cell') {|v| options[:prepend_timestamp] = v }
|
94
108
|
|
@@ -97,7 +111,7 @@ class Gspush
|
|
97
111
|
|
98
112
|
argv = opt.parse(argv_original)
|
99
113
|
|
100
|
-
if argv.empty?
|
114
|
+
if argv.empty? && !options[:generate_token_file]
|
101
115
|
puts opt.banner
|
102
116
|
exit 1
|
103
117
|
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'highline'
|
2
|
+
require 'oauth2'
|
3
|
+
|
4
|
+
class Gspush
|
5
|
+
class Oauth2
|
6
|
+
attr_accessor :access_token, :refresh_token, :expires_at
|
7
|
+
attr_accessor :client_id, :client_secret, :redirect_uri
|
8
|
+
|
9
|
+
def initialize(file_path = nil)
|
10
|
+
@file_path = file_path || "#{ENV["HOME"]}/.gspushrc"
|
11
|
+
@redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
|
12
|
+
end
|
13
|
+
|
14
|
+
def token
|
15
|
+
self.load or raise "First you need gspush_generate to create oauth2 configuration"
|
16
|
+
token = OAuth2::AccessToken.from_hash(client, to_hash)
|
17
|
+
|
18
|
+
if expired?
|
19
|
+
token.refresh!
|
20
|
+
save
|
21
|
+
end
|
22
|
+
|
23
|
+
token
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.generate
|
27
|
+
puts "Input your application information"
|
28
|
+
puts "(see https://code.google.com/apis/console )"
|
29
|
+
|
30
|
+
oauth2 = new
|
31
|
+
|
32
|
+
hi = HighLine.new
|
33
|
+
oauth2.client_id = hi.ask("Client ID? > ")
|
34
|
+
oauth2.client_secret = hi.ask("Client Secret? > ")
|
35
|
+
|
36
|
+
oauth2.get_access_token
|
37
|
+
oauth2.save
|
38
|
+
end
|
39
|
+
|
40
|
+
def save
|
41
|
+
File.open(@file_path, 'w') do |f|
|
42
|
+
f.print to_hash.to_yaml
|
43
|
+
end
|
44
|
+
puts "ok saved in #{@file_path}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def load
|
48
|
+
if FileTest.exist?(@file_path)
|
49
|
+
o = YAML.load(File.read(@file_path))
|
50
|
+
@access_token = o[:access_token]
|
51
|
+
@refresh_token = o[:refresh_token]
|
52
|
+
@expires_at = o[:expires_at]
|
53
|
+
@client_id = o[:client_id]
|
54
|
+
@client_secret = o[:client_secret]
|
55
|
+
@redirect_uri = o[:redirect_uri]
|
56
|
+
true
|
57
|
+
else
|
58
|
+
false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def client
|
63
|
+
OAuth2::Client.new(
|
64
|
+
@client_id, @client_secret,
|
65
|
+
:site => "https://accounts.google.com",
|
66
|
+
:token_url => "/o/oauth2/token",
|
67
|
+
:authorize_url => "/o/oauth2/auth")
|
68
|
+
end
|
69
|
+
|
70
|
+
def get_access_token
|
71
|
+
auth_url = client.auth_code.authorize_url(
|
72
|
+
:redirect_uri => @redirect_uri,
|
73
|
+
:scope =>
|
74
|
+
["https://docs.google.com/feeds/",
|
75
|
+
"https://docs.googleusercontent.com/",
|
76
|
+
"https://spreadsheets.google.com/feeds/"].join(" "))
|
77
|
+
hi = HighLine.new
|
78
|
+
|
79
|
+
puts "Access in your browser: #{auth_url}"
|
80
|
+
authorization_code = hi.ask("And please input authorization code: ")
|
81
|
+
|
82
|
+
auth_token = client.auth_code.get_token(
|
83
|
+
authorization_code, :redirect_uri => @redirect_uri)
|
84
|
+
|
85
|
+
@access_token = auth_token.token
|
86
|
+
@refresh_token = auth_token.refresh_token
|
87
|
+
@expires_at = auth_token.expires_at
|
88
|
+
|
89
|
+
@access_token
|
90
|
+
end
|
91
|
+
|
92
|
+
def expired?
|
93
|
+
@expires_at && Time.now.to_i > @expires_at
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_hash
|
97
|
+
{
|
98
|
+
:client_id => client_id,
|
99
|
+
:client_secret => client_secret,
|
100
|
+
:redirect_uri => redirect_uri,
|
101
|
+
:access_token => access_token,
|
102
|
+
:refresh_token => refresh_token,
|
103
|
+
:expires_at => expires_at,
|
104
|
+
}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
data/lib/gspush/version.rb
CHANGED
data/spec/gspush_spec.rb
CHANGED
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gspush
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: google_drive
|
@@ -43,11 +43,28 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: fakefs
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
description: Push command to Google Spreadsheet
|
47
63
|
email:
|
48
64
|
- holysugar@gmail.com
|
49
65
|
executables:
|
50
66
|
- gspush
|
67
|
+
- gspush_generate
|
51
68
|
extensions: []
|
52
69
|
extra_rdoc_files: []
|
53
70
|
files:
|
@@ -57,10 +74,13 @@ files:
|
|
57
74
|
- README.md
|
58
75
|
- Rakefile
|
59
76
|
- bin/gspush
|
77
|
+
- bin/gspush_generate
|
60
78
|
- gspush.gemspec
|
61
79
|
- lib/gspush.rb
|
80
|
+
- lib/gspush/oauth2.rb
|
62
81
|
- lib/gspush/version.rb
|
63
82
|
- spec/gspush_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
64
84
|
homepage: https://github.com/holysugar/gspush
|
65
85
|
licenses: []
|
66
86
|
post_install_message:
|
@@ -87,3 +107,4 @@ specification_version: 3
|
|
87
107
|
summary: Phsh command to Google Spreadsheet
|
88
108
|
test_files:
|
89
109
|
- spec/gspush_spec.rb
|
110
|
+
- spec/spec_helper.rb
|