Skip to content

Commit

Permalink
Remove logic when form data is hash
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRTi committed Sep 20, 2023
1 parent ee2527f commit 8a91e56
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/rocket_chat/messages/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def upload_file(room_id:, file:, **rest_params)
response = session.request_json(
"#{API_PREFIX}/rooms.upload/#{room_id}",
method: :post,
form_data: file_upload_hash(file: file, **rest_params)
form_data: file_upload_array(file: file, **rest_params)
)

RocketChat::Message.new response['message'] if response['success']
Expand All @@ -345,16 +345,16 @@ def validate_attribute(attribute)
self.class.settable_attributes.include?(attribute)
end

def file_upload_hash(**params)
def file_upload_array(**params)
permited_keys_for_file_upload = %i[file msg description tmid]
hash = Util.slice_hash(params, *permited_keys_for_file_upload)
hash = Util.slice_hash(params, *permited_keys_for_file_upload).compact

# NOTE: https://www.rubydoc.info/github/ruby/ruby/Net/HTTPHeader:set_form
file_options = params.slice(:filename, :content_type).compact
hash.map do |key, value|
next [key, value, file_options] if key == :file && file_options.keys.any?
next [key.to_s, value, file_options] if key == :file && file_options.keys.any?

[key, value]
[key.to_s, value]
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rocket_chat/request_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def create_request(path, options)
req = Net::HTTP::Post.new(path, headers)
add_body(req, body) if body

form_data = reject_nils(options[:form_data])
form_data = options[:form_data]
add_form_data(req, form_data) if form_data
else
uri = path
Expand All @@ -131,7 +131,6 @@ def add_body(request, body)
end

def add_form_data(request, form_data)
form_data = form_data.transform_keys(&:to_s) if form_data.is_a? Hash
request.set_form(form_data, 'multipart/form-data')
end

Expand Down

0 comments on commit 8a91e56

Please sign in to comment.