bricooke / harvest-ruby

Ruby wrapper around the Harvest API (http://www.getharvest.com/api)

This URL has Read+Write access

harvest-ruby / lib / harvest_entry.rb
100644 47 lines (41 sloc) 1.024 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
class HarvestEntry
  def initialize(parsed)
    @hash = parsed
  end
  
  def id
    self.method_missing(:id)
  end
  
  def empty?
    if @hash["type"] == "array" && @hash["content"] == "\n"
      true
    else
      false
    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
    
    if @hash.has_key?(method.to_s.singularize)
      entry = @hash[method.to_s.singularize]
      if method.to_s.pluralize == method.to_s && entry.class == Array
        return entry.collect {|e| HarvestEntry.new(e)}
      else
        return entry[0] unless entry[0].class == Hash && entry[0].has_key?("content")
        return entry[0]["content"]
      end
    elsif @hash.has_key?(method.to_s)
      entry = @hash[method.to_s]
      return entry[0] unless entry[0].class == Hash && entry[0].has_key?("content")
      return entry[0]["content"]
    else
      super
    end
  end
end