Class: GoogleDocSeed

Inherits:
Object
  • Object
show all
Defined in:
lib/google_doc_seed.rb,
lib/google_doc_seed/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ GoogleDocSeed

Returns a new instance of GoogleDocSeed.



23
24
25
# File 'lib/google_doc_seed.rb', line 23

def initialize(token)
  @session = GoogleDrive.(token)
end

Class Method Details

.get_access_token(client_id, client_secret) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/google_doc_seed.rb', line 6

def self.get_access_token(client_id, client_secret)
  require 'google/api_client'
  require 'launchy'

  client = Google::APIClient.new(application_name: 'RubyGoogleDocSeed', 
                                 application_version: GoogleDocSeed::VERSION)
  
  client.authorization.client_id = client_id
  client.authorization.client_secret = client_secret
  client.authorization.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
  client.authorization.scope = 'https://spreadsheets.google.com/feeds'
  Launchy.open(client.authorization.authorization_uri)
  STDERR.puts 'Enter authorization code: '
  client.authorization.code = STDIN.gets.chomp
  client.authorization.fetch_access_token!
end

Instance Method Details

#to_csv_string(doc_key, ws_name = nil) ⇒ Object



27
28
29
30
31
# File 'lib/google_doc_seed.rb', line 27

def to_csv_string(doc_key, ws_name = nil)
  doc = @session.spreadsheet_by_key(doc_key)
  ws = ws_name ? doc.worksheet_by_title(ws_name) : doc.worksheets[0]
  CSV.generate { |csv| ws.rows.each { |row| csv << row } }
end