-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathupload.rb
37 lines (33 loc) · 1.11 KB
/
upload.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'open-uri'
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