Class: Cashboard::Base

Inherits:
Struct show all
Includes:
HTTParty
Defined in:
lib/cashboard/base.rb

Constant Summary collapse

@@api_url =
"https://api.cashboardapp.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

#initialize

Methods inherited from TypecastedOpenStruct

attr_typecast, element

Constructor Details

This class inherits a constructor from Cashboard::Struct

Instance Attribute Details

#hrefObject

The unique HTTP URL that is used to access an object.



87
88
89
# File 'lib/cashboard/base.rb', line 87

def href
  @href
end

#idObject

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_authenticationObject

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
})


56
57
58
59
60
61
62
# File 'lib/cashboard/base.rb', line 56

def self.create(params={}, options={})
  options = merge_options(options)
  options.merge!({:body => self.new(params).to_xml})
  response = post("/#{resource_name}", options)
  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(options={})
  self.get_collection("/#{resource_name}", self, options)
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, options={})
  response = get(url, merge_options(options))
  check_status_code(response)
  return self.new(response[resource_name])        
end

Instance Method Details

#deleteObject

Destroys Cashboard object on the server. Returns boolean upon success.



110
111
112
113
114
115
116
117
118
119
# File 'lib/cashboard/base.rb', line 110

def delete
  options = self.class.merge_options()
  response = self.class.delete(self.href, options)
  begin 
    self.class.check_status_code(response)
  rescue
    return false
  end
  return true
end

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.



76
77
78
79
80
81
82
83
84
# File 'lib/cashboard/base.rb', line 76

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



125
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
# File 'lib/cashboard/base.rb', line 125

def to_xml(options={})
  options[:indent] ||= 2
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  xml.instruct! unless options[: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(
            options.merge({ 
              :root => key, 
              :skip_instruct => true 
            })
          )
        when ::Array
          value.to_xml(
            options.merge({ 
              :root => key, 
              :children => key.to_s.singularize, 
              :skip_instruct => true
            })
          )
        else
          xml.tag!(key, value)
      end
    end
  end
end

#updateObject

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


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cashboard/base.rb', line 96

def update
  options = self.class.merge_options()
  options.merge!({:body => self.to_xml})
  response = self.class.put(self.href, options)
  begin 
    self.class.check_status_code(response)
  rescue
    return false
  end
  return true
end