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"
DEFAULT_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.“

  • :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)

  • :scope => Optional. The scope of the access request, expressed either as an Array or as a space-delimited String.

Raises:

  • (ArgumentError)


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

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 => params.fetch(:scope, DEFAULT_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.



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

def client
  @client
end

Class Method Details

.factory(params) ⇒ Object

A utility method used to centralize the creation of connections



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

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



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

def self.(params)
  client = Signet::OAuth2::Client.new(
    :scope => params.fetch(:scope, DEFAULT_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.



95
96
97
# File 'lib/google/connection.rb', line 95

def access_token
  @client.access_token
end

#auth_codeObject

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



88
89
90
# File 'lib/google/connection.rb', line 88

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.



81
82
83
# File 'lib/google/connection.rb', line 81

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.



110
111
112
113
114
# File 'lib/google/connection.rb', line 110

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.



119
120
121
122
123
# File 'lib/google/connection.rb', line 119

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.



102
103
104
# File 'lib/google/connection.rb', line 102

def refresh_token
  @client.refresh_token
end

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

Send a request to google.



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

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