0
@@ -5,7 +5,9 @@ require 'activesupport'
0
require File.dirname(__FILE__) + '/harvest_entry'
0
def initialize(domain, email, password)
0
@@ -13,33 +15,64 @@ class Harvest
0
+ # returns all the users created in this Harvest account
0
+ # see http://www.getharvest.com/api/people for data available
0
request("/people", params).users
0
+ # returns all the projects created in this Harvest account
0
+ # see http://www.getharvest.com/api/projects for data available
0
request("/projects").projects
0
+ # returns all tasks and expenses for a given project or person
0
+ # in the given time period. The expenses and entries are
0
+ # mixed in the same array returned. Use .expense? to determine
0
+ # what type it is. Reference http://www.getharvest.com/api/reporting
0
+ # to get data available for each type
0
def report(from, to, project_and_or_person_id)
0
- if project_and_or_person_id.has_key?(:project_id)
0
- 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
0
- if project_and_or_person_id.has_key?(:person_id)
0
- 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
0
+ %w(entries expenses).each do |type|
0
+ if project_and_or_person_id.has_key?(:project_id)
0
+ t = request("/projects/#{project_and_or_person_id[:project_id]}/#{type}?from=#{from.strftime("%Y%m%d")}&to=#{to.strftime("%Y%m%d")}")
0
+ temp = (type == "entries" ? t.day_entries : t.expenses)
0
+ entries += temp.select {|entry| !(entries.any? {|match| match.id == entry.id })}
0
+ if project_and_or_person_id.has_key?(:person_id)
0
+ t = request("/people/#{project_and_or_person_id[:person_id]}/#{type}?from=#{from.strftime("%Y%m%d")}&to=#{to.strftime("%Y%m%d")}")
0
+ temp = (type == "entries" ? t.day_entries : t.expenses)
0
+ entries += temp.select {|entry| !(entries.any? {|match| match.id == entry.id })}
0
- def tasks(task_id=nil)
0
- request("/tasks").tasks
0
- request("/tasks/#{task_id}")
0
+ # returns all tasks and caches the results
0
+ # see http://www.getharvest.com/api/tasks
0
+ @tasks ||= request("/tasks").tasks
0
+ # return a specific task
0
+ # see http://www.getharvest.com/api/tasks
0
+ self.tasks.find {|t| t.id.to_i == id.to_i}
0
+ # returns all expense categories and caches the results
0
+ # see http://www.getharvest.com/api/expenses
0
+ def expense_categories
0
+ @expense_categories ||= request("/expense_categories").expense_categories
0
+ # returns a specific expense_category
0
+ # see http://www.getharvest.com/api/expenses
0
+ def expense_category(id)
0
+ self.expense_categories.find {|e| e.id.to_i == id.to_i}
0
@@ -50,6 +83,9 @@ private
0
Net::HTTP.new(@domain).start {|http|
0
response = http.request(request)
0
+ if response.class == Net::HTTPServiceUnavailable
0
+ raise ArgumentError, "API Limit exceeded. Retry after #{response["Retry-After"].to_i/60} minutes."
0
ret = HarvestEntry.new(XmlSimple.xml_in(response.body))
Comments
No one has commented yet.