From e6925a087a2cb6829ac3b77eb1885ea09a1a522d Mon Sep 17 00:00:00 2001 From: Marian Theisen Date: Tue, 4 Feb 2020 12:14:29 +0100 Subject: [PATCH 1/2] add new concern for multipart file upload --- Gemfile | 3 ++ lib/pipedrive.rb | 1 + lib/pipedrive/file.rb | 1 + lib/pipedrive/operations/upload.rb | 36 ++++++++++++++++++++ pipedrive.gemspec | 4 +-- spec/lib/pipedrive/operations/upload_spec.rb | 25 ++++++++++++++ 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 lib/pipedrive/operations/upload.rb create mode 100644 spec/lib/pipedrive/operations/upload_spec.rb diff --git a/Gemfile b/Gemfile index e38a343..2224036 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,9 @@ source 'https://rubygems.org' # Specify your gem's dependencies in pipedrive.gemspec gemspec +# TODO: remove after release of new faraday middleware +gem 'faraday_middleware', git: 'https://github.com/lostisland/faraday_middleware.git', branch: 'e169ab28a3f1fc6cc3160f86873903c7e5e8b882' + group :test do gem 'simplecov', :require => false gem 'coveralls', :require => false diff --git a/lib/pipedrive.rb b/lib/pipedrive.rb index 96a0c46..ea3d383 100644 --- a/lib/pipedrive.rb +++ b/lib/pipedrive.rb @@ -48,6 +48,7 @@ def logger require 'pipedrive/operations/read' require 'pipedrive/operations/update' require 'pipedrive/operations/delete' +require 'pipedrive/operations/upload' # Persons require 'pipedrive/person_field' diff --git a/lib/pipedrive/file.rb b/lib/pipedrive/file.rb index 38f53f2..d01eb9a 100644 --- a/lib/pipedrive/file.rb +++ b/lib/pipedrive/file.rb @@ -4,5 +4,6 @@ class File < Base include ::Pipedrive::Operations::Read include ::Pipedrive::Operations::Update include ::Pipedrive::Operations::Delete + include ::Pipedrive::Operations::Upload end end diff --git a/lib/pipedrive/operations/upload.rb b/lib/pipedrive/operations/upload.rb new file mode 100644 index 0000000..778b835 --- /dev/null +++ b/lib/pipedrive/operations/upload.rb @@ -0,0 +1,36 @@ +require 'open-uri' + +module Pipedrive + module Operations + module Upload + extend ActiveSupport::Concern + + def upload(file, mime_type, params = {}) + open(file) do |f| + params = params.each_with_object({}) do |(key, val), h| + h[key] = Faraday::ParamPart.new(val, nil, key) + end + params[:file] = Faraday::UploadIO.new(f, mime_type) + + url = build_url([]) + response = self.class.file_upload_connection.post(url, params) + process_response(response) + end + end + + class_methods do + def file_upload_connection + @file_upload_connection ||= Faraday.new(self.faraday_options) do |conn| + conn.request :multipart + conn.request :url_encoded + conn.response :mashify + conn.response :json, content_type: /\bjson$/ + conn.use FaradayMiddleware::ParseJson + conn.response :logger, ::Pipedrive.logger if ::Pipedrive.debug + conn.adapter Faraday.default_adapter + end + end + end + end + end +end diff --git a/pipedrive.gemspec b/pipedrive.gemspec index 84bfe2f..c72a254 100644 --- a/pipedrive.gemspec +++ b/pipedrive.gemspec @@ -19,8 +19,8 @@ Gem::Specification.new do |gem| gem.require_paths = ['lib'] gem.add_dependency('activesupport', '>= 4.0.0') - gem.add_dependency('faraday') - gem.add_dependency('faraday_middleware') + gem.add_dependency('faraday', '>= 1.0') + gem.add_dependency('faraday_middleware', '>= 1.0') gem.add_dependency('hashie', '>= 3.0') gem.add_development_dependency('bundler') gem.add_development_dependency('rake', '< 12') diff --git a/spec/lib/pipedrive/operations/upload_spec.rb b/spec/lib/pipedrive/operations/upload_spec.rb new file mode 100644 index 0000000..a4b46a2 --- /dev/null +++ b/spec/lib/pipedrive/operations/upload_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +RSpec.describe ::Pipedrive::Operations::Upload do + subject do + Class.new(::Pipedrive::Base) do + include ::Pipedrive::Operations::Upload + + def entity_name + 'bases' + end + end.new('token') + end + + context '#upload' do + it 'should make a multipart api call' do + stub_request(:post, 'https://api.pipedrive.com/v1/bases?api_token=token').to_return(:status => 200, :body => {}.to_json, :headers => {}) + expect_any_instance_of(::Faraday::Connection).to(receive(:post).with('/v1/bases?api_token=token', hash_including( + file: an_instance_of(Faraday::UploadIO), + deal_id: an_instance_of(Faraday::ParamPart) + ))).and_call_original + + subject.upload('./Gemfile', 'text/plain', deal_id: 123) + end + end +end From 4aceba8dcaac89d525f6db318ede8db60ed59fd9 Mon Sep 17 00:00:00 2001 From: Marian Theisen Date: Mon, 10 Feb 2020 11:29:24 +0100 Subject: [PATCH 2/2] add explicit filename --- lib/pipedrive/operations/upload.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pipedrive/operations/upload.rb b/lib/pipedrive/operations/upload.rb index 778b835..e6b8f9f 100644 --- a/lib/pipedrive/operations/upload.rb +++ b/lib/pipedrive/operations/upload.rb @@ -6,11 +6,12 @@ module Upload extend ActiveSupport::Concern def upload(file, mime_type, params = {}) + filename = ::File.basename(file) open(file) do |f| params = params.each_with_object({}) do |(key, val), h| h[key] = Faraday::ParamPart.new(val, nil, key) end - params[:file] = Faraday::UploadIO.new(f, mime_type) + params[:file] = Faraday::UploadIO.new(f, mime_type, filename) url = build_url([]) response = self.class.file_upload_connection.post(url, params)