Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize to JSON when content type is set to JSON #142

Merged
merged 3 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,19 @@ When calling any of the methods above, you can pass request headers to be used.
get 'http://example.com/api/v1/my_api', { 'x-auth-token' => 'my_token' }
```

For requests that require a body (`post`, `put`, `patch`) you can pass the body as a hash as well:
For requests that require a body (`post`, `put`, `patch`) you can pass the body as well:

```ruby
post 'http://example.com/api/v1/my_api', { :name => 'John Doe' }, { 'x-auth-token' => 'my_token' }
```

The body may be any JSON-serializable type, as long as you want to post `application/json` content type.
You may set a different content type and post a string body this way:

```ruby
post 'http://example.com/api/v1/my_api', "Hello there!", { content_type: 'text/plain' }
```

For requests that require Query params you can pass a params hash into headers.

```ruby
Expand Down
7 changes: 6 additions & 1 deletion lib/airborne/rest_client_requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def make_request(method, url, options = {})
res = if method == :post || method == :patch || method == :put
begin
request_body = options[:body].nil? ? '' : options[:body]
request_body = request_body.to_json if options[:body].is_a?(Hash)
request_body = request_body.to_json if is_json_request(headers)
RestClient.send(method, get_url(url), request_body, headers)
rescue RestClient::Exception => e
e.response
Expand All @@ -24,6 +24,11 @@ def make_request(method, url, options = {})

private

def is_json_request(headers)
header = headers.fetch(:content_type)
header == :json || /application\/([a-zA-Z0-9\.\_\-]*\+?)json/ =~ header
end

def base_headers
{ content_type: :json }.merge(Airborne.configuration.headers || {})
end
Expand Down
21 changes: 21 additions & 0 deletions spec/airborne/client_requester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,25 @@
expect(RestClient).to have_received(:send)
.with(:get, 'http://www.example.com/foo', { content_type: 'text/plain' })
end

it 'should serialize body to json when :content_type is (default) :json' do
post '/foo', { test: 'serialized' }

expect(RestClient).to have_received(:send)
.with(:post, 'http://www.example.com/foo', '{"test":"serialized"}', { content_type: :json })
end

it 'should serialize body to json when :content_type is any enhanced JSON content type' do
post '/foo', { test: 'serialized' }, { content_type: 'application/vnd.airborne.2+json' }

expect(RestClient).to have_received(:send)
.with(:post, 'http://www.example.com/foo', '{"test":"serialized"}', { content_type: 'application/vnd.airborne.2+json' })
end

it 'should not serialize body to json when :content_type does not match JSON' do
post '/foo', { test: 'not serialized' }, { content_type: 'text/plain' }

expect(RestClient).to have_received(:send)
.with(:post, 'http://www.example.com/foo', { test: 'not serialized' }, { content_type: 'text/plain' })
end
end