Class: Goauth2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/goauth2/client.rb,
lib/goauth2/calendar.rb,
lib/goauth2/contacts.rb

Constant Summary collapse

SCOPES =
{
  :calendar => 'https://www.google.com/calendar/feeds/',
  :contacts => 'https://www.google.com/m8/feeds/',
  :gmail => 'https://mail.google.com/mail/feed/atom/'
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/goauth2/client.rb', line 14

def initialize(options = {})
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @redirect_uri = options[:redirect_uri]
  @scope = options[:scope] || SCOPES.values.join(' ')
  
  @token = options[:token]
  @refresh_token = options[:refresh_token]
  @expires_at = options[:expires_at]
  @expires_in = options[:expires_in]
end

Instance Method Details

#access_tokenObject



57
58
59
60
# File 'lib/goauth2/client.rb', line 57

def access_token
  @access_token ||= OAuth2::AccessToken.new(client, @token, :refresh_token => @refresh_token, :expires_at => @expires_at, :expires_in => @expires_in)
  refresh!
end

#all_calendars(params = {}, headers = {}) ⇒ Object



3
4
5
# File 'lib/goauth2/calendar.rb', line 3

def all_calendars(params = {}, headers = {})
  _get_jsonc('https://www.google.com/calendar/feeds/default/allcalendars/full', params, headers.merge({'GData-Version' => '2'}))
end

#authorize(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/goauth2/client.rb', line 33

def authorize(options = {})
  @access_token ||= client.auth_code.get_token(
    options[:code],
    :redirect_uri => options[:redirect_uri] || @redirect_uri
  )
  @token = @access_token.token
  @refresh_token = @access_token.refresh_token
  @expires_at = @access_token.expires_at
  @expires_in = @access_token.expires_in
  @access_token
end

#authorize_url(options = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/goauth2/client.rb', line 26

def authorize_url(options = {})
  client.auth_code.authorize_url(
    :redirect_uri => options[:redirect_uri] || @redirect_uri,
    :scope => options[:scope] || @scope
  )
end

#clientObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/goauth2/client.rb', line 45

def client
  @client ||= OAuth2::Client.new(
    @client_id,
    @client_secret,
    { 
      :site => "https://accounts.google.com",
      :authorize_url => '/o/oauth2/auth',
      :token_url => '/o/oauth2/token'
    }
  )
end

#contactsObject



4
5
6
# File 'lib/goauth2/contacts.rb', line 4

def contacts
  _get_json('https://www.google.com/m8/feeds/contacts/default/full')
end

#events(event_feed, params = {}, headers = {}) ⇒ Object



11
12
13
# File 'lib/goauth2/calendar.rb', line 11

def events(event_feed, params = {}, headers = {})
  _get_jsonc(event_feed, params, headers.merge({'GData-Version' => '2'}))
end

#own_calendars(params = {}, headers = {}) ⇒ Object



7
8
9
# File 'lib/goauth2/calendar.rb', line 7

def own_calendars(params = {}, headers = {})
  _get_jsonc('https://www.google.com/calendar/feeds/default/owncalendars/full', params, headers.merge({'GData-Version' => '2'}))
end