Class: Datapimp::Clients::Google

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/datapimp/clients/google.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/datapimp/clients/google.rb', line 62

def method_missing meth, *args, &block
  if api.respond_to?(meth)
    return api.send(meth, *args, &block)
  end

  super
end

Class Method Details

.client(options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/datapimp/clients/google.rb', line 14

def self.client(options={})
  require 'google_drive' unless defined?(::GoogleDrive)

  @client ||= begin
                instance.with_options(options)
              end
end

.method_missing(meth, *args, &block) ⇒ Object



6
7
8
9
10
11
12
# 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

Instance Method Details

#apiObject



55
56
57
58
59
60
# File 'lib/datapimp/clients/google.rb', line 55

def api
  @api ||= begin
             refresh_access_token!
             GoogleDrive.(Datapimp.config.google_access_token)
           end
end

#auth_clientObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/datapimp/clients/google.rb', line 102

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) ⇒ Object



70
71
72
73
74
75
# File 'lib/datapimp/clients/google.rb', line 70

def authorize(token, secret)
  @api = nil if @api
  options[:token] = token
  options[:secret] = secret
  self
end

#get_application_keysObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/datapimp/clients/google.rb', line 90

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?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/datapimp/clients/google.rb', line 86

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?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/datapimp/clients/google.rb', line 131

def has_refresh_token?
  refresh_token.length > 0
end

#optionsObject



77
78
79
# File 'lib/datapimp/clients/google.rb', line 77

def options
  @options ||= {}
end

#refresh_access_token!Object



135
136
137
138
139
140
141
# File 'lib/datapimp/clients/google.rb', line 135

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_tokenObject



127
128
129
# File 'lib/datapimp/clients/google.rb', line 127

def refresh_token
  Datapimp.config.google_refresh_token.to_s
end

#refreshable?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/datapimp/clients/google.rb', line 22

def refreshable?
  has_application_keys? && has_refresh_token?
end

#sessionObject



51
52
53
# File 'lib/datapimp/clients/google.rb', line 51

def session
  api
end

#setup(options = {}) ⇒ Object

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datapimp/clients/google.rb', line 28

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(auth_client.authorization_uri)
    say("\n1. Open this page:\n%s\n\n" % auth_client.authorization_uri)
    auth_client.code = ask("2. Enter the authorization code shown in the page: ", String)
    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
end

#with_options(opts = {}) ⇒ Object



81
82
83
84
# File 'lib/datapimp/clients/google.rb', line 81

def with_options(opts={})
  options.merge!(opts)
  self
end