Skip to content

Commit

Permalink
[ruby] Use Marcel to detect content-type in form-data file upload usi…
Browse files Browse the repository at this point in the history
…ng faraday
  • Loading branch information
yoheyk committed Mar 27, 2024
1 parent e78aeb6 commit b5687ce
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 15 deletions.
4 changes: 1 addition & 3 deletions CI/circle_parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if [ "$NODE_INDEX" = "1" ]; then
echo "Running node $NODE_INDEX ..."
java -version

sudo apt-get -y install cpanminus libmagic-dev
sudo apt-get -y install cpanminus

(cd samples/client/petstore/perl && /bin/bash ./test.bash)
(cd samples/client/petstore/ruby && mvn integration-test)
Expand Down Expand Up @@ -112,5 +112,3 @@ else
(cd samples/client/petstore/java/apache-httpclient && mvn integration-test)

fi


Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require 'typhoeus'
{{#isFaraday}}
require 'faraday'
require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
require 'filemagic'
require 'marcel'
{{/isFaraday}}
{{#isHttpx}}
require 'httpx'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@
form_params.each do |key, value|
case value
when ::File, ::Tempfile
filemagic = FileMagic.new(FileMagic::MAGIC_MIME)
data[key] = Faraday::FilePart.new(value.path, filemagic.file(value.path, true))
data[key] = Faraday::FilePart.new(value.path, Marcel::MimeType.for(Pathname.new(value.path)))
when ::Array, nil
# let Faraday handle Array and nil parameters
data[key] = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
{{#isFaraday}}
s.add_runtime_dependency 'faraday', '>= 1.0.1', '< 3.0'
s.add_runtime_dependency 'faraday-multipart'
s.add_runtime_dependency 'ruby-filemagic'
s.add_runtime_dependency 'marcel'
{{/isFaraday}}
{{#isTyphoeus}}
s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
require 'time'
require 'faraday'
require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
require 'filemagic'
require 'marcel'


module OpenapiClient
Expand Down Expand Up @@ -141,8 +141,7 @@ def build_request_body(header_params, form_params, body)
form_params.each do |key, value|
case value
when ::File, ::Tempfile
filemagic = FileMagic.new(FileMagic::MAGIC_MIME)
data[key] = Faraday::FilePart.new(value.path, filemagic.file(value.path, true))
data[key] = Faraday::FilePart.new(value.path, Marcel::MimeType.for(Pathname.new(value.path)))
when ::Array, nil
# let Faraday handle Array and nil parameters
data[key] = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'faraday', '>= 1.0.1', '< 3.0'
s.add_runtime_dependency 'faraday-multipart'
s.add_runtime_dependency 'ruby-filemagic'
s.add_runtime_dependency 'marcel'

s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
require 'time'
require 'faraday'
require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
require 'filemagic'
require 'marcel'


module Petstore
Expand Down Expand Up @@ -141,8 +141,7 @@ def build_request_body(header_params, form_params, body)
form_params.each do |key, value|
case value
when ::File, ::Tempfile
filemagic = FileMagic.new(FileMagic::MAGIC_MIME)
data[key] = Faraday::FilePart.new(value.path, filemagic.file(value.path, true))
data[key] = Faraday::FilePart.new(value.path, Marcel::MimeType.for(Pathname.new(value.path)))
when ::Array, nil
# let Faraday handle Array and nil parameters
data[key] = value
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby-faraday/petstore.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'faraday', '>= 1.0.1', '< 3.0'
s.add_runtime_dependency 'faraday-multipart'
s.add_runtime_dependency 'ruby-filemagic'
s.add_runtime_dependency 'marcel'

s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'

Expand Down

0 comments on commit b5687ce

Please sign in to comment.