Skip to content

Commit 7ed675a

Browse files
committed
Add json_parse_options config
1 parent 1fcce78 commit 7ed675a

File tree

7 files changed

+32
-4
lines changed

7 files changed

+32
-4
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ Fetch.configure do |config|
129129
end
130130
```
131131

132+
### Parsing JSON
133+
134+
By default, `Fetch::Response#json` calls `JSON.parse` without options. Additional options can be specified in `Fetch.config.json_parse_options`.
135+
136+
``` ruby
137+
Fetch.configure do |config|
138+
config.json_parse_options = {
139+
symbolize_names: true
140+
}
141+
end
142+
```
143+
132144
## Development
133145

134146
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/fetch.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ def self.configure(&block)
1212
configure do |config|
1313
config.connection_max_idle_time = 30
1414
config.on_connection_create = -> (*) {}
15+
config.json_parse_options = {}
1516
end
1617
end

lib/fetch/config.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module Fetch
2-
Config = Struct.new(:connection_max_idle_time, :on_connection_create)
2+
Config = Struct.new(
3+
:connection_max_idle_time,
4+
:on_connection_create,
5+
:json_parse_options
6+
)
37
end

lib/fetch/response.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def status_text
2121
Rack::Utils::HTTP_STATUS_CODES[status]
2222
end
2323

24-
def json(...)
24+
def json(**json_parse_options)
2525
return nil unless body
2626

27-
JSON.parse(body, ...)
27+
JSON.parse(body, **Fetch.config.json_parse_options, **json_parse_options)
2828
end
2929
end
3030
end

sig/fetch/config.rbs

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module Fetch
22
class Config
33
attr_accessor connection_max_idle_time: Integer
44
attr_accessor on_connection_create: ^(Net::HTTP, URI::HTTP) -> void
5+
attr_accessor json_parse_options: json_options
56
end
67
end

sig/fetch/response.rbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ module Fetch
99
def initialize: (url: String, status: Integer, headers: Headers, body: String?, redirected: bool) -> void
1010
def ok: () -> bool
1111
def status_text: () -> String
12-
def json: (json_options) -> untyped
12+
def json: (**json_options) -> untyped
1313
end
1414
end

spec/fetch/response_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'spec_helper'
22

3+
require 'active_support/core_ext/object/with'
4+
35
RSpec.describe Fetch::Response do
46
def create_response(url: 'http://example.com', status: 200, headers: [], body: nil, redirected: false)
57
Fetch::Response.new(url:, status:, headers:, body:, redirected:)
@@ -24,4 +26,12 @@ def create_response(url: 'http://example.com', status: 200, headers: [], body: n
2426

2527
expect(res.json).to eq('foo' => 'bar')
2628
end
29+
30+
example '#json with config' do
31+
Fetch::config.with json_parse_options: {symbolize_names: true} do
32+
res = create_response(body: '{"foo":"bar"}')
33+
34+
expect(res.json).to eq(foo: 'bar')
35+
end
36+
end
2737
end

0 commit comments

Comments
 (0)