Sha256: 44ae2a489187e573d15fec8fbe8b214506288e647dcf6fb845918e88ee6956be

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require 'fileutils'
require 'rails_drive/google'

module RailsDrive
  # Alias the module
  Oauth2       = Google::Apis::Oauth2V2
  Auth         = Google::Auth

  class Handler

    include RailsDrive::Config
    attr_reader :credentials, :auth_client

    def initialize(user_id = 'default')
      # Token store
      FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
      client_id    = Auth::ClientId.from_file CLIENT_SECRETS_PATH
      token_store  = Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
      @user_id     = user_id
      # credentials
      @authorizer  = Auth::UserAuthorizer.new(client_id, SCOPE, token_store, CALLBACK_URI)
      @credentials = @authorizer.get_credentials(@user_id)
      # serive
      @service     = Google::Apis::DriveV3::DriveService.new
      @service.client_options.application_name = 'seo-report'
      @service.authorization = @credentials
    end

    def get_authorization_url
      @authorizer.get_authorization_url base_url: BASE_URL
    end

    def get_and_store_credentials_from_code(code)
      @credentials = @authorizer.get_and_store_credentials_from_code(
        user_id: @user_id, code: code, base_url: BASE_URL)
      @service.authorization = @credentials
    end

    def list_files(options={})
      # List files under a folder
      conditions = []
      conditions << "'#{options[:folder]}' in parents" if options.has_key? :folder
      conditions << "name = '#{options[:file_name]}'" if options.has_key? :file_name
      options = {
        q: conditions.join(" and "),
        spaces: 'drive',
        fields:'nextPageToken, files(id, name, webViewLink)'
      }
      @service.list_files options
    end

    def get_folder(name)
      options = {
        q: "mimeType='application/vnd.google-apps.folder' and name='#{name}'",
        spaces: 'drive',
        fields:'nextPageToken, files(id, name, webViewLink)'
      }
      @service.list_files options
    end

    def has_credentials?
      @credentials
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_drive-0.1.0 lib/rails_drive/handler.rb