Module: Cortex::Request
- Included in:
- Client
- Defined in:
- lib/cortex/request.rb
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #delete!(path) ⇒ Object
- #get(path, params = {}) ⇒ Object
- #parse_response(response) ⇒ Object
- #post(path, params = {}) ⇒ Object
- #put(path, params = {}) ⇒ Object
- #save(path, model) ⇒ Object
Instance Method Details
#delete(path) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/cortex/request.rb', line 29 def delete(path) response = connection.delete do |r| r.url base_url + path end response.status < 300 end |
#delete!(path) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cortex/request.rb', line 36 def delete!(path) response = connection.delete do |r| r.url base_url + path end if response.status < 300 true else raise parse_response(response) end end |
#get(path, params = {}) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/cortex/request.rb', line 5 def get(path, params = {}) response = connection.get do |r| r.url base_url + path r.params = params end parse_response(response) end |
#parse_response(response) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/cortex/request.rb', line 51 def parse_response(response) if response.status < 300 || (response.body.kind_of?(Hash) && response.body['error']) OpenStruct.new({body: response.body, headers: response.headers.select { |k| [:'Content-Range', :'X-Total-Items'].include? k } }) else OpenStruct.new({:error => response.body, :status => response.status, :original => response}) end end |
#post(path, params = {}) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/cortex/request.rb', line 13 def post(path, params = {}) response = connection.post do |r| r.url base_url + path r.body = params unless params.empty? end parse_response(response) end |
#put(path, params = {}) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/cortex/request.rb', line 21 def put(path, params = {}) response = connection.put do |r| r.url base_url + path r.body = params unless params.empty? end parse_response(response) end |
#save(path, model) ⇒ Object
47 48 49 |
# File 'lib/cortex/request.rb', line 47 def save(path, model) model[:id] ? put("#{path}/#{model[:id]}", model) : post(path, model) end |