Class: DriveService
- Inherits:
-
Object
- Object
- DriveService
- Defined in:
- lib/gdsh/drive.rb
Overview
DriveService module implements a service that interfaces with Google Drive using Google Drive API.
Direct Known Subclasses
Instance Method Summary collapse
- #authorize ⇒ Object
- #authorize_from_authorization_code ⇒ Object
- #authorize_from_refresh_token ⇒ Object
- #credentials_from_file ⇒ Object
-
#credentials_from_stdin ⇒ Object
Get credentials from shell if no credentials file was specified.
- #init_client ⇒ Object
-
#initialize(filename = nil) ⇒ DriveService
constructor
Creates a new Google Drive Shell object.
- #puts_refresh_error ⇒ Object
- #write_session_info_to_file ⇒ Object
Constructor Details
#initialize(filename = nil) ⇒ DriveService
Creates a new Google Drive Shell object.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/gdsh/drive.rb', line 18 def initialize(filename = nil) # default to per-file permissions @oauth_scope = 'https://www.googleapis.com/auth/drive.file' @redirect_uri = 'urn:ietf:wg:oauth:2.0:oob' @filename = filename if filename && File.exist?(filename) credentials_from_file else credentials_from_stdin end end |
Instance Method Details
#authorize ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gdsh/drive.rb', line 55 def init_client begin rescue puts_refresh_error ensure @client..fetch_access_token! end end |
#authorize_from_authorization_code ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/gdsh/drive.rb', line 78 def uri = @client.. Launchy.open(uri) # Exchange authorization code for access token print 'Enter authorization code: '.colorize(:light_blue) @client..code = $stdin.gets.chomp end |
#authorize_from_refresh_token ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/gdsh/drive.rb', line 68 def fail StandardError unless File.exist?('.session.yaml') f = File.open('.session.yaml', 'r') session = YAML.load(f.read) @client..grant_type = 'refresh_token' @client..refresh_token = session..refresh_token f.close end |
#credentials_from_file ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/gdsh/drive.rb', line 31 def credentials_from_file File.open(@filename, 'r') do |f| buffer = f.read credentials = JSON.parse(buffer) @client_id = credentials['installed']['client_id'] @client_secret = credentials['installed']['client_secret'] end end |
#credentials_from_stdin ⇒ Object
Get credentials from shell if no credentials file was specified.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/gdsh/drive.rb', line 90 def credentials_from_stdin # get preset if exists @client_id ||= '' @client_secret ||= '' # Ask from user otherwise if @client_id == '' print 'Please enter your client id: ' @client_id = $stdin.gets.chomp end if @client_secret == '' print 'Please enter your client secret: ' @client_secret = $stdin.gets.chomp end end |
#init_client ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gdsh/drive.rb', line 40 def init_client # Create a new API client & load the Google Drive API @client = Google::APIClient.new # Request authorization @client..client_id = @client_id @client..client_secret = @client_secret @client..scope = @oauth_scope @client..redirect_uri = @redirect_uri end |
#puts_refresh_error ⇒ Object
51 52 53 |
# File 'lib/gdsh/drive.rb', line 51 def puts_refresh_error puts 'Could not refresh token from saved session.'.colorize(:red) end |
#write_session_info_to_file ⇒ Object
107 108 109 110 111 112 |
# File 'lib/gdsh/drive.rb', line 107 def write_session_info_to_file return if @client.nil? f = File.new('.session.yaml', 'w') f.write(@client.to_yaml) f.close end |