Skip to content

Commit

Permalink
Fix #add_attachment multipart upload with Faraday
Browse files Browse the repository at this point in the history
  • Loading branch information
hoppergee committed Nov 11, 2023
1 parent af12089 commit 528fc3d
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/trello/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def add_attachment(attachment, name = '')
# Is it a file object or a string (url)?
if attachment.respond_to?(:path) && attachment.respond_to?(:read)
client.post("/cards/#{id}/attachments", {
file: attachment,
file: Trello::TInternet.multipart_file(attachment),
name: name
})
else
Expand Down
4 changes: 4 additions & 0 deletions lib/trello/net.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class << self
def execute(request)
Trello.http_client.execute(request)
end

def multipart_file(file)
Trello.http_client.multipart_file(file)
end
end
end
end
31 changes: 31 additions & 0 deletions lib/trello/net/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ class TInternet
class << self
begin
require 'faraday'
require 'faraday/multipart'
require 'mime/types'
rescue LoadError
end

def execute(request)
try_execute request
end

def multipart_file(file)
Faraday::Multipart::FilePart.new(
file,
content_type(file),
filename(file)
)
end

private

def try_execute(request)
Expand All @@ -33,13 +43,34 @@ def execute_core(request)
request: { timeout: 10 }
) do |faraday|
faraday.response :raise_error
faraday.request :multipart
faraday.request :json
end

conn.send(request.verb) do |req|
req.body = request.body
end
end

def content_type(file)
return file.content_type if file.respond_to?(:content_type)

mime = MIME::Types.type_for file.path
if mime.empty?
'text/plain'
else
mime[0].content_type
end
end

def filename(file)
if file.respond_to?(:original_filename)
file.original_filename
else
File.basename(file.path)
end
end

end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/trello/net/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def execute(request)
try_execute request
end

def multipart_file(file)
file
end

private

def try_execute(request)
Expand Down
2 changes: 2 additions & 0 deletions ruby-trello.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ Gem::Specification.new do |s|
s.add_dependency 'json', '>= 2.3.0'
s.add_dependency 'oauth', '>= 0.4.5'
s.add_dependency 'faraday', '~> 2.0'
s.add_dependency 'faraday-multipart', '~> 1.0'
s.add_dependency('mime-types', '>= 3.0', '< 4.0')
end
2 changes: 1 addition & 1 deletion spec/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ module Trello

allow(client)
.to receive(:post)
.with("/cards/abcdef123456789123456789/attachments", { file: f, name: '' })
.with("/cards/abcdef123456789123456789/attachments", { file: instance_of(Multipart::Post::UploadIO), name: '' })
.and_return "not important"

card.add_attachment(f)
Expand Down
244 changes: 244 additions & 0 deletions spec/cassettes/can_add_a_file_on_a_card_with_farady.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion spec/integration/card/add_and_remove_attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

describe '#add_attachment' do
it 'can success add an attachment(file) on a card' do
VCR.use_cassette('can_add_a_file_on_a_card') do
cassette_name = Trello.http_client == "rest-client" ? "can_add_a_file_on_a_card" : "can_add_a_file_on_a_card_with_farady"
VCR.use_cassette(cassette_name) do
card = Trello::Card.find('5e95d1b4f43f9a06497f17f7')
file = File.new('spec/integration/card/add_and_remove_attachment_spec.rb', 'r')

Expand Down

0 comments on commit 528fc3d

Please sign in to comment.