Class: GoogleContactsApi::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/google_contacts_api/api.rb

Constant Summary collapse

BASE_URL =

keep separate in case of new auth method

"https://www.google.com/m8/feeds/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth) ⇒ Api

Returns a new instance of Api.



13
14
15
16
# File 'lib/google_contacts_api/api.rb', line 13

def initialize(oauth)
  # TODO: Later, accept ClientLogin
  @oauth = oauth
end

Instance Attribute Details

#oauthObject (readonly)

Returns the value of attribute oauth.



12
13
14
# File 'lib/google_contacts_api/api.rb', line 12

def oauth
  @oauth
end

Class Method Details

.parse_response_code(response) ⇒ Object

Parse the response code Needed because of difference between oauth and oauth2 gems



64
65
66
# 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

Instance Method Details

#delete(link, params = {}, headers = {}) ⇒ Object

Delete request to specified link, with query params Not tried yet

Raises:

  • (NotImplementedError)


56
57
58
59
60
# 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 = {}) ⇒ Object

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.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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 = {}) ⇒ Object

Post request to specified link, with query params Not tried yet, might be issues with params

Raises:

  • (NotImplementedError)


40
41
42
43
44
# 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 = {}) ⇒ Object

Put request to specified link, with query params Not tried yet

Raises:

  • (NotImplementedError)


48
49
50
51
52
# 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