Class: Saasu::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/saasu/base.rb

Direct Known Subclasses

Invoice

Constant Summary collapse

ENDPOINT =
"https://secure.saasu.com/webservices/rest/r1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/saasu/base.rb', line 7

def initialize(xml)
  klass = self.class.name.split("::")[1].downcase

  xml.children.each do |child|
    if !child.text?
      child.attributes.each do |attr|
        send("#{attr[1].name.underscore.sub(/#{klass}_/, "")}=", attr[1].text)
      end
      
      send("#{child.name.underscore.sub(/#{klass}_/, "")}=", child.children.first.text) unless child.children.first.nil?
    end
  end
end

Class Method Details

.all(options = {}) ⇒ Object

Returns all resources matching the supplied conditions

Parameters:

  • conditions (Hash)

    for the request



50
51
52
53
54
55
56
57
58
59
# File 'lib/saasu/base.rb', line 50

def all(options = {})
  response = get(options)
  xml      = Nokogiri::XML(response).css("#{defaults[:collection_name]}")
  
  collection = xml.inject([]) do |result, item|
    result << new(item)
    result
  end
  collection
end

.api_keyObject

Return the API key



31
32
33
# File 'lib/saasu/base.rb', line 31

def api_key
  @@api_key
end

.api_key=(key) ⇒ Object

Parameters:

  • the (String)

    API key



25
26
27
# File 'lib/saasu/base.rb', line 25

def api_key=(key)
  @@api_key = key
end

.defaults(options = nil) ⇒ Object

Allows defaults for the object to be set. Generally the class name will be suitable and options will not need to be provided

Parameters:

  • options (Hash) (defaults to: nil)

    to override the default settings



74
75
76
77
78
79
80
81
# File 'lib/saasu/base.rb', line 74

def defaults(options = nil)
  @defaults ||= default_options
  if options
    @defaults = default_options.merge!(options)
  else
    @defaults
  end
end

.file_uidObject

Returns the file_uid



43
44
45
# File 'lib/saasu/base.rb', line 43

def file_uid
  @@file_uid
end

.file_uid=(uid) ⇒ Object

Parameters:

  • the (Integer)

    file_uid



37
38
39
# File 'lib/saasu/base.rb', line 37

def file_uid=(uid)
  @@file_uid = uid
end

.find(uid) ⇒ Object

Finds a specific resource by its uid

Parameters:

  • the (Integer)

    uid



64
65
66
67
68
# File 'lib/saasu/base.rb', line 64

def find(uid)
  response = get({:uid => uid}, false)
  xml      = Nokogiri::XML(response).css("#{defaults[:resource_name]}")
  new(xml.first)
end