Class: DynamicsCRM::Response::Result

Inherits:
Hash
  • Object
show all
Defined in:
lib/dynamics_crm/response/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Result

Returns a new instance of Result.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dynamics_crm/response/result.rb', line 7

def initialize(xml)
  @document = REXML::Document.new(xml)

  fault_xml = @document.get_elements("//[local-name() = 'Fault']")
  raise XML::Fault.new(fault_xml) if fault_xml.any?

  @result_response = @document.get_elements("//#{response_element}").first

  # Child classes should override this method.
  h = parse_result_response(@result_response)

  # Calling super causes undesired behavior so just merge.
  self.merge!(h)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Allows method-like access to the hash using camelcase field names.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dynamics_crm/response/result.rb', line 34

def method_missing(method, *args, &block)
  # First return local hash entry for symbol or string.
  return self[method] if self.has_key?(method)

  string_method = method.to_s
  return self[string_method] if self.has_key?(string_method)

  value = nil
  # Then check if string converted to underscore finds a match.
  if string_method =~ /[A-Z+]/
    string_method = ::DynamicsCRM::StringUtil.underscore(string_method)
    value = self[string_method] || self[string_method.to_sym]
  end

  # Finally return nil.
  value
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



4
5
6
# File 'lib/dynamics_crm/response/result.rb', line 4

def document
  @document
end

#result_responseObject (readonly)

Returns the value of attribute result_response.



5
6
7
# File 'lib/dynamics_crm/response/result.rb', line 5

def result_response
  @result_response
end

Instance Method Details

#parse_result_response(result) ⇒ Object

Invoked by constructor, should be implemented in sub-classes.



28
29
30
31
# File 'lib/dynamics_crm/response/result.rb', line 28

def parse_result_response(result)
  # do nothing here
  {}
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/dynamics_crm/response/result.rb', line 52

def respond_to_missing?(method_name, include_private = false)
  self.has_key?(method_name.to_s) || self.has_key?(method_name) || super
end

#response_elementObject

Returns base element of the response document to parse.



23
24
25
# File 'lib/dynamics_crm/response/result.rb', line 23

def response_element
  class_name = self.class.to_s.split('::').last
end