Class: HttpObservatory::Request
- Inherits:
-
Object
- Object
- HttpObservatory::Request
- Defined in:
- lib/http_observatory/request.rb
Class Method Summary collapse
- .api ⇒ Object
- .errors(response) ⇒ Object
- .get(path, query = {}) ⇒ Object
- .get_json(root_path, query = {}) ⇒ Object
- .post(path, query) ⇒ Object
- .post_json(root_path, query = {}) ⇒ Object
Class Method Details
.api ⇒ Object
33 34 35 |
# File 'lib/http_observatory/request.rb', line 33 def api Connection.api end |
.errors(response) ⇒ Object
16 17 18 19 |
# File 'lib/http_observatory/request.rb', line 16 def errors(response) error = { errors: { status: response["status"], message: response["message"] } } response.merge(error) end |
.get(path, query = {}) ⇒ Object
6 7 8 9 |
# File 'lib/http_observatory/request.rb', line 6 def get(path, query = {}) response, status = get_json(path, query) status == 200 ? response : errors(response) end |
.get_json(root_path, query = {}) ⇒ Object
21 22 23 24 25 |
# File 'lib/http_observatory/request.rb', line 21 def get_json(root_path, query = {}) query_string = query.map{|k,v| "#{k}=#{v}"}.join("&") response = api.get("#{root_path}?#{query_string}") [JSON.parse(response.body), response.status] end |
.post(path, query) ⇒ Object
11 12 13 14 |
# File 'lib/http_observatory/request.rb', line 11 def post(path, query) response, status = post_json(path, query) status == 201 ? response : errors(response) end |
.post_json(root_path, query = {}) ⇒ Object
27 28 29 30 31 |
# File 'lib/http_observatory/request.rb', line 27 def post_json(root_path, query = {}) query_string = query.map{|k,v| "#{k}=#{v}"}.join("&") response = api.post("#{root_path}?#{query_string}") [JSON.parse(response.body), response.status] end |