Class: UptimeReportsSpreadsheet::Auth
- Inherits:
-
Object
- Object
- UptimeReportsSpreadsheet::Auth
- Defined in:
- lib/uptime_reports_spreadsheet/auth.rb
Constant Summary collapse
- OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'
- APPLICATION_NAME =
'Google Sheets API Ruby Quickstart'
- CLIENT_SECRETS_PATH =
'client_secret.json'
- INVALID_SCOPE_ERROR =
"Sorry, this is an invalid scope"
Instance Attribute Summary collapse
-
#response ⇒ Object
Returns the value of attribute response.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#service ⇒ Object
Returns the value of attribute service.
-
#spreadsheet ⇒ Object
Returns the value of attribute spreadsheet.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
- #authorize ⇒ Object
- #choose_scope ⇒ Object
-
#initialize(scope = nil) ⇒ Auth
constructor
A new instance of Auth.
- #save_credentials ⇒ Object
- #scopes ⇒ Object
Constructor Details
#initialize(scope = nil) ⇒ Auth
Returns a new instance of Auth.
18 19 20 21 22 23 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 18 def initialize scope=nil @scope=scope || [5] @service = Google::Apis::SheetsV4::SheetsService.new @service..application_name = APPLICATION_NAME @user_id='default' end |
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
15 16 17 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 15 def response @response end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
16 17 18 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 16 def scope @scope end |
#service ⇒ Object
Returns the value of attribute service.
15 16 17 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 15 def service @service end |
#spreadsheet ⇒ Object
Returns the value of attribute spreadsheet.
15 16 17 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 15 def spreadsheet @spreadsheet end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
16 17 18 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 16 def user_id @user_id end |
Instance Method Details
#authorize ⇒ Object
25 26 27 28 29 30 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 25 def client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH) token_store = Google::Auth::Stores::FileTokenStore.new(file: credentials_path) = Google::Auth::UserAuthorizer.new(client_id, scope_url, token_store) .get_credentials(user_id) end |
#choose_scope ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 32 def choose_scope puts "Choose one scope:" scopes.each_pair do |k,v| puts "#{k}.- #{v[:name]}" puts "\t#{v[:desc]}" end s=gets(chomp: true) s=s.split @scope=s.map do |option| raise INVALID_SCOPE_ERROR if !scopes.keys.include?(option.to_i) option.to_i end end |
#save_credentials ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 50 def save_credentials choose_scope FileUtils.mkdir_p(File.dirname(credentials_path)) client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH) token_store = Google::Auth::Stores::FileTokenStore.new(file: credentials_path) = Google::Auth::UserAuthorizer.new( client_id, scope_url, token_store) url = .(base_url: OOB_URI) puts "Visit #{url} to get the code" code=gets(chomp: true) .get_and_store_credentials_from_code(user_id: user_id, code: code, base_url: OOB_URI) save_code code end |
#scopes ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/uptime_reports_spreadsheet/auth.rb', line 69 def scopes { 1 => { name: 'AUTH_DRIVE', desc: 'View and manage the files in your Google Drive', url: 'https://www.googleapis.com/auth/drive' }, 2 => { name: 'AUTH_DRIVE_FILE', desc: 'View and manage Google Drive files and folders that you have opened or created with this app', url: 'https://www.googleapis.com/auth/drive.file' }, 3 => { name: 'AUTH_DRIVE_READONLY', desc: 'View the files in your Google Drive', url: 'https://www.googleapis.com/auth/drive.readonly' }, 4 => { name: 'AUTH_SPREADSHEETS', desc: 'View and manage your spreadsheets in Google Drive', url: 'https://www.googleapis.com/auth/spreadsheets' }, 5 => { name: 'AUTH_SPREADSHEETS_READONLY', desc: 'View your Google Spreadsheets', url: 'https://www.googleapis.com/auth/spreadsheets.readonly' } } end |