Class: ExactTargetSDK::APIObject

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/exact_target_sdk/api_object.rb

Overview

Parent class of all ExactTarget API objects (listed here: docs.code.exacttarget.com/020_Web_Service_Guide/Objects). Provides class-level declarations, validation, and rendering that makes modeling each object easy.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ APIObject

By default, any properties may be passed and set.

May be overridden.



103
104
105
106
107
# File 'lib/exact_target_sdk/api_object.rb', line 103

def initialize(properties = {})
  properties.each do |key, value|
    self.send "#{key}=", value
  end
end

Class Method Details

.array_property(name, options = {}) ⇒ Object

Declares a property as an array of values.

Provides a getter and setter for this property. The getter will always return an array (not null), so the client may simply append to this property.

Note that once the property has been either read or written to, it will be rendered.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/exact_target_sdk/api_object.rb', line 49

def array_property(name, options = {})
  # TODO: type validation would be nice
  name = name.to_s

  options = {
    :nest_children => false,
    :singular => name.singularize
  }.merge(options)

  class_eval <<-__EOF__
    def #{name}
      @_set_#{name} = true
      @#{name} ||= []
    end
    def #{name}=(value)
      @_set_#{name} = true
      @#{name} = value
    end
  __EOF__
  register_property!(name, options)
end

.int_property(name, options = {}) ⇒ Object

Same as #property, adding validation the the provided value is an integer.



73
74
75
76
77
78
79
# File 'lib/exact_target_sdk/api_object.rb', line 73

def int_property(name, options = {})
  options = {
    :required => false
  }.merge(options)
  property(name, options)
  validates name.to_sym, :numericality => { :allow_nil => true, :only_integer => true }
end

.propertiesObject

Returns an array of all registered properties.



82
83
84
# File 'lib/exact_target_sdk/api_object.rb', line 82

def properties
  @properties || {}
end

.property(name, options = {}) ⇒ Object

Declares a property of this object, optionally requiring it upon validation.

Provides a getter and setter for this property, keeping track of whether or not it has been set and registering it for rendering.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/exact_target_sdk/api_object.rb', line 22

def property(name, options = {})
  options = {
    :required => false
  }.merge(options)

  name = name.to_s
  attr_reader name.to_sym
  class_eval <<-__EOF__
    def #{name}=(value)
      @_set_#{name} = true
      @#{name} = value
    end
  __EOF__
  if options[:required]
    validates name.to_sym, :presence => true
  end
  register_property!(name, options)
end

.type_nameObject



86
87
88
# File 'lib/exact_target_sdk/api_object.rb', line 86

def type_name
  name.split('::').last
end

Instance Method Details

#render!(xml) ⇒ Object

By default, runs validation and executes #render_properties!.

If overridden, the child class should check wehter or not the object is valid, and then render the object. In general, the render_properties! method should be overridden instead.

Raises:



121
122
123
124
# File 'lib/exact_target_sdk/api_object.rb', line 121

def render!(xml)
  raise(InvalidAPIObject, self) if invalid?
  render_properties!(xml)
end

#render_properties!(xml) ⇒ Object

By default, loops through all registered properties, and renders each that has been explicitly set.

May be overridden.



130
131
132
133
134
135
136
# File 'lib/exact_target_sdk/api_object.rb', line 130

def render_properties!(xml)
  self.class.properties.each do |property, options|
    next unless instance_variable_get("@_set_#{property}")
    property_value = self.send(property)
    render_property!(property, property_value, xml, options)
  end
end

#render_property!(property_name, property_value, xml, options = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/exact_target_sdk/api_object.rb', line 144

def render_property!(property_name, property_value, xml, options = {})
  if property_value.is_a?(APIObject)
    xml.__send__(property_name, { "xsi:type" => property_value.type_name } ) do
      property_value.render!(xml)
    end
  elsif property_value.is_a?(Array)
    if options[:nest_children]
      xml.__send__(property_name) do
        render_property_array!(options[:singular], property_value, xml)
      end
    else
      render_property_array!(property_name, property_value, xml)
    end
  else
    xml.__send__(property_name, property_value.to_s)
  end
end

#render_property_array!(property_name, array, xml) ⇒ Object



138
139
140
141
142
# File 'lib/exact_target_sdk/api_object.rb', line 138

def render_property_array!(property_name, array, xml)
  array.each do |current|
    render_property!(property_name, current, xml)
  end
end

#type_nameObject

By default, returns the name of the class.

May be overridden.



112
113
114
# File 'lib/exact_target_sdk/api_object.rb', line 112

def type_name
  self.class.type_name
end