Class: IronBank::Describe::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_bank/describe/object.rb

Overview

Describe an object in Zuora: name, label, fields, etc.

Defined Under Namespace

Classes: InvalidXML

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_connection(connection, name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/iron_bank/describe/object.rb', line 18

def self.from_connection(connection, name)
  IronBank.logger.info "Describe (#{name})"
  xml = connection.get("v1/describe/#{name}").body
  new(Nokogiri::XML(xml))
rescue TypeError
  # NOTE: Zuora returns HTTP 401 (unauthorized) roughly 1 out of 3 times
  #       we make this call. Since this is a setup-only call and not a
  #       runtime one, we deemed it acceptable to keep retrying until it
  #       works.
  retry
rescue IronBank::InternalServerError
  # TODO: Need to properly store which object failed to be described by
  #       Zuora API and send a report to the console.
  nil
end

.from_xml(doc) ⇒ Object



14
15
16
# File 'lib/iron_bank/describe/object.rb', line 14

def self.from_xml(doc)
  new(doc)
end

Instance Method Details

#exportObject



34
35
36
# File 'lib/iron_bank/describe/object.rb', line 34

def export
  File.open(file_path, "w") { |file| file << doc.to_xml }
end

#fieldsObject



49
50
51
52
53
# File 'lib/iron_bank/describe/object.rb', line 49

def fields
  @fields ||= doc.xpath(".//fields/field").map do |node|
    IronBank::Describe::Field.from_xml(node)
  end
end

#inspectObject



75
76
77
# File 'lib/iron_bank/describe/object.rb', line 75

def inspect
  "#<#{self.class}:0x#{(object_id << 1).to_s(16)} #{name}>"
end

#labelObject



45
46
47
# File 'lib/iron_bank/describe/object.rb', line 45

def label
  doc.at_xpath(".//object/label").text
end

#nameObject

Raises:



38
39
40
41
42
43
# File 'lib/iron_bank/describe/object.rb', line 38

def name
  node = doc.at_xpath(".//object/name")
  raise InvalidXML unless node

  node.text
end

#query_custom_fieldsObject



59
60
61
62
63
64
65
66
67
# File 'lib/iron_bank/describe/object.rb', line 59

def query_custom_fields
  @query_custom_fields ||= begin
    custom_fields = fields.select do |field|
      field.selectable? && field.custom?
    end

    custom_fields.map(&:name)
  end
end

#query_fieldsObject



55
56
57
# File 'lib/iron_bank/describe/object.rb', line 55

def query_fields
  @query_fields ||= fields.select(&:selectable?).map(&:name)
end


69
70
71
72
73
# File 'lib/iron_bank/describe/object.rb', line 69

def related
  @related ||= doc.xpath(".//related-objects/object").map do |node|
    IronBank::Describe::Related.from_xml(node)
  end
end