From ac34330bca2cd1ffb80fe01999c25f91814ac27a Mon Sep 17 00:00:00 2001 From: Alex Neigher Date: Thu, 15 Mar 2018 08:48:26 -0700 Subject: [PATCH] adding file upload functionality --- lib/hubspot-ruby.rb | 1 + lib/hubspot/connection.rb | 9 +++++++ lib/hubspot/file.rb | 50 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 lib/hubspot/file.rb diff --git a/lib/hubspot-ruby.rb b/lib/hubspot-ruby.rb index ebae0ad6..1dd989cf 100644 --- a/lib/hubspot-ruby.rb +++ b/lib/hubspot-ruby.rb @@ -19,6 +19,7 @@ require 'hubspot/owner' require 'hubspot/engagement' require 'hubspot/subscription' +require 'hubspot/file' module Hubspot def self.configure(config={}) diff --git a/lib/hubspot/connection.rb b/lib/hubspot/connection.rb index 7c87e2dc..e04093fc 100644 --- a/lib/hubspot/connection.rb +++ b/lib/hubspot/connection.rb @@ -22,6 +22,15 @@ def post_json(path, opts) no_parse ? response : response.parsed_response end + def post_multipart(path, opts) + url = generate_url(path, opts[:params]) + response = post(url, body: opts[:body], headers: { 'Content-Type' => '"multipart/form-data"' }, format: :multipart) + log_request_and_response url, response, opts[:body] + raise(Hubspot::RequestError.new(response)) unless response.success? + + JSON.parse(response.body) + end + def put_json(path, opts) url = generate_url(path, opts[:params]) response = put(url, body: opts[:body].to_json, headers: { 'Content-Type' => 'application/json' }, format: :json) diff --git a/lib/hubspot/file.rb b/lib/hubspot/file.rb new file mode 100644 index 00000000..bb5159ec --- /dev/null +++ b/lib/hubspot/file.rb @@ -0,0 +1,50 @@ +require 'hubspot/utils' +require 'base64' +require 'pp' + +module Hubspot + # + # HubSpot Files API + # + # {https://developers.hubspot.com/docs/methods/files/post_files} + # + class File + GET_FILE_PATH = "/filemanager/api/v2/files/:file_id" + DELETE_FILE_PATH = "/filemanager/api/v2/files/:file_id/full-delete" + LIST_FILE_PATH = "/filemanager/api/v2/files" + + attr_reader :id + attr_reader :properties + + def initialize(response_hash) + @id = response_hash["id"] + @properties = response_hash + end + + class << self + def find_by_id(file_id) + response = Hubspot::Connection.get_json(GET_FILE_PATH, { file_id: file_id }) + new(response) + end + + # {https://developers.hubspot.com/docs/methods/files/post_files} + def create!(local_path, hubspot_folder_paths = '') + files = {'files': open(local_path)} + response = Hubspot::Connection.post_multipart( + LIST_FILE_PATH, + params: {}, + body: { data: { "folder_paths": hubspot_folder_paths }, files: files } + ) + new(response) + end + + end + + # Permanently delete a file and all related data and thumbnails from file manager. + # {https://developers.hubspot.com/docs/methods/files/hard_delete_file_and_associated_objects} + def destroy! + Hubspot::Connection.post_json(DELETE_FILE_PATH, params: {file_id: id}) + end + + end +end \ No newline at end of file