fetch-api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/lib/fetch/api.rb +11 -0
- data/lib/fetch/client.rb +51 -0
- data/lib/fetch/version.rb +3 -0
- data/lib/fetch-api.rb +1 -0
- data/lib/fetch.rb +7 -0
- data/sig/fetch.rbs +4 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d8968e4c64a4d0b5370aef72a78f1b4ac04096057deb92d1bc1a6dd14353b3cd
|
4
|
+
data.tar.gz: 2b7cdf82b75cf2c2d51abd5693f1ad278242e4e93645779b0909bfa7fd62de7a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39bfda51fed25d97acb41128134615980b4eb35357a790d76c79feb1bf57c06e7f2047e2908c530d346e1b051f79b1846cf01c912209d1be6cebb7d1eeaf88c1
|
7
|
+
data.tar.gz: 42cd5a712cfe067f16e70482c6013162d2a29d76666d709d5032badc79a4202a9c474d1ba38dd19598657ae1b2b0d51f5de32ef49ce7395d6221ba1f7d752526
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Keita Urashima
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Fetch API
|
2
|
+
|
3
|
+
Ruby's Net::HTTP is very powerful, but has a complicated API. OpenURI is easy to use, but has limited functionality. Third-party HTTP clients each have different APIs, and it can sometimes be difficult to learn how to use them.
|
4
|
+
|
5
|
+
There is one HTTP client that we can all use. It is the browser's Fetch API. This is an HTTP client for Ruby that can be used in a way that is similar to the Fetch API.
|
6
|
+
|
7
|
+
This is not intended to be a complete copy of the Fetch API. For example, responses are returned synchronously rather than asynchronously, as that is more familiar behavior for Ruby programmers. It is only "like" the Fetch API.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
```
|
14
|
+
$ bundle add fetch-api
|
15
|
+
```
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
18
|
+
|
19
|
+
```
|
20
|
+
$ gem install fetch-api
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Basic
|
26
|
+
|
27
|
+
``` ruby
|
28
|
+
require 'fetch-api'
|
29
|
+
|
30
|
+
res = Fetch::API.fetch('https://example.com')
|
31
|
+
|
32
|
+
# res is a Rack::Response object
|
33
|
+
puts res.body
|
34
|
+
```
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
require 'fetch-api'
|
38
|
+
|
39
|
+
include Fetch::API
|
40
|
+
|
41
|
+
res = fetch('https://example.com')
|
42
|
+
```
|
43
|
+
|
44
|
+
### Post JSON
|
45
|
+
|
46
|
+
``` ruby
|
47
|
+
res = fetch('http://example.com', **{
|
48
|
+
method: 'POST',
|
49
|
+
|
50
|
+
headers: {
|
51
|
+
'Content-Type' => 'application/json'
|
52
|
+
},
|
53
|
+
|
54
|
+
body: {
|
55
|
+
name: 'Alice'
|
56
|
+
}.to_json
|
57
|
+
})
|
58
|
+
```
|
59
|
+
|
60
|
+
### Post Form
|
61
|
+
|
62
|
+
``` ruby
|
63
|
+
res = fetch('http://example.com', **{
|
64
|
+
method: 'POST',
|
65
|
+
|
66
|
+
headers: {
|
67
|
+
'Content-Type' => 'multipart/form-data'
|
68
|
+
},
|
69
|
+
|
70
|
+
body: Rack::Multipart.build_multipart(
|
71
|
+
file: Rack::Multipart::UploadedFile.new(io: StringIO.new('foo'), filename: 'foo.txt')
|
72
|
+
)
|
73
|
+
})
|
74
|
+
```
|
75
|
+
|
76
|
+
Note: `Rack::Multipart.build_multipart` returns nil if the parameter does not include UploadedFile.
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
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.
|
81
|
+
|
82
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ursm/fetch-api.
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/fetch/api.rb
ADDED
data/lib/fetch/client.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require_relative '../fetch'
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'rack/response'
|
6
|
+
require 'singleton'
|
7
|
+
require 'uri'
|
8
|
+
|
9
|
+
module Fetch
|
10
|
+
class Client
|
11
|
+
include Singleton
|
12
|
+
|
13
|
+
def fetch(resource, method: 'GET', headers: {}, body: nil, redirect: 'follow')
|
14
|
+
uri = URI.parse(resource)
|
15
|
+
req = Net::HTTP.const_get(method.capitalize).new(uri)
|
16
|
+
|
17
|
+
headers.each do |k, v|
|
18
|
+
req[k] = v
|
19
|
+
end
|
20
|
+
|
21
|
+
req.body = body
|
22
|
+
|
23
|
+
http = Net::HTTP.new(uri.hostname, uri.port)
|
24
|
+
http.use_ssl = uri.scheme == 'https'
|
25
|
+
|
26
|
+
res = http.start { _1.request(req) }
|
27
|
+
|
28
|
+
case res
|
29
|
+
when Net::HTTPRedirection
|
30
|
+
case redirect
|
31
|
+
when 'follow'
|
32
|
+
fetch(res['location'], method:, headers:, body:, redirect:)
|
33
|
+
when 'error'
|
34
|
+
raise RedirectError, "redirected to #{res['location']}"
|
35
|
+
when 'manual'
|
36
|
+
to_rack_response(res)
|
37
|
+
else
|
38
|
+
raise ArgumentError, "invalid redirect option: #{redirect}"
|
39
|
+
end
|
40
|
+
else
|
41
|
+
to_rack_response(res)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def to_rack_response(res)
|
48
|
+
Rack::Response.new(res.body, res.code, res.to_hash)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/fetch-api.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'fetch/api'
|
data/lib/fetch.rb
ADDED
data/sig/fetch.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fetch-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keita Urashima
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-07-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: singleton
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: uri
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- ursm@ursm.jp
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- lib/fetch-api.rb
|
79
|
+
- lib/fetch.rb
|
80
|
+
- lib/fetch/api.rb
|
81
|
+
- lib/fetch/client.rb
|
82
|
+
- lib/fetch/version.rb
|
83
|
+
- sig/fetch.rbs
|
84
|
+
homepage: https://github.com/ursm/fetch-api
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata:
|
88
|
+
homepage_uri: https://github.com/ursm/fetch-api
|
89
|
+
source_code_uri: https://github.com/ursm/fetch-api.git
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 3.1.0
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.5.14
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Something like the Fetch API for Ruby
|
109
|
+
test_files: []
|