Permalink
Browse files

Created ActiveResource branch and created first tests for AR branch

  • Loading branch information...
1 parent c520687 commit 711f7f500c327969c43244966ea0a38571445018 @aaronrussell committed Oct 20, 2009
View
@@ -1,46 +0,0 @@
-module Freeagent
-
- class Base
-
- def initialize(attributes={})
- attributes.each do |key, value|
- raise "no attr_accessor set for #{key} on #{self.class}" if !respond_to?("#{key}=")
- self.send("#{key}=", value)
- end
- end
-
- def self.get(path)
- @@resp = APICache.get(path) do
- http = Net::HTTP.new(Freeagent.domain, 443)
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- http.start do |http|
- request = Net::HTTP::Get.new(path, {'Content-Type' => 'application/xml', 'Accept' => 'application/xml'})
- request.basic_auth(Freeagent.username, Freeagent.password)
- response = http.request(request)
- case response
- when Net::HTTPSuccess
- @@resp = response.body
- else
- response.error!
- raise APICache::InvalidResponse
- end
- end
- end
- end
-
- def self.parse(path, options)
- response = []
- Nokogiri::XML(@@resp).xpath(path).each do |ts|
- res = {}
- options.each do |key|
- res[key.underscore.to_sym] = ts.xpath(key).text
- end
- response << self.new(res)
- end
- return response
- end
-
- end
-
-end
@@ -1,20 +0,0 @@
-module Freeagent
-
- class Contact < Base
- @elements = ['id', 'organisation-name', 'first-name', 'last-name', 'address1', 'address2', 'address3', 'town', 'region', 'country', 'postcode', 'phone-number', 'email', 'contact-name-on-invoices', 'sales-tax-registration_number', 'uses-contact-invoice-sequence']
- @elements.each {|t| attr_accessor t.underscore.to_sym}
-
- def self.find_all
- get '/contacts'
- contacts = parse('contacts/contact', @elements)
- return contacts
- end
-
- def self.find(contact_id)
- get '/contacts/'+contact_id
- contacts = parse('contact', @elements)
- return contacts[0]
- end
- end
-
-end
@@ -1,27 +0,0 @@
-module Freeagent
-
- class Invoice < Base
- @elements = ['id', 'contact-id', 'project-id', 'dated-on', 'due-on', 'reference', 'net-value', 'sales-tax-value', 'status', 'comments', 'discount-percent', 'omit-header', 'payment-terms', 'written-off-date', 'invoice-items']
- @elements.each {|t| attr_accessor t.underscore.to_sym}
-
- def self.find_all(project_id = false)
- if project_id
- get '/projects/'+project_id+'/invoices'
- else
- get '/invoices'
- end
- invoices = parse('invoices/invoice', @elements)
- return invoices.reverse
- end
-
- def self.find(invoice_id)
- get '/invoices/'+invoice_id
- invoices = parse('invoice', @elements)
- invoices.each do |i|
- i.invoice_items = InvoiceItem.find_all(invoice_id)
- end
- return invoices[0]
- end
- end
-
-end
@@ -1,13 +0,0 @@
-module Freeagent
-
- class InvoiceItem < Invoice
- @elements = ['id', 'invoice-id', 'project-id', 'item-type', 'price', 'quantity', 'description', 'sales-tax-rate']
- @elements.each {|t| attr_accessor t.underscore.to_sym}
-
- def self.find_all(invoice_id)
- items = parse('invoice/invoice-items/invoice-item', @elements)
- return items
- end
- end
-
-end
@@ -1,20 +0,0 @@
-module Freeagent
-
- class Project < Base
- @elements = ['id', 'contact-id', 'name', 'billing-basis', 'budget', 'budget-units', 'invoicing-reference', 'is-ir35', 'normal-billing-rate', 'payment-terms-in-days', 'starts-on', 'ends-on', 'status', 'uses-project-invoice-sequence']
- @elements.each {|t| attr_accessor t.underscore.to_sym}
-
- def self.find_all
- get '/projects'
- projects = parse('projects/project', @elements)
- return projects
- end
-
- def self.find(project_id)
- get '/projects/'+project_id
- projects = parse('project', @elements)
- return projects[0]
- end
- end
-
-end
View
@@ -1,14 +0,0 @@
-module Freeagent
-
- class Task < Base
- @elements = ['id', 'project-id', 'name']
- @elements.each {|t| attr_accessor t.underscore.to_sym}
-
- def self.find(project_id, task_id)
- get '/projects/'+project_id+'/tasks/'+task_id
- tasks = parse('task', @elements)
- return tasks[0]
- end
- end
-
-end
@@ -1,27 +0,0 @@
-module Freeagent
-
- class Timeslip < Base
- @elements = ['id', 'dated-on', 'project-id', 'task-id', 'task', 'user-id', 'hours', 'comment']
- @elements.each {|t| attr_accessor t.underscore.to_sym}
-
- def self.find_all(project_id = false)
- if project_id
- get '/projects/'+project_id+'/timeslips'
- else
- get '/timeslips'
- end
- timeslips = parse('timeslips/timeslip', @elements)
- timeslips.each do |t|
- t.task = Task.find(project_id, t.task_id)
- end
- return timeslips.reverse
- end
-
- def self.find(timeslip_id)
- get '/timeslips'+timeslip_id
- timeslips = parse('timeslip', @elements)
- return timeslips[0]
- end
- end
-
-end
View
@@ -1,29 +1,190 @@
require 'rubygems'
-require 'nokogiri'
-require 'net/https'
-require 'api_cache'
-require 'activesupport'
require 'activeresource'
-# Require Freeagent library files
-Dir[File.join(File.dirname(__FILE__), "freeagent/*.rb")].each { |f| require f }
-
module Freeagent
class << self
attr_accessor :domain, :username, :password
+ end
+
+ class Error < StandardError; end
- def self.domain=(domain)
- @domain = domain
+ class Base < ActiveResource::Base
+ def self.authenticate
+ self.site = "https://#{Freeagent.domain}"
+ self.user = Freeagent.username
+ self.password = Freeagent.password
end
+ end
+
+ # Find contacts
+ #
+ # Contact.find :all # find all contacts
+ # Contact.find contact_id # find specific contact by ID
+ #
+ # Create contact
+ #
+ # Required attributes
+ # :first_name
+ # :last_name
+ #
+ # contact = Contact.new :first_name => 'Joe', :last_name => 'Bloggs'
+ # contact.save
+ #
+ # Update contact
+ #
+ # contact = Contact.find contact_id
+ # contact.first_name = 'Joe'
+ # contact.last_name = 'Bloggs'
+ # contact.save
+ #
+ # Delete contact
+ #
+ # Contact.delete contact_id
+ # contact.destroy
+ #
+
+ class Contact < Base
+ end
+
+ # Find projects
+ #
+ # Project.find :all # find all projects
+ # Project.find project_id # find specific project by ID
+ #
+ # Create project
+ #
+ # Required attributes
+ # :contact_id
+ # :name
+ # :payment_term_in_days
+ # :billing_basis # must be 1, 7, 7.5, or 8
+ # :budget_units # must be Hours, Days, or Monetary
+ # :status # must be Active or Completed
+ #
+ # Project = Project.new params
+ # contact.save
+ #
+ # Update project
+ #
+ # project = Project.find project_id
+ # project.name = 'Website redesign and build'
+ # project.save
+ #
+ # Delete project
+ #
+ # Project.delete project_id
+ # project.destroy
+ #
+
+ class Project < Base
- def self.username=(username)
- @username = username
+ def invoices
+ Invoice.find :all, :params => {:project_id => id}
+ end
+
+ def tasks
+ Task.find :all, :params => {:project_id => id}
+ end
+
+ def timeslips
+ Timeslip.find :all, :params => {:project_id => id}
end
- def self.password=(password)
- @password = password
+ end
+
+ # Find invoices
+ #
+ # Invoice.find :all
+ # Invoice.find :all, :params => {:project_id => project_id}
+ # Invoice.find task_id
+ #
+ #TODO Create invoice
+ #
+ #TODO Update invoice
+ #
+ #TODO Delete project
+ #
+ ##TODO add Change status methods
+ # /invoices/invoice_id/mark_as_draft
+ # /invoices/invoice_id/mark_as_sent
+ # /invoices/invoice_id/mark_as_cancelled
+
+
+ class Invoice < Base
+
+ def self.find(*args)
+ opts = args.slice!(1) || {}
+ self.prefix = "/projects/#{opts[:params][:project_id]}/" if opts[:params] && opts[:params][:project_id]
+ super
+ end
+
+ end
+
+ # Find invoice items
+ #
+ # InvoiceItem.find :all, :params => {:invoice_id => invoice_id}
+ # InvoiceItem.find invoice_item_id, :params => {:invoice_id => invoice_id}
+ #
+ #TODO Create invoice item
+ #
+
+ class InvoiceItem < Base
+ self.prefix = '/invoices/:invoice_id/'
+ end
+
+ # Find tasks
+ #
+ # Task.find :all
+ # Task.find :all, :params => {:project_id => project_id}
+ # Task.find task_id
+ #
+ #TODO Create task
+ #
+ #TODO Update task
+ #
+ #TODO Delete project
+ #
+
+ class Task < Base
+
+ self.prefix = '/projects/:project_id/'
+
+# def self.find(*args)
+# opts = args.slice!(1) || {}
+# self.prefix = "/projects/#{opts[:params][:project_id]}/" if opts[:params] && opts[:params][:project_id]
+# super
+# end
+
+ end
+
+ # Find timeslips
+ #
+ # Timeslip.find :all, :params => {:view => '2009-01-01_2009-10-01'}
+ # Timeslip.find :all, :params => {:project_id => project_id}
+ # Timeslip.find :timeslip_id
+
+
+ class Timeslip < Base
+
+ def self.find(*args)
+ opts = args.slice!(1) || {}
+ self.prefix = "/projects/#{opts[:params][:project_id]}/" if opts[:params] && opts[:params][:project_id]
+ super
end
+
end
+# class ActiveResource::Connection
+# def http
+# http = Net::HTTP.new(@site.host, @site.port)
+# http.use_ssl = @site.is_a?(URI::HTTPS)
+# http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
+# http.read_timeout = @timeout if @timeout
+# #Here's the addition that allows you to see the output
+# http.set_debug_output $stderr
+# return http
+# end
+# end
+
end
Oops, something went wrong.

0 comments on commit 711f7f5

Please sign in to comment.