Skip to content

Commit b1796ad

Browse files
committed
init
0 parents  commit b1796ad

File tree

20 files changed

+480
-0
lines changed

20 files changed

+480
-0
lines changed

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
ruby:
13+
- 3.1.6
14+
- 3.2.4
15+
- 3.3.3
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- uses: ruby/setup-ruby@v1
21+
with:
22+
ruby-version: ${{ matrix.ruby }}
23+
bundler-cache: true
24+
25+
- run: bundle exec rake

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
10+
# rspec failure tracking
11+
.rspec_status

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec
4+
5+
gem 'rake'
6+
gem 'rspec'
7+
gem 'webmock'

Gemfile.lock

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
PATH
2+
remote: .
3+
specs:
4+
fetch-api (0.1.0)
5+
net-http
6+
rack
7+
singleton
8+
uri
9+
10+
GEM
11+
remote: https://rubygems.org/
12+
specs:
13+
addressable (2.8.7)
14+
public_suffix (>= 2.0.2, < 7.0)
15+
bigdecimal (3.1.8)
16+
crack (1.0.0)
17+
bigdecimal
18+
rexml
19+
diff-lcs (1.5.1)
20+
hashdiff (1.1.0)
21+
net-http (0.4.1)
22+
uri
23+
public_suffix (6.0.0)
24+
rack (3.1.6)
25+
rake (13.2.1)
26+
rexml (3.3.1)
27+
strscan
28+
rspec (3.13.0)
29+
rspec-core (~> 3.13.0)
30+
rspec-expectations (~> 3.13.0)
31+
rspec-mocks (~> 3.13.0)
32+
rspec-core (3.13.0)
33+
rspec-support (~> 3.13.0)
34+
rspec-expectations (3.13.1)
35+
diff-lcs (>= 1.2.0, < 2.0)
36+
rspec-support (~> 3.13.0)
37+
rspec-mocks (3.13.1)
38+
diff-lcs (>= 1.2.0, < 2.0)
39+
rspec-support (~> 3.13.0)
40+
rspec-support (3.13.1)
41+
singleton (0.2.0)
42+
strscan (3.1.0)
43+
uri (0.13.0)
44+
webmock (3.23.1)
45+
addressable (>= 2.8.0)
46+
crack (>= 0.3.2)
47+
hashdiff (>= 0.4.0, < 2.0.0)
48+
49+
PLATFORMS
50+
ruby
51+
x86_64-linux
52+
53+
DEPENDENCIES
54+
fetch-api!
55+
rake
56+
rspec
57+
webmock
58+
59+
BUNDLED WITH
60+
2.5.14

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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).

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'bundler/gem_tasks'
2+
require 'rspec/core/rake_task'
3+
4+
RSpec::Core::RakeTask.new :spec
5+
6+
task default: :spec

bin/console

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env ruby
2+
require 'bundler/setup'
3+
require 'fetch'
4+
5+
# You can add fixtures and/or initialization code here to make experimenting
6+
# with your gem easier. You can also use a different console, if you like.
7+
8+
require 'irb'
9+
IRB.start(__FILE__)

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

0 commit comments

Comments
 (0)