Skip to content

Commit 5af010e

Browse files
author
蔡耀賢
committed
Merge branch 'upgrade-faraday' into 'master'
Upgrade faraday See merge request kdanmobile/shared-code-base/gems/json_requester!17
2 parents b795035 + caf6e7d commit 5af010e

12 files changed

Lines changed: 343 additions & 37 deletions

.gitlab-ci.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@ include:
66
stages:
77
- test
88
- sast_upload
9+
- rspec
910
- publish
1011

12+
rspec:
13+
stage: rspec
14+
image: ruby:3.3.5
15+
tags:
16+
- arm64
17+
script:
18+
- bundle install
19+
- rspec spec -fd
20+
only:
21+
- master
22+
1123
to_gem:
1224
stage: publish
1325
image: ruby:3.3.5
@@ -23,4 +35,4 @@ to_gem:
2335
- gem build json_requester.gemspec
2436
- gem push $GEM_NAME
2537
only:
26-
- master
38+
- /release-\d+\.\d+\.\d+(\.\d+)?/

.rspec

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

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## [2.0.0] - 2025-04-25
2+
3+
### Breaking changes
4+
5+
- Upgrade Faraday to version 2.x.
6+
- Remove support for Ruby 2.7 and below.
7+
8+
### Features
9+
- Add rspec for testing.
10+
111
## [1.1.3] - 2024-10-28
212
- Replace `.present?` method with `object_present?` for string in `json_send` method.
313

Gemfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ source 'https://rubygems.org'
22

33
gemspec
44

5-
# http request
6-
gem 'faraday', '~> 1.10'
5+
# Simple, but flexible HTTP client library, with support for multiple backends.
6+
gem 'faraday', '~> 2.13.1'
7+
# Perform multipart-post requests using Faraday.
8+
gem 'faraday-multipart', '~> 1.1.0'

Gemfile.lock

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,70 @@
11
PATH
22
remote: .
33
specs:
4-
json_requester (1.1.3)
5-
faraday (~> 1.10, >= 1.10.3)
4+
json_requester (2.0.0)
5+
faraday (~> 2.0, >= 2.0.1)
6+
faraday-multipart (~> 1.1.0)
67

78
GEM
89
remote: https://rubygems.org/
910
specs:
11+
addressable (2.8.7)
12+
public_suffix (>= 2.0.2, < 7.0)
13+
bigdecimal (3.1.9)
1014
coderay (1.1.3)
11-
faraday (1.10.4)
12-
faraday-em_http (~> 1.0)
13-
faraday-em_synchrony (~> 1.0)
14-
faraday-excon (~> 1.1)
15-
faraday-httpclient (~> 1.0)
16-
faraday-multipart (~> 1.0)
17-
faraday-net_http (~> 1.0)
18-
faraday-net_http_persistent (~> 1.0)
19-
faraday-patron (~> 1.0)
20-
faraday-rack (~> 1.0)
21-
faraday-retry (~> 1.0)
22-
ruby2_keywords (>= 0.0.4)
23-
faraday-em_http (1.0.0)
24-
faraday-em_synchrony (1.0.0)
25-
faraday-excon (1.1.0)
26-
faraday-httpclient (1.0.1)
27-
faraday-multipart (1.0.4)
28-
multipart-post (~> 2)
29-
faraday-net_http (1.0.2)
30-
faraday-net_http_persistent (1.2.0)
31-
faraday-patron (1.0.0)
32-
faraday-rack (1.0.0)
33-
faraday-retry (1.0.3)
15+
crack (1.0.0)
16+
bigdecimal
17+
rexml
18+
diff-lcs (1.6.1)
19+
faraday (2.13.1)
20+
faraday-net_http (>= 2.0, < 3.5)
21+
json
22+
logger
23+
faraday-multipart (1.1.0)
24+
multipart-post (~> 2.0)
25+
faraday-net_http (3.4.0)
26+
net-http (>= 0.5.0)
27+
hashdiff (1.1.2)
28+
json (2.11.3)
29+
logger (1.7.0)
3430
method_source (1.1.0)
3531
multipart-post (2.4.1)
32+
net-http (0.6.0)
33+
uri
3634
pry (0.14.2)
3735
coderay (~> 1.1)
3836
method_source (~> 1.0)
39-
ruby2_keywords (0.0.5)
37+
public_suffix (6.0.1)
38+
rexml (3.4.1)
39+
rspec (3.13.0)
40+
rspec-core (~> 3.13.0)
41+
rspec-expectations (~> 3.13.0)
42+
rspec-mocks (~> 3.13.0)
43+
rspec-core (3.13.3)
44+
rspec-support (~> 3.13.0)
45+
rspec-expectations (3.13.3)
46+
diff-lcs (>= 1.2.0, < 2.0)
47+
rspec-support (~> 3.13.0)
48+
rspec-mocks (3.13.2)
49+
diff-lcs (>= 1.2.0, < 2.0)
50+
rspec-support (~> 3.13.0)
51+
rspec-support (3.13.2)
52+
uri (1.0.3)
53+
webmock (3.25.1)
54+
addressable (>= 2.8.0)
55+
crack (>= 0.3.2)
56+
hashdiff (>= 0.4.0, < 2.0.0)
4057

4158
PLATFORMS
4259
ruby
4360

4461
DEPENDENCIES
45-
faraday (~> 1.10)
62+
faraday (~> 2.13.1)
63+
faraday-multipart (~> 1.1.0)
4664
json_requester!
4765
pry (~> 0.14.2)
66+
rspec (~> 3.0)
67+
webmock (~> 3.25, >= 3.25.1)
4868

4969
BUNDLED WITH
50-
2.5.22
70+
2.6.8

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44

55
### Install
66

7+
If you want to use faraday 1.x version, install json_requester 1.x version:
78
```bash
8-
$ gem install json_requester
9+
$ gem install json_requester -v '~> 1.0'
10+
```
11+
12+
If you want to use faraday 2.x version, install json_requester 2.x version:
13+
```bash
14+
$ gem install json_requester -v '~> 2.0'
915
```
1016

1117
### How to Use
1218

1319
```ruby
20+
# initialize the JsonRequester class
1421
host = 'http://httpbingo.org'
1522
# `timeout` at Faraday gem default is 60 secs.
1623
# `user_agent` at Faraday gem default is like "Faraday v1.10.0", it would be deep_merge at Faraday default setting.
24+
# `multipart` option enables multipart form requests (for file uploads), default is false
25+
# `ssl_verify` controls SSL certificate verification, default is true
1726
requester = JsonRequester.new(host, timeout: 120, user_agent: 'My Agent 1.2')
1827

1928
http_method = :get # :get / :post / :put / :delete
@@ -31,10 +40,30 @@ $ gem install json_requester
3140
# other methods: `form_send`, `multipart_form_send`
3241
# `sort_params` at Faraday gem default is true.
3342
# `content_type_charset` default is 'utf-8', this will add ; charset=utf-8 after `Content-Type` header (ex. `Content-Type=application/json; charset=utf-8`).
43+
# `need_response_header` when set to true will include response headers in the result
3444
res = requester.http_send(http_method, path, params, headers, sort_params: true, content_type_charset: 'utf-8')
3545

3646
# http response code
3747
puts res['status'] # 200, 404, .. etc
3848
# the response JSON body
39-
puts res['body'] # { foo: 'bar' }
49+
puts res['body'] # { foo: 'bar' }
50+
51+
# If need_response_header is true, you can access response headers
52+
puts res['headers'] if res.key?('headers')
53+
54+
# For form-encoded requests (application/x-www-form-urlencoded)
55+
form_res = requester.form_send(:post, '/post', params, headers)
56+
57+
# For file uploads or multipart form requests
58+
# First initialize with multipart: true
59+
multipart_requester = JsonRequester.new(host, multipart: true)
60+
61+
# Then prepare your payload with file objects
62+
upload_params = {
63+
file: Faraday::Multipart::FilePart.new('path/to/file.txt', 'text/plain'),
64+
description: 'File upload example'
65+
}
66+
67+
# Send multipart request
68+
upload_res = multipart_requester.multipart_form_send(:post, '/upload_path', upload_params, headers)
4069
```

VERSION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.3
1+
2.0.0

json_requester.gemspec

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ Gem::Specification.new do |s|
1818
s.rdoc_options = ['--charset=UTF-8']
1919
s.require_path = "lib"
2020

21-
s.required_ruby_version = '>= 2.5.1'
22-
s.add_runtime_dependency 'faraday', '~> 1.10', '>= 1.10.3'
21+
s.required_ruby_version = '>= 3.0.0'
22+
s.add_runtime_dependency "faraday", "~> 2.0", ">= 2.0.1"
23+
s.add_runtime_dependency 'faraday-multipart', '~> 1.1.0'
2324
s.add_development_dependency 'pry', '~> 0.14.2'
25+
s.add_development_dependency "rspec", "~> 3.0"
26+
s.add_development_dependency "webmock", "~> 3.25", ">= 3.25.1"
2427
end

lib/json_requester.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'faraday'
2+
require 'faraday/multipart'
23
require 'json'
34

45
class JsonRequester
@@ -75,8 +76,12 @@ def init_conn(sort_params: true)
7576
# https://lostisland.github.io/faraday/#/customization/index?id=order-of-parameters
7677
Faraday::NestedParamsEncoder.sort_params = sort_params # faraday default is true
7778
Faraday.default_connection_options = { headers: { user_agent: @user_agent } } unless @user_agent.empty?
79+
options = {
80+
url: host,
81+
ssl: { verify: @ssl_verify }
82+
}
7883

79-
Faraday.new(url: host, ssl: { verify: @ssl_verify }) do |faraday|
84+
Faraday.new(options) do |faraday|
8085
faraday.request :multipart if @multipart # multipart form POST request
8186
faraday.request :url_encoded # form-encode POST params
8287
faraday.response :logger # log requests to $stdout
@@ -112,6 +117,7 @@ def object_present?(object)
112117
def object_blank?(object)
113118
# Ref: https://github.com/rails/rails/blob/v7.1.4.2/activesupport/lib/active_support/core_ext/object/blank.rb#L18
114119
# active_support blank? method
120+
return true if object.nil?
115121
object.respond_to?(:empty?) ? !!object.empty? : false
116122
end
117123

spec/fixtures/sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test file for multipart upload testing.

0 commit comments

Comments
 (0)