class GoogleContactsApi::Api
Constants
- BASE_URL
keep separate in case of new auth method
Attributes
oauth[R]
Public Class Methods
new(oauth)
click to toggle source
# File lib/google_contacts_api/api.rb, line 13 def initialize(oauth) # TODO: Later, accept ClientLogin @oauth = oauth end
parse_response_code(response)
click to toggle source
Parse the response code Needed because of difference between oauth and oauth2 gems
# File lib/google_contacts_api/api.rb, line 64 def self.parse_response_code(response) (defined?(response.code) ? response.code : response.status).to_i end
Public Instance Methods
delete(link, params = {}, headers = {})
click to toggle source
Delete request to specified link, with query params Not tried yet
# File lib/google_contacts_api/api.rb, line 56 def delete(link, params = {}, headers = {}) raise NotImplementedError params["alt"] = "json" @oauth.delete("#{BASE_URL}#{link}?#{params.to_query}", headers) end
get(link, params = {}, headers = {})
click to toggle source
Get request to specified link, with query params For get, post, put, delete, always use JSON, it’s simpler and lets us use Hashie::Mash. Note that in the JSON conversion from XML, “:” is replaced with $, element content is keyed with $t Raise UnauthorizedError
if not authorized.
# File lib/google_contacts_api/api.rb, line 23 def get(link, params = {}, headers = {}) merged_params = params_with_defaults(params) begin result = @oauth.get("#{BASE_URL}#{link}?#{merged_params.to_query}", headers) rescue => e # TODO: OAuth 2.0 will raise a real error raise UnauthorizedError if defined?(e.response) && self.class.parse_response_code(e.response) == 401 raise e end # OAuth 1.0 uses Net::HTTP internally raise UnauthorizedError if result.is_a?(Net::HTTPUnauthorized) result end
post(link, params = {}, headers = {})
click to toggle source
Post request to specified link, with query params Not tried yet, might be issues with params
# File lib/google_contacts_api/api.rb, line 40 def post(link, params = {}, headers = {}) raise NotImplementedError params["alt"] = "json" @oauth.post("#{BASE_URL}#{link}?#{params.to_query}", headers) end
put(link, params = {}, headers = {})
click to toggle source
Put request to specified link, with query params Not tried yet
# File lib/google_contacts_api/api.rb, line 48 def put(link, params = {}, headers = {}) raise NotImplementedError params["alt"] = "json" @oauth.put("#{BASE_URL}#{link}?#{params.to_query}", headers) end
Private Instance Methods
params_with_defaults(params)
click to toggle source
# File lib/google_contacts_api/api.rb, line 70 def params_with_defaults(params) p = params.merge({ "alt" => "json" }) p['v'] = '3' unless p['v'] p end