Skip to content

Commit

Permalink
Accept Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
ursm committed Jul 6, 2024
1 parent 68e1926 commit 11c9a4b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ res = fetch('https://example.com')

Options for `fetch` method:

- `method`: HTTP method (default: `'GET'`)
- `method`: HTTP method (default: `:get`)
- `headers`: Request headers (default: `{}`)
- `body`: Request body (default: `nil`)
- `redirect`: Follow redirects (one of `'follow'`, `'error'`, `'manual'`, default: `'follow'`)
- `redirect`: Follow redirects (one of `:follow`, `:error`, `:manual`, default: `:follow`)

Methods of `Fetch::Response` object:

Expand All @@ -65,7 +65,7 @@ Methods of `Fetch::Response` object:

``` ruby
res = fetch('http://example.com', **{
method: 'POST',
method: :post,

headers: {
'Content-Type' => 'application/json'
Expand All @@ -81,7 +81,7 @@ res = fetch('http://example.com', **{

``` ruby
res = fetch('http://example.com', **{
method: 'POST',
method: :post,

body: Fetch::URLSearchParams.new(
name: 'Alice'
Expand All @@ -93,7 +93,7 @@ res = fetch('http://example.com', **{

``` ruby
res = fetch('http://example.com', **{
method: 'POST',
method: :post,

body: Fetch::FormData.build(
name: 'Alice',
Expand Down
6 changes: 3 additions & 3 deletions lib/fetch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Fetch
class Client
include Singleton

def fetch(resource, method: 'GET', headers: [], body: nil, redirect: 'follow', _redirected: false)
def fetch(resource, method: :get, headers: [], body: nil, redirect: 'follow', _redirected: false)
uri = URI.parse(resource)
req = Net::HTTP.const_get(method.capitalize).new(uri)

Expand Down Expand Up @@ -48,15 +48,15 @@ def fetch(resource, method: 'GET', headers: [], body: nil, redirect: 'follow', _

case res
when Net::HTTPRedirection
case redirect
case redirect.to_s
when 'follow'
fetch(res['Location'], method:, headers:, body:, redirect:, _redirected: true)
when 'error'
raise RedirectError, "redirected to #{res['Location']}"
when 'manual'
to_response(resource, res, _redirected)
else
raise ArgumentError, "invalid redirect option: #{redirect}"
raise ArgumentError, "invalid redirect option: #{redirect.inspect}"
end
else
to_response(resource, res, _redirected)
Expand Down
14 changes: 7 additions & 7 deletions spec/fetch/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
stub_request :post, 'http://example.com'

fetch 'http://example.com', **{
method: 'POST',
method: :post,

headers: {
'Content-Type' => 'application/json'
Expand All @@ -53,11 +53,11 @@
)
end

example 'post form' do
example 'post urlencoded' do
stub_request :post, 'http://example.com'

fetch 'http://example.com', **{
method: 'POST',
method: :post,
body: Fetch::URLSearchParams.new(name: 'Alice')
}

Expand All @@ -75,7 +75,7 @@

File.open 'spec/fixtures/files/foo.txt' do |f|
fetch 'http://example.com', **{
method: 'POST',
method: :post,

headers: {
'Content-Type' => 'multipart/form-data'
Expand Down Expand Up @@ -104,7 +104,7 @@

stub_request :get, 'http://example.com/redirected'

res = fetch('http://example.com', redirect: 'follow')
res = fetch('http://example.com', redirect: :follow)

expect(res.status).to eq(200)
expect(res.redirected).to eq(true)
Expand All @@ -118,7 +118,7 @@
})

expect {
fetch 'http://example.com', redirect: 'error'
fetch 'http://example.com', redirect: :error
}.to raise_error(Fetch::RedirectError)
end

Expand All @@ -127,7 +127,7 @@
'Location' => 'http://example.com/redirected'
})

res = fetch('http://example.com', redirect: 'manual')
res = fetch('http://example.com', redirect: :manual)

expect(res.status).to eq(302)
expect(res.redirected).to eq(false)
Expand Down

0 comments on commit 11c9a4b

Please sign in to comment.