Class: Cashboard::Base
- Inherits:
-
Struct
- Object
- OpenStruct
- TypecastedOpenStruct
- Struct
- Cashboard::Base
- Includes:
- HTTParty
- Defined in:
- lib/cashboard/base.rb
Direct Known Subclasses
Account, ClientCompany, ClientContact, CompanyMembership, DocumentTemplate, Employee, Estimate, Expense, Invoice, InvoiceLineItem, InvoicePayment, LineItem, Payment, Project, ProjectAssignment, TimeEntry
Constant Summary collapse
- @@api_url =
"https://api.cashboardapp.com"
Instance Attribute Summary collapse
-
#href ⇒ Object
The unique HTTP URL that is used to access an object.
-
#id ⇒ Object
Override OpenStruct’s implementation of the id property.
Class Method Summary collapse
-
.authenticate(subdomain, api_key) ⇒ Object
Sets authentication credentials for all following requests.
-
.clear_authentication ⇒ Object
Clears authentication credentials.
-
.create(params = {}, options = {}) ⇒ Object
Creates a resource.
-
.list(options = {}) ⇒ Object
Lists all items for a resource.
-
.new_from_url(url, options = {}) ⇒ Object
Initializes an object of this type by passing a known URL in.
Instance Method Summary collapse
-
#delete ⇒ Object
Destroys Cashboard object on the server.
-
#links ⇒ Object
Returns hash of HTTP links for this object, returned as <link> tags in the XML.
-
#to_xml(options = {}) ⇒ Object
Utilizes ActiveSupport to turn our objects into XML that we can pass back to the server.
-
#update ⇒ Object
Updates the object on server, after attributes have been set.
Methods inherited from Struct
Methods inherited from TypecastedOpenStruct
Constructor Details
This class inherits a constructor from Cashboard::Struct
Instance Attribute Details
#href ⇒ Object
The unique HTTP URL that is used to access an object.
88 89 90 |
# File 'lib/cashboard/base.rb', line 88 def href @href end |
#id ⇒ Object
Override OpenStruct’s implementation of the id property. This allows us to set and retrieve id’s for our corresponding Cashboard items.
17 18 19 |
# File 'lib/cashboard/base.rb', line 17 def id @id end |
Class Method Details
.authenticate(subdomain, api_key) ⇒ Object
Sets authentication credentials for all following requests.
21 22 23 |
# File 'lib/cashboard/base.rb', line 21 def self.authenticate(subdomain, api_key) @@auth = {:username => subdomain, :password => api_key} end |
.clear_authentication ⇒ Object
Clears authentication credentials.
26 27 28 |
# File 'lib/cashboard/base.rb', line 26 def self.clear_authentication @@auth = {} end |
.create(params = {}, options = {}) ⇒ Object
Creates a resource.
Returns object of type created if success, or raise error if something went wrong.
Allows you to pass in a hash to create without naming it.
Example:
te = Cashboard::TimeEntry.create(
:minutes => 60, :project_id => 12345
)
57 58 59 60 61 62 63 |
# File 'lib/cashboard/base.rb', line 57 def self.create(params={}, ={}) = () .merge!({:body => self.new(params).to_xml}) response = post("/#{resource_name}", ) check_status_code(response) return self.new(response.parsed_response) end |
.list(options = {}) ⇒ Object
Lists all items for a resource.
Returns array of objects of the type listed. raises error if something goes wrong.
41 42 43 |
# File 'lib/cashboard/base.rb', line 41 def self.list(={}) self.get_collection("/#{resource_name}", self, ) end |
.new_from_url(url, options = {}) ⇒ Object
Initializes an object of this type by passing a known URL in.
31 32 33 34 35 |
# File 'lib/cashboard/base.rb', line 31 def self.new_from_url(url, ={}) response = get(url, ()) check_status_code(response) return self.new(response[resource_name]) end |
Instance Method Details
#delete ⇒ Object
Destroys Cashboard object on the server. Returns boolean upon success.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cashboard/base.rb', line 111 def delete = self.class.() response = self.class.delete(self.href, ) begin self.class.check_status_code(response) rescue return false end return true end |
#links ⇒ Object
Returns hash of HTTP links for this object, returned as <link> tags in the XML.
These links determine what you can do with an object, as defined by the Cashboard API.
77 78 79 80 81 82 83 84 85 |
# File 'lib/cashboard/base.rb', line 77 def links @links ||= begin links = HashWithIndifferentAccess.new self.link.each do |link| links[link['rel']] = link['href'] end links end end |
#to_xml(options = {}) ⇒ Object
Utilizes ActiveSupport to turn our objects into XML that we can pass back to the server.
General concept stolen from Rails CoreExtensions::Hash::Conversions
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cashboard/base.rb', line 126 def to_xml(={}) [:indent] ||= 2 xml = [:builder] ||= Builder::XmlMarkup.new(:indent => [:indent]) xml.instruct! unless [:skip_instruct] obj_name = self.class.resource_name.singularize # Turn our OpenStruct attributes into a hash we can export to XML obj_attrs = self.marshal_dump xml.tag!(obj_name) do obj_attrs.each do |key,value| next if key.to_sym == :link # Don't feed back links to server case value when ::Hash value.to_xml( .merge({ :root => key, :skip_instruct => true }) ) when ::Array value.to_xml( .merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true }) ) else xml.tag!(key, value) end end end end |
#update ⇒ Object
Updates the object on server, after attributes have been set. Returns boolean if successful
Example:
te = Cashboard::TimeEntry.new_from_url(time_entry_url)
te.minutes = 60
update_success = te.update
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cashboard/base.rb', line 97 def update = self.class.() .merge!({:body => self.to_xml}) response = self.class.put(self.href, ) begin self.class.check_status_code(response) rescue return false end return true end |