Class: GoogleClient::User
- Inherits:
-
Object
- Object
- GoogleClient::User
- Includes:
- Format
- Defined in:
- lib/google_client/user.rb
Instance Attribute Summary collapse
-
#json_mode ⇒ Object
Returns the value of attribute json_mode.
-
#oauth_credentials ⇒ Object
Returns the value of attribute oauth_credentials.
Instance Method Summary collapse
-
#calendar(calendar_id = :all) ⇒ Object
Parameters * calendar_id Calendar unique identifier.
-
#contacts ⇒ Object
Fetch user contacts.
-
#create_calendar(params = {}) ⇒ Object
Create a calendar.
-
#http ⇒ Object
(also: #connection)
Method that creates and returns the HttpConnection instance that shall be used.
-
#initialize(oauth_credentials, user_credentials = nil) ⇒ User
constructor
A new instance of User.
-
#profile ⇒ Object
Get user profile.
-
#refresh(refresh_token, client_id, client_secret) ⇒ Object
Refresh an invalid access_token ==== Parameters * refresh_token * client_id * client_secret.
-
#refresh!(refresh_token, client_id, client_secret) ⇒ Object
Same as refresh method, but also updates the oauth_credentials variable with the new granted token.
Methods included from Format
Constructor Details
#initialize(oauth_credentials, user_credentials = nil) ⇒ User
Returns a new instance of User.
9 10 11 12 |
# File 'lib/google_client/user.rb', line 9 def initialize(oauth_credentials, user_credentials = nil) @oauth_credentials = oauth_credentials @json_mode = true end |
Instance Attribute Details
#json_mode ⇒ Object
Returns the value of attribute json_mode.
5 6 7 |
# File 'lib/google_client/user.rb', line 5 def json_mode @json_mode end |
#oauth_credentials ⇒ Object
Returns the value of attribute oauth_credentials.
4 5 6 |
# File 'lib/google_client/user.rb', line 4 def oauth_credentials @oauth_credentials end |
Instance Method Details
#calendar(calendar_id = :all) ⇒ Object
Parameters
-
calendar_id Calendar unique identifier
Return
-
Calendar instance
-
*Array of Calendar* instances
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/google_client/user.rb', line 68 def calendar(calendar_id = :all) calendars = if calendar_id.nil? || calendar_id.eql?(:all) calendars = decode_response http.get "/calendar/feeds/default/allcalendars/full" calendars = calendars["feed"]["entry"] calendars.map{ |calendar| Calendar.build_calendar(calendar, self)} elsif calendar_id.eql?(:own) calendars = decode_response http.get "/calendar/feeds/default/owncalendars/full" calendars = calendars["feed"]["entry"] calendars.map{ |calendar| Calendar.build_calendar(calendar, self)} elsif calendar_id.is_a?(String) Calendar.new({:id => calendar_id, :user => self}).fetch elsif calendar_id.is_a?(Hash) # TODO add support to {:title => calendar_title} raise ArgumentError.new "Invalid argument type #{calendar_id.class}" else raise ArgumentError.new "Invalid argument type #{calendar_id.class}" end end |
#contacts ⇒ Object
Fetch user contacts
108 109 110 111 112 |
# File 'lib/google_client/user.rb', line 108 def contacts contacts = decode_response http.get "/m8/feeds/contacts/default/full", {"max-results" => "1000"} contacts = contacts["feed"]["entry"] contacts.map{|contact| Contact.build_contact(contact, self)} end |
#create_calendar(params = {}) ⇒ Object
Create a calendar
Parameters
-
params Hash
-
:title
-
:details
-
:timezone
-
:location
-
Return
Calendar instance
98 99 100 101 102 103 104 105 |
# File 'lib/google_client/user.rb', line 98 def create_calendar(params = {}) calendar = if block_given? Calendar.create(params.merge({:user => self}), &Proc.new) else Calendar.create(params.merge({:user => self})) end calendar.save end |
#http ⇒ Object Also known as: connection
Method that creates and returns the HttpConnection instance that shall be used
Return
-
HttpConnection instance
119 120 121 122 123 124 125 |
# File 'lib/google_client/user.rb', line 119 def http @http ||= HttpConnection.new("https://www.google.com", {:alt => "json"}, {:Authorization => "OAuth #{oauth_credentials}", "Content-Type" => "json", :Accept => "application/json"}) end |
#profile ⇒ Object
Get user profile
Return
-
Profile instance
18 19 20 21 22 23 24 |
# File 'lib/google_client/user.rb', line 18 def profile data = decode_response http.get "/m8/feeds/contacts/default/full", {"max-results" => 1} email = data["feed"]["id"]["$t"] Profile.new({:email => email, :external_id => email.split("@").first}) rescue Profile.new end |
#refresh(refresh_token, client_id, client_secret) ⇒ Object
Refresh an invalid access_token
Parameters
-
refresh_token
-
client_id
-
client_secret
Return
-
Hash with the following keys:
-
access_token
-
token_type
-
expires_in
-
-
BadRequestError if invalid refresh token or invalid client
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/google_client/user.rb', line 38 def refresh refresh_token, client_id, client_secret _params = { :client_id => client_id, :client_secret => client_secret, :refresh_token => refresh_token, :grant_type => "refresh_token" } data = HttpConnection.new("https://accounts.google.com", {:alt => "json"}, {:Authorization => "OAuth #{oauth_credentials}", "Content-Type" => "application/x-www-form-urlencoded", :Accept => "application/json"}).post "/o/oauth2/token", _params decode_response data.body end |
#refresh!(refresh_token, client_id, client_secret) ⇒ Object
Same as refresh method, but also updates the oauth_credentials variable with the new granted token
54 55 56 57 58 |
# File 'lib/google_client/user.rb', line 54 def refresh! refresh_token, client_id, client_secret data = refresh refresh_token, client_id, client_secret @oauth_credentials = data["access_token"] data end |