lib/genius/resource.rb in genius-0.1.0 vs lib/genius/resource.rb in genius-0.1.1
- old
+ new
@@ -8,31 +8,48 @@
format :json
attr_reader :raw_response, :resource
def self.resource_name(resource_name = nil)
- if resource_name
- @resource_name = resource_name
- end
-
+ @resource_name = resource_name if resource_name
@resource_name || name.downcase.split("::").last
end
def self.find(id, params: {}, headers: {})
params = default_params.merge(params)
headers = default_headers.merge(headers)
- new(http_get("/#{resource_name}s/#{id}", query: params, headers: headers),
- text_format: params[:text_format])
+ new(http_get("/#{resource_name}s/#{id}",
+ query: params,
+ headers: headers),
+ text_format: params[:text_format])
end
def reload
self.class.find(id, params: { text_format: text_format })
end
def self.http_get(path, query: {}, headers: {})
response = get(path, query: query, headers: headers)
+ handle_response(response)
+ end
+ def self.http_post(path, body: {}, headers: {})
+ response = post(path, body: body, headers: headers)
+ handle_response(response)
+ end
+
+ def self.http_put(path, body: {}, headers: {})
+ response = put(path, body: body, headers: headers)
+ handle_response(response)
+ end
+
+ def self.http_delete(path, headers: {})
+ response = delete(path, headers: headers)
+ handle_response(response)
+ end
+
+ def self.handle_response(response)
case response.code
when 404 then raise NotFoundError
when 401, 403 then raise AuthenticationError
else response
end