Class: Google::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/google/connection.rb

Overview

This is a utility class that communicates with the google calendar api.

Constant Summary collapse

BASE_URI =
"https://www.googleapis.com/calendar/v3"
TOKEN_URI =
"https://accounts.google.com/o/oauth2/token"
AUTH_URI =
"https://accounts.google.com/o/oauth2/auth"
SCOPE =
"https://www.googleapis.com/auth/calendar"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, client = nil) ⇒ Connection

Prepare a connection to google for fetching a calendar events

the +params+ paramater accepts
  • :client_id => the client ID that you received from Google after registering your application with them (console.developers.google.com/)

  • :client_secret => the client secret you received from Google after registering your application with them.

  • :redirect_uri => the url where your users will be redirected to after they have successfully permitted access to their calendars. Use 'urn:ietf:wg:oauth:2.0:oob' if you are using an 'application'“

  • :refresh_token => if a user has already given you access to their calendars, you can specify their refresh token here and you will be 'logged on' automatically (i.e. they don't need to authorize access again)

Raises:

  • (ArgumentError)

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/google/connection.rb', line 49

def initialize(params, client=nil)

  raise ArgumentError unless client || Connection.credentials_provided?(params)

  @client = client || Signet::OAuth2::Client.new(
    :client_id => params[:client_id],
    :client_secret => params[:client_secret],
    :redirect_uri => params[:redirect_url],
    :refresh_token => params[:refresh_token],
    :state => params[:state],
    :authorization_uri => AUTH_URI,
    :token_credential_uri => TOKEN_URI,
    :scope => SCOPE
  )

  # try to get an access token if possible.
  if params[:refresh_token]
    @client.refresh_token = params[:refresh_token]
    @client.grant_type = 'refresh_token'
  end

  if params[:refresh_token] || params[:signing_key]
    Connection.get_new_access_token(@client)
  end

end

Instance Attribute Details

#clientObject

Returns the value of attribute client


13
14
15
# File 'lib/google/connection.rb', line 13

def client
  @client
end

Class Method Details

.factory(params) ⇒ Object

A utility method used to centralize the creation of connections


30
31
32
33
34
35
36
37
38
# File 'lib/google/connection.rb', line 30

def self.factory(params) # :nodoc
  Connection.new(
    :client_id => params[:client_id],
    :client_secret => params[:client_secret],
    :refresh_token => params[:refresh_token],
    :redirect_url => params[:redirect_url],
    :state => params[:state]
  )
end

.new_with_service_account(params) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/google/connection.rb', line 15

def self.(params)
  client = Signet::OAuth2::Client.new(
    :scope => SCOPE,
    :issuer => params[:client_id],
    :audience => TOKEN_URI,
    :token_credential_uri => TOKEN_URI,
    :signing_key => params[:signing_key],
    :person => params[:person]
  )
  Connection.new(params, client)
end

Instance Method Details

#access_tokenObject

The current access token. Used during a session, typically expires in a hour.


93
94
95
# File 'lib/google/connection.rb', line 93

def access_token
  @client.access_token
end

#auth_codeObject

The single use auth code that google uses during the auth process.


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

def auth_code
  @client.code
end

#authorize_urlObject

The URL you need to send a user in order to let them grant you access to their calendars.


79
80
81
# File 'lib/google/connection.rb', line 79

def authorize_url
  @client.authorization_uri
end

#login_with_auth_code(auth_code) ⇒ Object

Convenience method used to streamline the process of logging in with a auth code. Returns the refresh token.


108
109
110
111
112
# File 'lib/google/connection.rb', line 108

def (auth_code)
  @client.code = auth_code
  Connection.get_new_access_token(@client)
  @client.refresh_token
end

#login_with_refresh_token(refresh_token) ⇒ Object

Convenience method used to streamline the process of logging in with a refresh token.


117
118
119
120
121
# File 'lib/google/connection.rb', line 117

def (refresh_token)
  @client.refresh_token = refresh_token
  @client.grant_type = 'refresh_token'
  Connection.get_new_access_token(@client)
end

#refresh_tokenObject

The refresh token is used to obtain a new access token. It remains valid until a user revokes access.


100
101
102
# File 'lib/google/connection.rb', line 100

def refresh_token
  @client.refresh_token
end

#send(path, method, content = '') ⇒ Object

Send a request to google.


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/google/connection.rb', line 126

def send(path, method, content = '')

  uri = BASE_URI + path
  response = @client.fetch_protected_resource(
    :uri => uri,
    :method => method,
    :body  => content,
    :headers => {'Content-type' => 'application/json'}
  )

  check_for_errors(response)

  return response
end