Module: Sync

Included in:
HarvestThings::Application
Defined in:
lib/harvestthings/sync.rb

Instance Method Summary collapse

Instance Method Details

#add_client_to_harvest(area_name) ⇒ Integer

add_client_to_harvest - saves a Things area_name as a Harvest client

Parameters:

  • - (str)

    the Things area_name

Returns:

  • (Integer)
    • the new Harvest client id



94
95
96
97
98
99
100
101
102
103
# File 'lib/harvestthings/sync.rb', line 94

def add_client_to_harvest(area_name)
str = <<EOS
<client>
  <name>#{area_name}</name>
  <details></details>
</client> 
EOS
  response = @harvest.request '/clients', :post, str
  define_harvest_clients
end

#add_project_to_harvest(proj_name, client) ⇒ Boolean

add_project_to_harvest - saves a Things project as a Harvest project

Parameters:

  • - (str)

    the name of the Things project

  • - (str)

    the Harvest client id

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/harvestthings/sync.rb', line 38

def add_project_to_harvest(proj_name, client)
str = <<EOS
<project>
   <name>#{proj_name}</name>
   <active type="boolean">true</active>
   <bill-by>none</bill-by>
   <client-id type="integer">#{client}</client-id>
   <code></code>
   <notes></notes>
   <budget type="decimal"></budget>
   <budget-by>none</budget-by>
</project>
EOS
  response = @harvest.request '/projects', :post, str
  # redefine harvest projects, since we've added to it
  define_harvest_projects
end

#add_task_to_harvest(project_name, task_desc) ⇒ String

add_task_to_harvest - saves a Things task as a Harvest task

Parameters:

  • - (str)

    the Thing project

  • - (str)

    the Things task description

Returns:

  • (String)
    • the cleaned string



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/harvestthings/sync.rb', line 61

def add_task_to_harvest(project_name, task_desc)
str = <<EOS
<task>
  <billable-by-default type="boolean">true</billable-by-default>
  <default-hourly-rate type="decimal"></default-hourly-rate>
  <is-default type="boolean">false</is-default>
  <name>#{task_desc}</name>
</task>
EOS
  response = @harvest.request '/tasks', :post, str 
  new_task_location = response['Location']
  new_task_id = new_task_location.gsub(/\/tasks\//, '')
  assign_task_assignment(new_task_id, project_name)
end

#assign_task_assignment(new_task_id, project_name) ⇒ Integer

assign_task_assignment - assigns a newly created task to a Project in Harvest

Parameters:

  • - (Integer)

    the new Harvest task ID

Returns:

  • (Integer)
    • the Harvest project ID



80
81
82
83
84
85
86
87
88
# File 'lib/harvestthings/sync.rb', line 80

def assign_task_assignment(new_task_id, project_name)
str = <<EOS
<task>
  <id type="integer">#{new_task_id}</id>
</task>
EOS
  response = @harvest.request "/projects/#{harvest_project_id(project_name)}/task_assignments", :post, str
  define_harvest_tasks
end

#things_projects_to_harvestArray

things_projects_to_harvest - detemines which Things projects get sent to Harvest

Returns:

  • (Array)
    • array of projects



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/harvestthings/sync.rb', line 6

def things_projects_to_harvest
  define_harvest_projects
  define_harvest_tasks
  define_harvest_clients
  
  @things.projects.each do |project|
    print "."
    name = @things.project_title(project).downcase
    client = @things.project_area(project).downcase
    client_id = harvest_client?(client) ? harvest_client_id(client) : add_client_to_harvest(client)
    add_project_to_harvest(name, client_id) unless harvest_project?(name)
    things_tasks_to_harvest(project)
  end
end

#things_tasks_to_harvest(project) ⇒ Array

things_tasks_to_harvest - determines which Things tasks get sent to Harvest

Returns:

  • (Array)
    • array of tasks



24
25
26
27
28
29
30
31
# File 'lib/harvestthings/sync.rb', line 24

def things_tasks_to_harvest(project)
  @things.tasks(project).each do |task|
    unless @things.task_complete?(task) # complete in Things
      task_desc = @things.task_description(task).downcase
      add_task_to_harvest(@things.project_title(project).downcase, task_desc) unless harvest_task?(task_desc) # unless already exists in Harvest
    end
  end
end