Class: Caren::Base

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

Overview

The base class for all API objects that can be converted to XML. The class provides basic functionality to convert objects to and from xml based on the default Caren API format.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, xml = "") ⇒ Base

Basic initializer, handles quick setting of passed attributes.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/caren/base.rb', line 9

def initialize args={}, xml=""
  self.original_xml = xml
  self.attributes = {}
  self.class.keys.each do |key|
    if args.has_key?(key)
      self.attributes[key] = args[key]
    elsif args.has_key?(key.to_s)
      self.attributes[key] = args[key.to_s]
    else
      self.attributes[key] = nil
    end
  end
  # We want to use #id and #type (in Ruby 1.8.x we need to undef it)
  self.instance_eval('undef id') if self.respond_to?(:id)
  self.instance_eval('undef type') if self.respond_to?(:type)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(args, value = nil) ⇒ Object (private)

Method missing calls to enable getters/setters



111
112
113
114
115
# File 'lib/caren/base.rb', line 111

def method_missing args, value=nil
  return self.attributes[args] if self.class.keys.include?(args)
  return self.attributes[args.to_s.gsub('=','').to_sym] = value if self.class.keys.include?(args.to_s.gsub('=','').to_sym)
  super
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/caren/base.rb', line 6

def attributes
  @attributes
end

#original_xmlObject

Returns the value of attribute original_xml.



6
7
8
# File 'lib/caren/base.rb', line 6

def original_xml
  @original_xml
end

Class Method Details

.array_rootObject

Root name of the XML array of objects



32
33
34
# File 'lib/caren/base.rb', line 32

def self.array_root
  :objects
end

.from_xml(xml) ⇒ Object

Convert a XML string to a single object or an array of objects



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/caren/base.rb', line 62

def self.from_xml xml
  return nil if xml == ''
  hash = xml.is_a?(Hash) ? xml : Hash.from_xml(xml)
  raise Caren::Exceptions::InvalidXmlResponse unless hash
  if hash.has_key?(self.array_root.to_s)
    return hash[self.array_root.to_s].map{ |h| init_dependent_objects(self.from_xml(h)) }
  elsif hash.has_key?(self.node_root.to_s)
    return init_dependent_objects(self.new( hash[self.node_root.to_s], xml ))
  else
    return init_dependent_objects(self.new( hash, xml ))
  end
end

.hash_from_image(hash_or_path) ⇒ Object



101
102
103
104
105
106
# File 'lib/caren/base.rb', line 101

def self.hash_from_image hash_or_path
  return hash_or_path if hash_or_path.is_a?(Hash)
  { :name => File.basename(hash_or_path),
    :content => File.open(hash_or_path).read,
    :content_type => `file -b --mime-type #{hash_or_path}`.gsub(/\n/,"").split(";")[0] }
end

.init_dependent_objects(object) ⇒ Object

this method is called for each parsed object. So subclasses can add extra parsing later on.



57
58
59
# File 'lib/caren/base.rb', line 57

def self.init_dependent_objects object
  object
end

.keysObject

List of available attributes for this object



27
28
29
# File 'lib/caren/base.rb', line 27

def self.keys
  [ :updated_at, :created_at, :action ]
end

.node_rootObject

Name of each XML if converted to XML



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

def self.node_root
  :object
end

.resource_locationObject

The relative location of this resource.



47
48
49
# File 'lib/caren/base.rb', line 47

def self.resource_location
  raise "No resource location found"
end

.resource_url(id = nil) ⇒ Object

The absolute (constructed url) to the resource.



88
89
90
# File 'lib/caren/base.rb', line 88

def self.resource_url id=nil
  [self.resource_location,id].compact.join("/")
end

.search_url(key, value) ⇒ Object



92
93
94
# File 'lib/caren/base.rb', line 92

def self.search_url key, value
  "#{self.resource_url}?key=#{key.to_s.dasherize}&value=#{CGI.escape(value.to_s)}"
end

.to_xml(array) ⇒ Object

Convert an array of these objects to XML



52
53
54
# File 'lib/caren/base.rb', line 52

def self.to_xml array
  array.to_xml( :root => self.array_root )
end

Instance Method Details

#as_xmlObject

Overridable hash of attributes that is used for converting to XML.



83
84
85
# File 'lib/caren/base.rb', line 83

def as_xml
  attributes.reject{ |k,v| k == :action }
end

#node_rootObject

Override this for instance specific node roots



42
43
44
# File 'lib/caren/base.rb', line 42

def node_root
  nil
end

#resource_url(id = nil) ⇒ Object

The absolute (constructed url) to the resource.



97
98
99
# File 'lib/caren/base.rb', line 97

def resource_url id=nil
  self.class.resource_url(id)
end

#to_xml(options = {}, action = nil) ⇒ Object

Convert this object to XML



76
77
78
79
80
# File 'lib/caren/base.rb', line 76

def to_xml options={}, action=nil
  base_hash = self.as_xml
  base_hash = base_hash.merge( :action => action ) if action
  base_hash.to_xml(options.merge( :root => (self.node_root || self.class.node_root) ))
end