Module: EAAL::Result

Defined in:
lib/eaal/result.rb

Defined Under Namespace

Classes: ResultBase, ResultContainer, ResultElement

Class Method Summary collapse

Class Method Details

.new(prefix, xml) ⇒ Object

create a new result derived from ResultBase



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/eaal/result.rb', line 102

def self.new(prefix, xml)
    classname = prefix + 'Result'
    members = []
    values = {}
    if (xml/"eveapi/error").length > 0
      error = (xml/"eveapi/error").first
      raise EAAL::Exception.raiseEveAPIException(error["code"], error.inner_html)
    end
    if (xml/"eveapi/result").length < 1
      raise EAAL::Exception::EAALError.new("Unknown API error, no result element was found")
    end
    elements = (xml/"eveapi/result").first.containers
    elements.each {|element|
        el = EAAL::Result::ResultElement.parse_element(prefix, element)

        members << el.name
        if el.kind_of? EAAL::Rowset::RowsetBase
            values.merge!({el.name => el})
        else
            values.merge!({el.name => el.value})
        end
    }
    if not Object.const_defined? classname
        klass = Object.const_set(classname, Class.new(EAAL::Result::ResultBase))
        klass.class_eval do
            attr_accessor(*members)
        end
    else
        klass = Object.const_get(classname)
    end
    result = klass.new
    result.request_time = (xml/"eveapi/currentTime").first.inner_html
    result.cached_until = (xml/"eveapi/cachedUntil").first.inner_html
    values.each { |key,value|
        # This class may have been set up with some missing keys if the original
        # response that trigged its creation didn't contain certain elements
        # For example, some CharacterSheets don't have an "allianceName" element
        # if the character isn't in an alliance.  This adds them if they're missing
        if(!result.respond_to?("#{key}=".to_sym))
            result.class.class_eval do
                attr_accessor key.to_sym
            end
        end
        result.send(key + "=", value)
    }
    result
end