costagent 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +13 -1
  2. data/lib/costagent.rb +27 -13
  3. metadata +3 -3
data/README.rdoc CHANGED
@@ -20,10 +20,14 @@ costagent = CostAgent.new subdomain, username, password
20
20
 
21
21
  (remember you need to enable API access from within your FreeAgent account)
22
22
 
23
- To see all projects:
23
+ To see all active projects:
24
24
 
25
25
  projects = costagent.projects
26
26
 
27
+ To see all projects:
28
+
29
+ projects = costagent.projects("all")
30
+
27
31
  To find a specific project:
28
32
 
29
33
  project = costagent.project(id)
@@ -32,6 +36,14 @@ To find timeslips:
32
36
 
33
37
  timeslips = costagent.timeslips(start_date, end_date)
34
38
 
39
+ To return all tasks for a specific project:
40
+
41
+ tasks = costagent.tasks(project_id)
42
+
43
+ To return your FA user ID:
44
+
45
+ costagent.user_id
46
+
35
47
  And to return a GBP figure of the money earnt during a specific timeframe based on billable hours:
36
48
 
37
49
  total = costagent.earnt(start_date, end_date)
data/lib/costagent.rb CHANGED
@@ -23,13 +23,13 @@ class CostAgent
23
23
  end
24
24
 
25
25
  # Returns all projects
26
- def projects
27
- @projects ||= (self.api("projects")/"project").collect { |project| Project.new((project/"id").text.to_i, (project/"name").text, (project/"currency").text, (project/"normal-billing-rate").text.to_f) }
26
+ def projects(filter = "active")
27
+ @projects ||= (self.api("projects", {:view => filter})/"project").collect { |project| Project.new((project/"id").text.to_i, (project/"name").text, (project/"currency").text, (project/"normal-billing-rate").text.to_f) }
28
28
  end
29
29
 
30
30
  # This returns the specified project
31
31
  def project(id)
32
- self.projects.detect { |p| p.id == id }
32
+ self.projects("all").detect { |p| p.id == id }
33
33
  end
34
34
 
35
35
  # This returns all timeslips for the specified date range, with additional cost information
@@ -37,14 +37,18 @@ class CostAgent
37
37
  (self.api("timeslips", :view => "#{start_date.strftime("%Y-%m-%d")}_#{end_date.strftime("%Y-%m-%d")}")/"timeslip").collect do |timeslip|
38
38
  # Find the project and hours for this timeslip
39
39
  project = self.project((timeslip/"project-id").text.to_i)
40
- hours = (timeslip/"hours").text.to_f
41
- # Build the timeslip out using the timeslip data and the project it's tied to
42
- Timeslip.new((timeslip/"id").text.to_i,
43
- project,
44
- hours,
45
- DateTime.parse((timeslip/"updated-at").text),
46
- project.hourly_billing_rate * hours)
47
- end
40
+ if project
41
+ hours = (timeslip/"hours").text.to_f
42
+ # Build the timeslip out using the timeslip data and the project it's tied to
43
+ Timeslip.new((timeslip/"id").text.to_i,
44
+ project,
45
+ hours,
46
+ DateTime.parse((timeslip/"updated-at").text),
47
+ project.hourly_billing_rate * hours)
48
+ else
49
+ nil
50
+ end
51
+ end - [nil]
48
52
  end
49
53
 
50
54
  # This returns all tasks for the specified project_id
@@ -58,6 +62,11 @@ class CostAgent
58
62
  project)
59
63
  end
60
64
  end
65
+
66
+ # This looks up the user ID using the CostAgent credentials
67
+ def user_id
68
+ self.client("verify").get.headers[:user_id]
69
+ end
61
70
 
62
71
  # This returns the amount of GBP earnt in the specified timeframe
63
72
  def earnt(start_date = DateTime.now, end_date = start_date)
@@ -72,12 +81,17 @@ class CostAgent
72
81
 
73
82
  # This calls the FA API for the specified resource
74
83
  def api(resource, parameters = {})
75
- url = "https://#{self.subdomain}.freeagentcentral.com/#{resource}#{parameters.empty? ? "" : "?" + parameters.collect { |p| p.join("=") }.join("&")}"
76
- res = RestClient::Resource.new(url, self.username, self.password).get
84
+ res = self.client(resource, parameters).get
77
85
  raise "No response from #{url}!" if res.body.nil? && res.body.empty?
78
86
  Hpricot(res.body)
79
87
  end
80
88
 
89
+ # This returns a client ready to query the FA API
90
+ def client(resource, parameters = {})
91
+ url = "https://#{self.subdomain}.freeagentcentral.com/#{resource}#{parameters.empty? ? "" : "?" + parameters.collect { |p| p.join("=") }.join("&")}"
92
+ RestClient::Resource.new(url, self.username, self.password)
93
+ end
94
+
81
95
  class << self
82
96
  # This returns the current USD rate from xe.com (or falls back on 1.6 if there is an error)
83
97
  def usd_rate
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Elliott Draper
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-12 00:00:00 +01:00
17
+ date: 2010-06-02 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency