Class: ActBlue::ActiveBlue

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/actblue.rb

Constant Summary collapse

ACT_TYPES =
{
  'source' => 'Source', 
  'page' => 'Page', 
  'lineitem' => 'LineItem', 
  'lineitems' => lambda {|hash| if (hash.is_a?(Array) && hash.first.class.name == "ActBlue::LineItem") then return hash end; collection = []; if hash['lineitem'].is_a?(Hash) then collection << LineItem.new(hash['lineitem']); else hash['lineitem'].each {|l| collection << LineItem.new(l); }; end; return collection; },
  'entity' => 'Entity', 
  'instrument' => 'Instrument', 
  'election' => 'Election',
  'expires' => 'Expires',
  'listentry' => 'ListEntry',
  'listentries' => lambda {|hash| if (hash.is_a?(Array) && hash.first.class.name == "ActBlue::ListEntry") then return hash end; collection = []; if hash['listentry'].is_a?(Hash) then collection << ListEntry.new(hash['listentry']); else hash['listentry'].each {|l| collection << ListEntry.new(l); }; end; return collection; },
  'creditcard' => 'CreditCard',
  'check' => 'Check',
  'candidacy' => 'Candidacy',
  'office' => 'Office'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ActiveBlue

Returns a new instance of ActiveBlue.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/actblue.rb', line 62

def initialize(params = {})      
  @variables = {}
  params.each do |key, value|  
    if value
      if ACT_TYPES[key.to_s] && ACT_TYPES[key.to_s].is_a?(Proc)
        collection = []
        collection = ACT_TYPES[key.to_s].call(value) if !value.empty?
        @variables[key.to_s] = collection
      elsif ACT_TYPES[key.to_s] && value.is_a?(Hash)
        @variables[key.to_s] = Object.const_get(ACT_TYPES[key.to_s]).new(value)
      else  
        @variables[key.to_s] = value
      end
    end
  end
end

Instance Attribute Details

#variablesObject

Returns the value of attribute variables.



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

def variables
  @variables
end

Instance Method Details

#[](key) ⇒ Object



79
80
81
# File 'lib/actblue.rb', line 79

def [](key)
  @variables[key]
end

#[]=(key, val) ⇒ Object



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

def []=(key, val)
  @variables[key] = val
end

#to_xmlObject



87
88
89
90
91
92
93
# File 'lib/actblue.rb', line 87

def to_xml
  doc = REXML::Document.new
  doc.add_element(to_xml_element)
  output = ""
  doc.write(output)
  output
end

#to_xml_elementObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/actblue.rb', line 95

def to_xml_element
  element = REXML::Element.new(self.class::XML_NAME)
  self.class::ATTRIBUTES.each do |a|
    element.add_attribute(a, @variables[a]) if @variables[a]
  end
  self.class::ELEMENTS.each do |e|
    if @variables[e]
      if ACT_TYPES[e] && ACT_TYPES[e].is_a?(Proc)
        parentElement = REXML::Element.new(e)
        @variables[e].each do |c|
          if c.methods.include? "to_xml_element"
            parentElement.add_element(c.to_xml_element)
          end
        end
        element.add_element(parentElement)
      else
        if @variables[e].methods.include? "to_xml_element"
          element.add_element(@variables[e].to_xml_element)
        else
          newElement = REXML::Element.new(e)
          newElement.text = @variables[e]
          element.add_element(newElement)
        end
      end
    end
  end
  element
end