Created
October 26, 2015 22:44
-
-
Save mariusbutuc/4a6f4302caa7b0fb5578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'google/api_client' | |
require 'google/api_client/auth/file_storage' | |
require 'google/api_client/auth/installed_app' | |
module Google | |
class Drive | |
# https://github.com/google/google-api-ruby-client-samples/blob/master/drive/drive.rb | |
API_VERSION = 'v2'.freeze | |
CREDENTIAL_STORE_FILE = Rails.root.join('lib/google/credential_store.json').freeze | |
def self.get_file(id:) | |
result = client.execute( | |
api_method: api_wrapper.files.get, | |
parameters: { 'fileId' => id } | |
) | |
file = result.data | |
download_url = file.exportLinks['text/csv'] | |
RestClient.get(download_url) | |
end | |
private | |
def self.client | |
@client ||= Google::APIClient.new( | |
application_name: 'Wholesale Opt-in', | |
application_version: '1.0.0' | |
).tap do |client_to_auth| | |
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE) | |
if file_storage.authorization.nil? | |
flow = Google::APIClient::InstalledAppFlow.new( | |
client_id: Figaro.env.google_client_id, | |
client_secret: Figaro.env.google_client_secret, | |
scope: ['https://www.googleapis.com/auth/drive'] | |
) | |
client_to_auth.authorization = flow.authorize(file_storage) | |
else | |
client_to_auth.authorization = file_storage.authorization | |
end | |
end | |
end | |
def self.api_wrapper | |
@api_wrapper ||= client.discovered_api('drive', API_VERSION) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment