Class: Harvest::Resources::Timer
- Inherits:
-
HarvestResource
- Object
- ActiveResource::Base
- HarvestResource
- Harvest::Resources::Timer
- 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
-
.delete_with_action_hack(id, options = {}) ⇒ Object
Override to use delete path.
-
.element_path_with_extension_removal(id, prefix_options = {}, query_options = nil) ⇒ Object
Override to remove file extension.
-
.find_single_with_action_hack(scope, options) ⇒ Object
Override to use show path.
Instance Method Summary collapse
-
#create ⇒ Object
Override to not use collection_path.
-
#destroy ⇒ Object
Override to use delete path.
-
#encode(options = {}) ⇒ Object
Override to support Harvest’s custom XML format.
- #find_project ⇒ Object
- #find_task ⇒ Object
-
#initialize(attributes = {}) ⇒ Timer
constructor
Override to pull day_entry element data into attributes.
- #project ⇒ Object
- #task ⇒ Object
-
#update ⇒ Object
Override to use POST instead of PUT, and update path.
Methods included from Plugins::ActiveResourceInheritableHeaders
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, ={} delete_without_action_hack id, .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, = {}, = nil element_path_without_extension_removal(id, , ).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, ||= {} and [:params] ||= {} [:params].merge!(:action_hack => :show) find_single_without_action_hack scope, end |
Instance Method Details
#create ⇒ Object
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 |
#destroy ⇒ Object
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 ={} 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()) end |
#find_project ⇒ Object
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_task ⇒ Object
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 |
#project ⇒ Object
51 52 53 |
# File 'lib/harvest/resources/timer.rb', line 51 def project @project ||= find_project end |
#task ⇒ Object
62 63 64 |
# File 'lib/harvest/resources/timer.rb', line 62 def task @task ||= find_task end |
#update ⇒ Object
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(.merge(:action_hack => :update)), encode, headers).tap do |response| load_attributes_from_response(response) end end |