Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Merge pull request #94 from hyperoslo/master
Browse files Browse the repository at this point in the history
Make expenses attachments work
  • Loading branch information
zmoazeni committed Apr 21, 2017
2 parents 08b9f77 + e0839dd commit 9669df8
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/harvest/api/expenses.rb
Expand Up @@ -2,26 +2,32 @@ module Harvest
module API
class Expenses < Base
api_model Harvest::Expense

include Harvest::Behavior::Crud

def all(date = ::Time.now, user = nil)
date = ::Time.parse(date) if String === date
response = request(:get, credentials, "#{api_model.api_path}/#{date.yday}/#{date.year}", :query => of_user_query(user))
api_model.parse(response.parsed_response)
end

# This is currently broken, but will come back to it

def attach(expense, filename, receipt)
body = ""
body << "------------------------------b7edea381b46\r\n"
body << "--__X_ATTACH_BOUNDARY__\r\n"
body << %Q{Content-Disposition: form-data; name="expense[receipt]"; filename="#{filename}"\r\n}
body << "Content-Type: image/png\r\n"
body << "\r\n#{receipt.read}\r\n"
body << "------------------------------b7edea381b46\r\n"

request(:post, credentials, "#{api_model.api_path}/#{expense.to_i}/receipt", :headers => {'Content-Type' => 'multipart/form-data; boundary=------------------------------b7edea381b46'}, :body => body)
body << "\r\n#{receipt.read}"
body << "\r\n--__X_ATTACH_BOUNDARY__--\r\n\r\n"

request(
:post,
credentials,
"#{api_model.api_path}/#{expense.to_i}/receipt",
:headers => {
'Content-Type' => 'multipart/form-data; charset=utf-8; boundary=__X_ATTACH_BOUNDARY__',
'Content-Length' => body.length.to_s,
},
:body => body)
end
end
end
end
end

0 comments on commit 9669df8

Please sign in to comment.