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
-
#auth_handler ⇒ Object
A subclass of GData::Auth that handles authentication signing.
-
#authsub_scope ⇒ Object
The broadest AuthSub scope for working with an API.
-
#clientlogin_service ⇒ Object
A default service name for ClientLogin (overriden by subclasses).
-
#clientlogin_url ⇒ Object
The default URL for ClientLogin.
-
#headers ⇒ Object
Headers to include in every request.
-
#http_service ⇒ Object
A subclass of GData::HTTP that handles making HTTP requests.
-
#source ⇒ Object
A short string identifying the current application.
-
#version ⇒ Object
The API version being used.
Instance Method Summary collapse
-
#authsub_private_key=(key) ⇒ Object
Sets a private key to use with AuthSub requests.
-
#authsub_token=(token) ⇒ Object
Sets an AuthSub token for the service.
- #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) ⇒ Object
Performs ClientLogin for the service.
-
#delete(url) ⇒ Object
Performs an HTTP DELETE against the API.
-
#get(url) ⇒ Object
Performs an HTTP GET against the API.
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
-
#make_file_request(method, url, file_path, mime_type, entry = nil) ⇒ Object
Sends an HTTP request with the given file as a stream.
-
#make_request(method, url, body = '') ⇒ Object
Sends an HTTP request and return the response.
-
#oauth_info=(options) ⇒ Object
Sets OAuth info to use.
-
#post(url, body) ⇒ Object
Performs an HTTP POST against the API.
-
#post_file(url, file_path, mime_type, entry = nil) ⇒ Object
Performs an HTTP POST with the given file.
-
#prepare_headers ⇒ Object
Constructs some necessary headers for every request.
-
#put(url, body) ⇒ Object
Performs an HTTP PUT against the API.
-
#put_file(url, file_path, mime_type, entry = nil) ⇒ Object
Performs an HTTP PUT with the given file.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gdata/client/base.rb', line 39 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
#auth_handler ⇒ Object
A subclass of GData::Auth that handles authentication signing.
22 23 24 |
# File 'lib/gdata/client/base.rb', line 22 def auth_handler @auth_handler end |
#authsub_scope ⇒ Object
The broadest AuthSub scope for working with an API. This is overriden by the service-specific subclasses.
35 36 37 |
# File 'lib/gdata/client/base.rb', line 35 def authsub_scope @authsub_scope end |
#clientlogin_service ⇒ Object
A default service name for ClientLogin (overriden by subclasses).
32 33 34 |
# File 'lib/gdata/client/base.rb', line 32 def clientlogin_service @clientlogin_service end |
#clientlogin_url ⇒ Object
The default URL for ClientLogin.
30 31 32 |
# File 'lib/gdata/client/base.rb', line 30 def clientlogin_url @clientlogin_url end |
#headers ⇒ Object
Headers to include in every request.
26 27 28 |
# File 'lib/gdata/client/base.rb', line 26 def headers @headers end |
#http_service ⇒ Object
A subclass of GData::HTTP that handles making HTTP requests.
24 25 26 |
# File 'lib/gdata/client/base.rb', line 24 def http_service @http_service end |
#source ⇒ Object
A short string identifying the current application.
37 38 39 |
# File 'lib/gdata/client/base.rb', line 37 def source @source end |
#version ⇒ Object
The API version being used.
28 29 30 |
# File 'lib/gdata/client/base.rb', line 28 def version @version end |
Instance Method Details
#authsub_private_key=(key) ⇒ Object
Sets a private key to use with AuthSub requests.
173 174 175 176 177 178 179 |
# File 'lib/gdata/client/base.rb', line 173 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 |
#authsub_token=(token) ⇒ Object
Sets an AuthSub token for the service.
168 169 170 |
# File 'lib/gdata/client/base.rb', line 168 def authsub_token=(token) self.auth_handler = GData::Auth::AuthSub.new(token) end |
#authsub_url(next_url, secure = false, session = true, domain = nil, scope = nil) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/gdata/client/base.rb', line 159 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 |
#clientlogin(username, password, captcha_token = nil, captcha_answer = nil, service = nil, account_type = nil) ⇒ Object
Performs ClientLogin for the service. See GData::Auth::ClientLogin for details.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/gdata/client/base.rb', line 145 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 |
#delete(url) ⇒ Object
Performs an HTTP DELETE against the API.
127 128 129 |
# File 'lib/gdata/client/base.rb', line 127 def delete(url) return self.make_request(:delete, url) end |
#get(url) ⇒ Object
Performs an HTTP GET against the API.
102 103 104 |
# File 'lib/gdata/client/base.rb', line 102 def get(url) return self.make_request(:get, url) end |
#make_file_request(method, url, file_path, mime_type, entry = nil) ⇒ Object
Sends an HTTP request with the given file as a stream
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gdata/client/base.rb', line 51 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 |
#make_request(method, url, body = '') ⇒ Object
Sends an HTTP request and return the response.
71 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 |
# File 'lib/gdata/client/base.rb', line 71 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 |
#oauth_info=(options) ⇒ Object
Sets OAuth info to use
182 183 184 |
# File 'lib/gdata/client/base.rb', line 182 def oauth_info=() self.auth_handler = GData::Auth::OAuth.new() end |
#post(url, body) ⇒ Object
Performs an HTTP POST against the API.
117 118 119 |
# File 'lib/gdata/client/base.rb', line 117 def post(url, body) return self.make_request(:post, url, body) end |
#post_file(url, file_path, mime_type, entry = nil) ⇒ Object
Performs an HTTP POST with the given file
122 123 124 |
# File 'lib/gdata/client/base.rb', line 122 def post_file(url, file_path, mime_type, entry = nil) return self.make_file_request(:post, url, file_path, mime_type, entry) end |
#prepare_headers ⇒ Object
Constructs some necessary headers for every request.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/gdata/client/base.rb', line 132 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 |
#put(url, body) ⇒ Object
Performs an HTTP PUT against the API.
107 108 109 |
# File 'lib/gdata/client/base.rb', line 107 def put(url, body) return self.make_request(:put, url, body) end |
#put_file(url, file_path, mime_type, entry = nil) ⇒ Object
Performs an HTTP PUT with the given file
112 113 114 |
# File 'lib/gdata/client/base.rb', line 112 def put_file(url, file_path, mime_type, entry = nil) return self.make_file_request(:put, url, file_path, mime_type, entry) end |