Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.1.1. minimal documentation. support for expense_categories. fix dup…

…licates reported in report. support getting the Retry-After
  • Loading branch information...
commit afcce7c82561f7c1d9c385772aa2e11bea720259 1 parent 5a5ef6d
Brian Cooke authored
Showing with 58 additions and 13 deletions.
  1. +49 −13 lib/harvest.rb
  2. +9 −0 lib/harvest_entry.rb
View
62 lib/harvest.rb
@@ -5,7 +5,9 @@
require File.dirname(__FILE__) + '/harvest_entry'
class Harvest
- VERSION = '0.1.0'
+ VERSION = '0.1.1'
+ @tasks = []
+ @expenses = []
def initialize(domain, email, password)
@domain = domain
@@ -13,33 +15,64 @@ def initialize(domain, email, password)
@password = password
end
+ # returns all the users created in this Harvest account
+ # see http://www.getharvest.com/api/people for data available
def users(params={})
request("/people", params).users
end
+ # returns all the projects created in this Harvest account
+ # see http://www.getharvest.com/api/projects for data available
def projects
request("/projects").projects
end
+ # returns all tasks and expenses for a given project or person
+ # in the given time period. The expenses and entries are
+ # mixed in the same array returned. Use .expense? to determine
+ # what type it is. Reference http://www.getharvest.com/api/reporting
+ # to get data available for each type
def report(from, to, project_and_or_person_id)
entries = []
- if project_and_or_person_id.has_key?(:project_id)
- entries += request("/projects/#{project_and_or_person_id[:project_id]}/entries?from=#{from.strftime("%Y%m%d")}&to=#{to.strftime("%Y%m%d")}").day_entries
- end
- if project_and_or_person_id.has_key?(:person_id)
- entries += request("/people/#{project_and_or_person_id[:person_id]}/entries?from=#{from.strftime("%Y%m%d")}&to=#{to.strftime("%Y%m%d")}").day_entries
- end
+ %w(entries expenses).each do |type|
+ if project_and_or_person_id.has_key?(:project_id)
+ t = request("/projects/#{project_and_or_person_id[:project_id]}/#{type}?from=#{from.strftime("%Y%m%d")}&to=#{to.strftime("%Y%m%d")}")
+ temp = (type == "entries" ? t.day_entries : t.expenses)
+ entries += temp.select {|entry| !(entries.any? {|match| match.id == entry.id })}
+ end
+ if project_and_or_person_id.has_key?(:person_id)
+ t = request("/people/#{project_and_or_person_id[:person_id]}/#{type}?from=#{from.strftime("%Y%m%d")}&to=#{to.strftime("%Y%m%d")}")
+ temp = (type == "entries" ? t.day_entries : t.expenses)
+ entries += temp.select {|entry| !(entries.any? {|match| match.id == entry.id })}
+ end
+ end
entries
end
- def tasks(task_id=nil)
- if task_id.nil?
- request("/tasks").tasks
- else
- request("/tasks/#{task_id}")
- end
+ # returns all tasks and caches the results
+ # see http://www.getharvest.com/api/tasks
+ def tasks
+ @tasks ||= request("/tasks").tasks
+ end
+
+ # return a specific task
+ # see http://www.getharvest.com/api/tasks
+ def task(id)
+ self.tasks.find {|t| t.id.to_i == id.to_i}
+ end
+
+ # returns all expense categories and caches the results
+ # see http://www.getharvest.com/api/expenses
+ def expense_categories
+ @expense_categories ||= request("/expense_categories").expense_categories
+ end
+
+ # returns a specific expense_category
+ # see http://www.getharvest.com/api/expenses
+ def expense_category(id)
+ self.expense_categories.find {|e| e.id.to_i == id.to_i}
end
private
@@ -50,6 +83,9 @@ def request(path, params={})
ret = nil
Net::HTTP.new(@domain).start {|http|
response = http.request(request)
+ if response.class == Net::HTTPServiceUnavailable
+ raise ArgumentError, "API Limit exceeded. Retry after #{response["Retry-After"].to_i/60} minutes."
+ end
ret = HarvestEntry.new(XmlSimple.xml_in(response.body))
}
ret
View
9 lib/harvest_entry.rb
@@ -15,6 +15,15 @@ def empty?
end
end
+ def expense?
+ begin
+ self.expense_category_id
+ true
+ rescue
+ false
+ end
+ end
+
def method_missing(method, *args)
method = method.to_s.gsub("_", "-").to_sym
Please sign in to comment.
Something went wrong with that request. Please try again.