Skip to content

Commit

Permalink
Allowing a configurable delete body
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjeffries committed Sep 1, 2017
1 parent c5ae379 commit 5b1ea34
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.4.6

Feature:

- You can define how to handle the body of `delete` requests now with the mapping option `send_delete_body`.

## 1.4.5

Bugfix:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Person < Flexirest::Base
get :find, "/people/:id"
put :save, "/people/:id"
post :create, "/people"
delete :remove, "/people/:id"
end
```

Expand Down Expand Up @@ -129,6 +130,12 @@ id = @person.id
end
```

For `delete` requests whether an API can handle a body or not is undefined. The default is to ignore any parameters not sent in the URL named parameters, but you can optionally specify `send_delete_body` and it will encode them in your chosen way into the body.

```
delete :remove, "/people/:id", send_delete_body: true
```

If an API returns an array of results and you have [will_paginate](https://rubygems.org/gems/will_paginate) installed then you can call the paginate method to return a particular page of the results (note: this doesn't reduce the load on the server, but it can help with pagination if you have a cached response).

```ruby
Expand Down
7 changes: 5 additions & 2 deletions lib/flexirest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ def prepare_params
if http_method == :get
@get_params = default_params.merge(params || {})
@post_params = nil
elsif http_method == :delete && @method[:options][:send_delete_body]
@post_params = default_params.merge(params || {})
@get_params = {}
else
@post_params = default_params.merge(params || {})
@get_params = {}
Expand Down Expand Up @@ -335,7 +338,7 @@ def append_get_parameters

def prepare_request_body(params = nil)
if proxy == :json_api
if http_method == :get || http_method == :delete
if http_method == :get || (http_method == :delete && !@method[:options][:send_delete_body])
@body = ""
else
headers["Content-Type"] ||= "application/vnd.api+json"
Expand All @@ -344,7 +347,7 @@ def prepare_request_body(params = nil)

headers["Accept"] ||= "application/vnd.api+json"
JsonAPIProxy::Headers.save(headers)
elsif http_method == :get || http_method == :delete
elsif http_method == :get || (http_method == :delete && !@method[:options][:send_delete_body])
@body = ""
elsif request_body_type == :form_encoded
@body ||= (params || @post_params || {}).to_query
Expand Down
2 changes: 1 addition & 1 deletion lib/flexirest/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Flexirest
VERSION = "1.4.5"
VERSION = "1.4.6"
end
11 changes: 11 additions & 0 deletions spec/lib/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ExampleClient < Flexirest::Base
put :update, "/put/:id"
put :conversion, "/put/:id", parse_fields: [:converted]
delete :remove, "/remove/:id"
delete :remove_body, "/remove/:id", send_delete_body: true
get :hal, "/hal", fake:"{\"_links\":{\"child\": {\"href\": \"/child/1\"}, \"other\": {\"href\": \"/other/1\"}, \"cars\":[{\"href\": \"/car/1\", \"name\":\"car1\"}, {\"href\": \"/car/2\", \"name\":\"car2\"}, {\"href\": \"/car/not-embed\", \"name\":\"car_not_embed\"} ], \"lazy\": {\"href\": \"/lazy/load\"}, \"invalid\": [{\"href\": \"/invalid/1\"}]}, \"_embedded\":{\"other\":{\"name\":\"Jane\"},\"child\":{\"name\":\"Billy\"}, \"cars\":[{\"_links\": {\"self\": {\"href\": \"/car/1\"} }, \"make\": \"Bugatti\", \"model\": \"Veyron\"}, {\"_links\": {\"self\": {\"href\": \"/car/2\"} }, \"make\": \"Ferrari\", \"model\": \"F458 Italia\"} ], \"invalid\": [{\"present\":true, \"_links\": {} } ] } }", has_many:{other:ExampleOtherClient}
get :fake, "/fake", fake:"{\"result\":true, \"list\":[1,2,3,{\"test\":true}], \"child\":{\"grandchild\":{\"test\":true}}}"
get :fake_proc, "/fake", fake:->(request) { "{\"result\":#{request.get_params[:id]}}" }
Expand Down Expand Up @@ -150,6 +151,16 @@ class WhitelistedDateClient < Flexirest::Base
ExampleClient.remove(id:1)
end

it "should get an HTTP connection when called and call delete with a body if send_delete_body is specified" do
expect_any_instance_of(Flexirest::Connection).to receive(:delete).with("/remove/1", "something=else", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{"result":true}', response_headers:{})))
ExampleClient.remove_body(id:1, something: "else")
end

it "should get an HTTP connection when called and call delete without a body if send_delete_body is not specified" do
expect_any_instance_of(Flexirest::Connection).to receive(:delete).with("/remove/1", "", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{"result":true}', response_headers:{})))
ExampleClient.remove(id:1, something: "else")
end

it "should work with faraday response objects" do
response = Faraday::Response.new
allow(response).to receive(:body).and_return({}.to_json)
Expand Down

0 comments on commit 5b1ea34

Please sign in to comment.