Class: Harvest::Resources::Timer

Inherits:
HarvestResource show all
Defined in:
lib/harvest/resources/timer.rb

Overview

This class is a hack to maintain compatibility with the ActiveResource API and Harvest’s fucked time tracking API.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugins::ActiveResourceInheritableHeaders

included

Constructor Details

#initialize(attributes = {}) ⇒ Timer

Override to pull day_entry element data into attributes



33
34
35
36
37
# File 'lib/harvest/resources/timer.rb', line 33

def initialize attributes={}
  @attributes     = {}
  @prefix_options = {}
  load(attributes.has_key?('day_entry') ? attributes['day_entry'] : attributes)
end

Class Method Details

.delete_with_action_hack(id, options = {}) ⇒ Object

Override to use delete path



89
90
91
# File 'lib/harvest/resources/timer.rb', line 89

def delete_with_action_hack id, options={}
  delete_without_action_hack id, options.merge(:action_hack => :delete)
end

.element_path_with_extension_removal(id, prefix_options = {}, query_options = nil) ⇒ Object

Override to remove file extension



75
76
77
# File 'lib/harvest/resources/timer.rb', line 75

def element_path_with_extension_removal id, prefix_options = {}, query_options = nil
  element_path_without_extension_removal(id, prefix_options, query_options).sub(/\.#{format.extension}/, '')
end

.find_single_with_action_hack(scope, options) ⇒ Object

Override to use show path



81
82
83
84
85
# File 'lib/harvest/resources/timer.rb', line 81

def find_single_with_action_hack scope, options
  options ||= {} and options[:params] ||= {}
  options[:params].merge!(:action_hack => :show)
  find_single_without_action_hack scope, options
end

Instance Method Details

#createObject

Override to not use collection_path



11
12
13
14
15
16
17
# File 'lib/harvest/resources/timer.rb', line 11

def create
  headers = self.class.headers.merge 'Accept' => 'application/xml'
  connection.post('/daily/add', encode, headers).tap do |response|
    self.id = id_from_response(response)
    load_attributes_from_response(response)
  end
end

#destroyObject

Override to use delete path



28
29
30
# File 'lib/harvest/resources/timer.rb', line 28

def destroy
  connection.delete(element_path(:action_hack => :delete), self.class.headers)
end

#encode(options = {}) ⇒ Object

Override to support Harvest’s custom XML format



40
41
42
43
44
45
46
47
48
49
# File 'lib/harvest/resources/timer.rb', line 40

def encode options={}
  massaged_attributes = {
    :notes => attributes['notes'],
    :hours => attributes['hours'].to_s,
    :project_id => (project ? project.id : nil),
    :task_id => (task ? task.id : nil),
    :spent_at => attributes['spent_at'] || Date.today
  }
  self.class.format.encode(massaged_attributes, {:root => 'request'}.merge(options))
end

#find_projectObject



55
56
57
58
59
60
# File 'lib/harvest/resources/timer.rb', line 55

def find_project
  case p = attributes['project']
  when Integer then Harvest::Resources::Project.find(p)
  else; Harvest::Resources::Project.find(:all).find{|pp|pp.name.strip == p.to_s.strip}
  end
end

#find_taskObject



66
67
68
69
70
71
# File 'lib/harvest/resources/timer.rb', line 66

def find_task
  case t = attributes['task']
  when Integer then Harvest::Resources::Task.find(t)
  else; Harvest::Resources::Task.find(:all).find{|tt|tt.name.strip == t.to_s.strip}
  end
end

#projectObject



51
52
53
# File 'lib/harvest/resources/timer.rb', line 51

def project
  @project ||= find_project
end

#taskObject



62
63
64
# File 'lib/harvest/resources/timer.rb', line 62

def task
  @task ||= find_task
end

#updateObject

Override to use POST instead of PUT, and update path



20
21
22
23
24
25
# File 'lib/harvest/resources/timer.rb', line 20

def update
  headers = self.class.headers.merge 'Accept' => 'application/xml'
  connection.post(element_path(prefix_options.merge(:action_hack => :update)), encode, headers).tap do |response|
    load_attributes_from_response(response)
  end
end