class Datapimp::Clients::Google

Public Class Methods

client(options={}) click to toggle source
# File lib/datapimp/clients/google.rb, line 14
def self.client(options={})
  unless defined?(::GoogleDrive)
    require 'google_drive'
    require 'google/api_client'
    require 'google_drive/session'
  end

  @client ||= begin
                instance.with_options(options)
              end
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/datapimp/clients/google.rb, line 6
def self.method_missing(meth, *args, &block)
  if client.respond_to?(meth)
    return client.send(meth, *args, &block)
  end

  super
end

Public Instance Methods

api() click to toggle source
# File lib/datapimp/clients/google.rb, line 67
def api
  @api ||= begin
             refresh_access_token!
             GoogleDrive.login_with_oauth(Datapimp.config.google_access_token)
           end
end
auth_client() click to toggle source
# File lib/datapimp/clients/google.rb, line 114
def auth_client
  return @auth_client if @auth_client

  client = ::Google::APIClient.new(
      :application_name => "google_drive Ruby library",
      :application_version => "0.3.11"
  )

  client_id = "452925651630-egr1f18o96acjjvphpbbd1qlsevkho1d.apps.googleusercontent.com"
  client_secret = "1U3-Krii5x1oLPrwD5zgn-ry"

  @auth_client = auth = client.authorization
  auth.client_id = client_id #Datapimp.config.google_client_id
  auth.client_secret = client_secret #Datapimp.config.google_client_secret
  auth.scope =
      "https://www.googleapis.com/auth/drive " +
      "https://spreadsheets.google.com/feeds/ " +
      "https://docs.google.com/feeds/ " +
      "https://docs.googleusercontent.com/"

  auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"

  auth
end
authorize(token, secret) click to toggle source
# File lib/datapimp/clients/google.rb, line 82
def authorize(token, secret)
  @api = nil if @api
  options[:token] = token
  options[:secret] = secret
  self
end
browser_authorization_url() click to toggle source
# File lib/datapimp/clients/google.rb, line 30
def browser_authorization_url
  auth_client.authorization_uri
end
consume_auth_client_code(code) click to toggle source
# File lib/datapimp/clients/google.rb, line 34
def consume_auth_client_code code
  auth_client.code = code
  auth_client.fetch_access_token!
  Datapimp.config.set "google_refresh_token", auth_client.refresh_token
  Datapimp.config.set "google_access_token", auth_client.access_token
end
get_application_keys() click to toggle source
# File lib/datapimp/clients/google.rb, line 102
def get_application_keys
  unless Datapimp.config.google_client_id.to_s.length > 0
    google_client_id = ask("What is the Google Client ID?", String)
    Datapimp.config.set "google_client_id", google_client_id
  end

  unless Datapimp.config.google_client_secret.to_s.length > 0
    google_client_secret = ask("What is the Google Client Secret?", String)
    Datapimp.config.set "google_client_secret", google_client_secret
  end
end
has_application_keys?() click to toggle source
# File lib/datapimp/clients/google.rb, line 98
def has_application_keys?
  (Datapimp.config.google_client_id.to_s.length > 0 && Datapimp.config.google_client_secret.to_s.length > 0)
end
has_refresh_token?() click to toggle source
# File lib/datapimp/clients/google.rb, line 143
def has_refresh_token?
  refresh_token.length > 0
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/datapimp/clients/google.rb, line 74
def method_missing meth, *args, &block
  if api.respond_to?(meth)
    return api.send(meth, *args, &block)
  end

  super
end
options() click to toggle source
# File lib/datapimp/clients/google.rb, line 89
def options
  @options ||= {}
end
refresh_access_token!() click to toggle source
# File lib/datapimp/clients/google.rb, line 147
def refresh_access_token!
  if has_refresh_token?
    auth_client.refresh_token = refresh_token
    auth_client.fetch_access_token!
    Datapimp.config.set "google_access_token", auth_client.access_token
  end
end
refresh_token() click to toggle source
# File lib/datapimp/clients/google.rb, line 139
def refresh_token
  Datapimp.config.google_refresh_token.to_s
end
refreshable?() click to toggle source
# File lib/datapimp/clients/google.rb, line 26
def refreshable?
  has_application_keys? && has_refresh_token?
end
session() click to toggle source
# File lib/datapimp/clients/google.rb, line 63
def session
  api
end
setup(options={}) click to toggle source

Runs through an interactive session where we get the necessary tokens needed to integrate with google drive.

# File lib/datapimp/clients/google.rb, line 43
def setup(options={})
  get_application_keys unless has_application_keys?

  if options[:client_id]
    Datapimp.config.set "google_client_id", options[:client_id]
  end

  if options[:client_secret]
    Datapimp.config.set "google_client_secret", options[:client_secret]
  end

  if has_refresh_token?
    refresh_access_token!
  elsif respond_to?(:ask)
    Launchy.open(browser_authorization_url)
    say("\n1. Open this page:\n%s\n\n" % auth_client.authorization_uri)
    consume_auth_client_code ask("2. Enter the authorization code shown in the page: ", String)
  end
end
with_options(opts={}) click to toggle source
# File lib/datapimp/clients/google.rb, line 93
def with_options(opts={})
  options.merge!(opts)
  self
end