Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support for file upload #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/pipedrive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions lib/pipedrive/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions lib/pipedrive/operations/upload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'open-uri'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required? There is possibility to use a url as file. Not sure if this secure enough.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it would be better if #upload expects a file/IO object, instead of URL/filename?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea!


module Pipedrive
module Operations
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, filename)

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
4 changes: 2 additions & 2 deletions pipedrive.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/pipedrive/operations/upload_spec.rb
Original file line number Diff line number Diff line change
@@ -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