Class: GData4Ruby::Service

Inherits:
Base
  • Object
show all
Defined in:
lib/gdata4ruby/service.rb

Overview

The service class is the main handler for all direct interactions with the Google Data API.

Instance Attribute Summary collapse

Attributes inherited from Base

#debug, #gdata_version, #proxy_info, #use_ssl

Instance Method Summary collapse

Methods inherited from Base

#create_url, #log, #send_request

Constructor Details

#initialize(attributes = {}) ⇒ Service

Accepts an optional attributes hash for initialization values, most likely :gdata_version



32
33
34
35
36
37
38
39
# File 'lib/gdata4ruby/service.rb', line 32

def initialize(attributes = {})
  super(attributes)
  attributes.each do |key, value|
    if self.respond_to?("#{key}=")
      self.send("#{key}=", value)
    end
  end    
end

Instance Attribute Details

#accountObject (readonly)

Convenience attribute contains the currently authenticated account name



26
27
28
# File 'lib/gdata4ruby/service.rb', line 26

def 
  @account
end

#auth_tokenObject (readonly)

The token returned by the Google servers, used to authorize all subsequent messages



29
30
31
# File 'lib/gdata4ruby/service.rb', line 29

def auth_token
  @auth_token
end

Instance Method Details

#authenticate(options = {}) ⇒ Object

The authenticate method passes the username and password to google servers.

If authentication succeeds, returns true, otherwise raises the AuthenticationFailed error. Thanks to David King and Scott Taylor for Ruby 1.9 fix.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gdata4ruby/service.rb', line 44

def authenticate(options = {})
  username = options[:username]
  password = options[:password]
  service = options[:service]
  @auth_token = nil
  ret = nil
  auth_args = "Email=#{username}&Passwd=#{password}&source=GCal4Ruby&service=#{service}&accountType=HOSTED_OR_GOOGLE"
  log(auth_args)
  ret = send_request(Request.new(:post, @@auth_url, auth_args))
  if ret.class == Net::HTTPOK
    body = ret.read_body
    lines = body.send(body.respond_to?(:lines) ? :lines : :to_s).to_a
    @auth_token = lines.to_a[2].gsub("Auth=", "").strip
    @account = username
    @password = password
    return true
  else
    @auth_token = nil
    raise AuthenticationFailed
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/gdata4ruby/service.rb', line 72

def authenticated?
  log("Authenticated: #{@auth_token}")
  return (@auth_token != nil)
end

#reauthenticate(options = {}) ⇒ Object



66
67
68
69
70
# File 'lib/gdata4ruby/service.rb', line 66

def reauthenticate(options = {})
  options[:username] ||= @account
  options[:password] ||= @password
  authenticate(options)
end