public
Description: Ruby wrapper around the Harvest API (http://www.getharvest.com/api)
Clone URL: git://github.com/bricooke/harvest-ruby.git
bricooke (author)
Mon Apr 14 12:33:04 -0700 2008
commit  5a5ef6d1f57704f4dec654dd2555c4e9ddf1cbd9
tree    54af3bb24a02b14d2a94e8d1b6f046534781a143
harvest-ruby / lib / harvest.rb
100644 57 lines (48 sloc) 1.459 kb
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'rubygems'
require 'net/http'
require 'xmlsimple'
require 'activesupport'
require File.dirname(__FILE__) + '/harvest_entry'
 
class Harvest
  VERSION = '0.1.0'
  
  def initialize(domain, email, password)
    @domain = domain
    @email = email
    @password = password
  end
 
  def users(params={})
    request("/people", params).users
  end
 
  def projects
    request("/projects").projects
  end
 
  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
    
    entries
  end
  
  def tasks(task_id=nil)
    if task_id.nil?
      request("/tasks").tasks
    else
      request("/tasks/#{task_id}")
    end
  end
 
private
  def request(path, params={})
    request = Net::HTTP::Get.new(path, { "Accept" => "application/xml"})
    request.basic_auth(@email, @password)
    request.content_type = "application/xml"
    ret = nil
    Net::HTTP.new(@domain).start {|http|
      response = http.request(request)
      ret = HarvestEntry.new(XmlSimple.xml_in(response.body))
    }
    ret
  end
end