Class: GData::Client::Base
- Inherits:
-
Object
- Object
- GData::Client::Base
- Defined in:
- lib/gdata/client/base.rb
Overview
A client object used to interact with different Google Data APIs.
Direct Known Subclasses
Apps, Blogger, BookSearch, Calendar, Contacts, DocList, Finance, GBase, GMail, Health, Notebook, Photos, Spreadsheets, WebmasterTools, YouTube
Instance Attribute Summary (collapse)
-
- (Object) auth_handler
A subclass of GData::Auth that handles authentication signing.
-
- (Object) authsub_scope
The broadest AuthSub scope for working with an API.
-
- (Object) clientlogin_service
A default service name for ClientLogin (overriden by subclasses).
-
- (Object) clientlogin_url
The default URL for ClientLogin.
-
- (Object) headers
Headers to include in every request.
-
- (Object) http_service
A subclass of GData::HTTP that handles making HTTP requests.
-
- (Object) source
A short string identifying the current application.
-
- (Object) version
The API version being used.
Instance Method Summary (collapse)
-
- (Object) authsub_private_key=(key)
Sets a private key to use with AuthSub requests.
-
- (Object) authsub_token=(token)
Sets an AuthSub token for the service.
- - (Object) authsub_url(next_url, secure = false, session = true, domain = nil, scope = nil)
-
- (Object) clientlogin(username, password, captcha_token = nil, captcha_answer = nil, service = nil, account_type = nil)
Performs ClientLogin for the service.
-
- (Object) delete(url)
Performs an HTTP DELETE against the API.
-
- (Object) get(url)
Performs an HTTP GET against the API.
-
- (Base) initialize(options = {})
constructor
A new instance of Base.
-
- (Object) make_file_request(method, url, file_path, mime_type, entry = nil)
Sends an HTTP request with the given file as a stream.
-
- (Object) make_request(method, url, body = '')
Sends an HTTP request and return the response.
-
- (Object) post(url, body)
Performs an HTTP POST against the API.
-
- (Object) post_file(url, file_path, mime_type, entry = nil)
Performs an HTTP POST with the given file.
-
- (Object) prepare_headers
Constructs some necessary headers for every request.
-
- (Object) put(url, body)
Performs an HTTP PUT against the API.
-
- (Object) put_file(url, file_path, mime_type, entry = nil)
Performs an HTTP PUT with the given file.
Constructor Details
- (Base) initialize(options = {})
A new instance of Base
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gdata/client/base.rb', line 40 def initialize( = {}) .each do |key, value| self.send("#{key}=", value) end @headers ||= {} @http_service ||= GData::HTTP::DefaultService @version ||= '2' @source ||= 'AnonymousApp' end |
Instance Attribute Details
- (Object) auth_handler
A subclass of GData::Auth that handles authentication signing.
23 24 25 |
# File 'lib/gdata/client/base.rb', line 23 def auth_handler @auth_handler end |
- (Object) authsub_scope
The broadest AuthSub scope for working with an API. This is overriden by the service-specific subclasses.
36 37 38 |
# File 'lib/gdata/client/base.rb', line 36 def authsub_scope @authsub_scope end |
- (Object) clientlogin_service
A default service name for ClientLogin (overriden by subclasses).
33 34 35 |
# File 'lib/gdata/client/base.rb', line 33 def clientlogin_service @clientlogin_service end |
- (Object) clientlogin_url
The default URL for ClientLogin.
31 32 33 |
# File 'lib/gdata/client/base.rb', line 31 def clientlogin_url @clientlogin_url end |
- (Object) headers
Headers to include in every request.
27 28 29 |
# File 'lib/gdata/client/base.rb', line 27 def headers @headers end |
- (Object) http_service
A subclass of GData::HTTP that handles making HTTP requests.
25 26 27 |
# File 'lib/gdata/client/base.rb', line 25 def http_service @http_service end |
- (Object) source
A short string identifying the current application.
38 39 40 |
# File 'lib/gdata/client/base.rb', line 38 def source @source end |
- (Object) version
The API version being used.
29 30 31 |
# File 'lib/gdata/client/base.rb', line 29 def version @version end |
Instance Method Details
- (Object) authsub_private_key=(key)
Sets a private key to use with AuthSub requests.
174 175 176 177 178 179 180 |
# File 'lib/gdata/client/base.rb', line 174 def authsub_private_key=(key) if @auth_handler.class == GData::Auth::AuthSub @auth_handler.private_key = key else raise Error, "An AuthSub token must be set first." end end |
- (Object) authsub_token=(token)
Sets an AuthSub token for the service.
169 170 171 |
# File 'lib/gdata/client/base.rb', line 169 def authsub_token=(token) self.auth_handler = GData::Auth::AuthSub.new(token) end |
- (Object) authsub_url(next_url, secure = false, session = true, domain = nil, scope = nil)
160 161 162 163 164 165 166 |
# File 'lib/gdata/client/base.rb', line 160 def authsub_url(next_url, secure = false, session = true, domain = nil, scope = nil) if scope.nil? scope = @authsub_scope end GData::Auth::AuthSub.get_url(next_url, scope, secure, session, domain) end |
- (Object) clientlogin(username, password, captcha_token = nil, captcha_answer = nil, service = nil, account_type = nil)
Performs ClientLogin for the service. See GData::Auth::ClientLogin for details.
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/gdata/client/base.rb', line 146 def clientlogin(username, password, captcha_token = nil, captcha_answer = nil, service = nil, account_type = nil) if service.nil? service = @clientlogin_service end = { :account_type => account_type } self.auth_handler = GData::Auth::ClientLogin.new(service, ) if @clientlogin_url @auth_handler.auth_url = @clientlogin_url end source = GData::Auth::SOURCE_LIB_STRING + @source @auth_handler.get_token(username, password, source, captcha_token, captcha_answer) end |
- (Object) delete(url)
Performs an HTTP DELETE against the API.
128 129 130 |
# File 'lib/gdata/client/base.rb', line 128 def delete(url) return self.make_request(:delete, url) end |
- (Object) get(url)
Performs an HTTP GET against the API.
103 104 105 |
# File 'lib/gdata/client/base.rb', line 103 def get(url) return self.make_request(:get, url) end |
- (Object) make_file_request(method, url, file_path, mime_type, entry = nil)
Sends an HTTP request with the given file as a stream
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gdata/client/base.rb', line 52 def make_file_request(method, url, file_path, mime_type, entry = nil) if not File.readable?(file_path) raise ArgumentError, "File #{file_path} is not readable." end file = File.open(file_path, 'rb') @headers['Slug'] = File.basename(file_path) if entry @headers['MIME-Version'] = '1.0' body = GData::HTTP::MimeBody.new(entry, file, mime_type) @headers['Content-Type'] = body.content_type response = self.make_request(method, url, body) else @headers['Content-Type'] = mime_type response = self.make_request(method, url, file) end file.close return response end |
- (Object) make_request(method, url, body = '')
Sends an HTTP request and return the response.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gdata/client/base.rb', line 72 def make_request(method, url, body = '') headers = self.prepare_headers request = GData::HTTP::Request.new(url, :headers => headers, :method => method, :body => body) if @auth_handler and @auth_handler.respond_to?(:sign_request!) @auth_handler.sign_request!(request) end service = http_service.new response = service.make_request(request) case response.status_code when 200, 201, 302 #Do nothing, it's a success. when 401, 403 raise AuthorizationError.new(response) when 400 raise BadRequestError.new(response) when 409 raise VersionConflictError.new(response) when 500 raise ServerError.new(response) else raise UnknownError.new(response) end return response end |
- (Object) post(url, body)
Performs an HTTP POST against the API.
118 119 120 |
# File 'lib/gdata/client/base.rb', line 118 def post(url, body) return self.make_request(:post, url, body) end |
- (Object) post_file(url, file_path, mime_type, entry = nil)
Performs an HTTP POST with the given file
123 124 125 |
# File 'lib/gdata/client/base.rb', line 123 def post_file(url, file_path, mime_type, entry = nil) return self.make_file_request(:post, url, file_path, mime_type, entry) end |
- (Object) prepare_headers
Constructs some necessary headers for every request.
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/gdata/client/base.rb', line 133 def prepare_headers headers = @headers headers['GData-Version'] = @version headers['User-Agent'] = GData::Auth::SOURCE_LIB_STRING + @source # by default we assume we are sending Atom entries if not headers.has_key?('Content-Type') headers['Content-Type'] = 'application/atom+xml' end return headers end |
- (Object) put(url, body)
Performs an HTTP PUT against the API.
108 109 110 |
# File 'lib/gdata/client/base.rb', line 108 def put(url, body) return self.make_request(:put, url, body) end |
- (Object) put_file(url, file_path, mime_type, entry = nil)
Performs an HTTP PUT with the given file
113 114 115 |
# File 'lib/gdata/client/base.rb', line 113 def put_file(url, file_path, mime_type, entry = nil) return self.make_file_request(:put, url, file_path, mime_type, entry) end |