Skip to content

Commit 91c6668

Browse files
committed
Merge branch 'master' into create-bank-account-resource
2 parents 4706b65 + c8736b4 commit 91c6668

File tree

11 files changed

+274
-2
lines changed

11 files changed

+274
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
example.rb
1+
example.rb
2+
resource_list

lib/freeagent.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ module FreeAgent
88
require_relative 'freeagent/timeline_item'
99
require_relative 'freeagent/attachment'
1010

11+
require_relative 'freeagent/contact'
12+
require_relative 'freeagent/project'
13+
require_relative 'freeagent/task'
14+
require_relative 'freeagent/timeslip'
15+
require_relative 'freeagent/note'
16+
require_relative 'freeagent/recurring_invoice'
17+
require_relative 'freeagent/invoice'
18+
1119
class << self
1220
attr_accessor :environment
1321
attr_accessor :debug

lib/freeagent/contact.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module FreeAgent
2+
class Contact < Resource
3+
resource :contact
4+
5+
resource_methods :default
6+
7+
attr_accessor :first_name, :last_name, :contact_name_on_invoice, :country, :locale, :sales_tax_registration_number, :uses_contact_invoice_sequence
8+
9+
attr_accessor :organisation_name, :email, :phone_number
10+
11+
attr_accessor :address1, :town, :region, :postcode, :address2, :address3, :country
12+
13+
date_accessor :created_at, :updated_at
14+
15+
decimal_accessor :account_balance
16+
17+
def self.active
18+
Contact.filter(:view => 'active')
19+
end
20+
21+
def self.clients
22+
Contact.filter(:view => 'clients')
23+
end
24+
25+
def self.suppliers
26+
Contact.filter(:view => 'suppliers')
27+
end
28+
29+
def self.active_projects
30+
Contact.filter(:view => 'active_projects')
31+
end
32+
33+
def self.completed_projects
34+
Contact.filter(:view => 'completed_projects')
35+
end
36+
37+
def self.open_clients
38+
Contact.filter(:view => 'open_clients')
39+
end
40+
41+
def self.open_suppliers
42+
Contact.filter(:view => 'open_suppliers')
43+
end
44+
45+
def self.hidden
46+
Contact.filter(:view => 'hidden')
47+
end
48+
end
49+
end

lib/freeagent/errors.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,20 @@ def inspect
3434
# TODO
3535
end
3636
end
37+
38+
class NotImplemented < StandardError
39+
attr_reader :message
40+
41+
def initialize(message)
42+
@message = message
43+
end
44+
45+
def to_s
46+
message
47+
end
48+
49+
def inspect
50+
"#<#{self.class}: message: #{message} >"
51+
end
52+
end
3753
end

lib/freeagent/invoice.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module FreeAgent
2+
class Invoice < Resource
3+
resource :invoice
4+
5+
resource_methods :default
6+
7+
attr_accessor :contact, :reference, :currency, :status, :omit_header, :payment_terms_in_days, :ec_status, :invoice_items
8+
9+
attr_accessor :project, :discount_percent, :written_off_date
10+
11+
decimal_accessor :exchange_rate, :net_value, :sales_tax_value
12+
13+
date_accessor :dated_on, :due_on
14+
15+
# TODO FIXME Need to rename this better
16+
def self.all_with_nested_items
17+
Invoice.filter(:nested_invoice_items => true)
18+
end
19+
20+
def self.recent_open_or_overdue
21+
Invoice.filter(:view => 'recent_open_or_overdue')
22+
end
23+
24+
def self.open_or_overdue
25+
Invoice.filter(:view => 'recent_open_or_overdue')
26+
end
27+
28+
def self.draft
29+
Invoice.filter(:view => 'recent_open_or_overdue')
30+
end
31+
32+
def self.scheduled_to_email
33+
Invoice.filter(:view => 'recent_open_or_overdue')
34+
end
35+
36+
def self.thank_you_emails
37+
Invoice.filter(:view => 'recent_open_or_overdue')
38+
end
39+
40+
def self.reminder_emails
41+
Invoice.filter(:view => 'recent_open_or_overdue')
42+
end
43+
44+
def self.last_month(n)
45+
Invoice.filter(:view => 'recent_open_or_overdue')
46+
end
47+
48+
def self.find_all_by_contact(contact)
49+
Invoice.filter(:contact => contact)
50+
end
51+
52+
def self.find_all_by_project(project)
53+
Invoice.filter(:project => project)
54+
end
55+
56+
# FIXME Need to figure out the format of the json.
57+
#def send_email(email)
58+
# FreeAgent.client.post("invoices/#{id}/send_email", email)
59+
#end
60+
61+
def mark_as_sent
62+
FreeAgent.client.put("invoices/#{id}/transitions/mark_as_sent", nil)
63+
end
64+
65+
def mark_as_draft
66+
FreeAgent.client.put("invoices/#{id}/transitions/mark_as_draft", nil)
67+
end
68+
69+
def mark_as_cancelled
70+
FreeAgent.client.put("invoices/#{id}/transitions/mark_as_cancelled", nil)
71+
end
72+
73+
# TODO Write invoice timeline wrapper
74+
#def timeline
75+
#
76+
#end
77+
end
78+
end

lib/freeagent/note.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module FreeAgent
2+
class Note < Resource
3+
resource :note
4+
5+
resource_methods :find, :filter, :update, :delete
6+
7+
attr_accessor :note, :parent_url, :author
8+
9+
date_accessor :created_at, :updated_at
10+
11+
# FIXME Need to change POST to allow query strings to be passed to it
12+
def self.create(attributes)
13+
raise FreeAgent::NotImplemented.new("FIXME Need to change POST to allow query strings to be passed to it")
14+
end
15+
16+
def self.find_all_by_contact(contact)
17+
Note.filter(:contact => contact)
18+
end
19+
20+
def self.find_all_by_project(project)
21+
Note.filter(:project => project)
22+
end
23+
end
24+
end

lib/freeagent/project.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module FreeAgent
2+
class Project < Resource
3+
resource :project
4+
5+
resource_methods :default
6+
7+
attr_accessor :name, :contact, :is_ir35, :status, :budget_units, :uses_project_invoice_sequence,
8+
:currency, :billing_period, :contract_po_reference
9+
10+
decimal_accessor :budget, :normal_billing_rate, :hours_per_day
11+
12+
date_accessor :starts_on, :ends_on, :created_at, :updated_at
13+
14+
def self.active
15+
Project.filter(:view => 'active')
16+
end
17+
18+
def self.completed
19+
Project.filter(:view => 'completed')
20+
end
21+
22+
def self.cancelled
23+
Project.filter(:view => 'cancelled')
24+
end
25+
26+
def self.hidden
27+
Project.filter(:view => 'hidden')
28+
end
29+
end
30+
end

lib/freeagent/recurring_invoice.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module FreeAgent
2+
class RecurringInvoice < Resource
3+
resource :recurring_invoice
4+
5+
resource_methods :all, :filter, :find
6+
7+
attr_accessor :contact, :frequency, :recurring_status, :reference, :currency, :omit_header, :payment_terms_in_days, :invoice_items
8+
9+
decimal_accessor :exchange_rate, :net_value, :sales_tax_value, :total_value
10+
11+
date_accessor :dated_on
12+
13+
def self.draft
14+
RecurringInvoice.filter(:view => 'draft')
15+
end
16+
17+
def self.active
18+
RecurringInvoice.filter(:view => 'active')
19+
end
20+
21+
def self.inactive
22+
RecurringInvoice.filter(:view => 'inactive')
23+
end
24+
25+
def self.find_all_by_contact(contact)
26+
RecurringInvoice.filter(:contact => contact)
27+
end
28+
end
29+
end

lib/freeagent/resource.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def self.define_all
113113
def self.define_filter
114114
self.define_singleton_method(:filter) do |params|
115115
response = FreeAgent.client.get("#{endpoint[:plural]}/", params)
116-
response[:endpoint[:plural]].collect{ |r| self.new(r) }
116+
response[endpoint[:plural]].collect{ |r| self.new(r) }
117117
end
118118
end
119119

@@ -123,6 +123,7 @@ def self.define_find
123123
response = FreeAgent.client.get("#{endpoint[:plural]}/#{id}")
124124
self.new(response[endpoint[:single]])
125125
rescue FreeAgent::ApiError => error
126+
raise error if FreeAgent.debug
126127
nil
127128
end
128129
end

lib/freeagent/task.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module FreeAgent
2+
class Task < Resource
3+
resource :task
4+
5+
resource_methods :find, :filter, :update, :delete
6+
7+
attr_accessor :project, :name, :is_billable, :billing_period, :status
8+
9+
decimal_accessor :billing_rate
10+
11+
decimal_accessor :created_at, :updated_at
12+
13+
# FIXME the create action on the Task resource does a POST to
14+
# https://api.freeagent.com/v2/tasks?project=:project which is
15+
# inconsistent with the rest of the API.
16+
def self.create(attributes = {})
17+
raise FreeAgent::NotImplemented.new("Opps creating tasks not implemented - see source for details")
18+
end
19+
20+
def self.find_by_project(project)
21+
Task.filter(:project => project)
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)