Module: Azure::ServiceBus::Serialization::ClassMethods

Included in:
Azure::ServiceBus::Serialization
Defined in:
lib/azure/service_bus/serialization.rb

Instance Method Summary collapse

Instance Method Details

#expect_node(node_name, xml) ⇒ Object



148
149
150
# File 'lib/azure/service_bus/serialization.rb', line 148

def expect_node(node_name, xml)
  raise "Xml is not a #{node_name} node." unless xml.name == node_name
end

#handle_rule_description_element(element, description) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/azure/service_bus/serialization.rb', line 91

def handle_rule_description_element(element, description)
  if element.name == "Filter" or element.name == "Action"
    value = {}
    value[:type] = element["type"]
    element.children.each do |child|
      if child.name == "SqlExpression"
        value[:sql_expression] = child.content
      elsif child.name == "CompatibilityLevel"
        value[:compatibility_level] = child.content
      elsif child.name == "CorrelationId"
        value[:correlation_id] = child.content
      end
    end
    description[element.name] = value
  end
end

#resource_from_xml(resource, node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/azure/service_bus/serialization.rb', line 70

def resource_from_xml(resource, node)
  resource = resource.to_s.capitalize

  name = (node % "title").text

  Azure::ServiceBus.const_get(resource).new(name) do |r|
    r.id          = URI((node % "id").text) if (node % "id")
    r.published   = Time.parse((node % "published").text) if (node % "published")
    r.updated     = Time.parse((node % "updated").text) if (node % "updated")
    r.author_name = (node % "author/name").text if (node % "author/name")

    r.description = (node / "content/#{resource}Description *").each_with_object({}) do |element, description|
      if resource == "Rule"
        handle_rule_description_element element, description
      else
        description[element.name] = element.text
      end
    end
  end
end

#resource_to_xml(resource, entry) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/azure/service_bus/serialization.rb', line 51

def resource_to_xml(resource, entry)
  doc = Nokogiri::XML::Builder.new do |xml|
    xml.entry(:xmlns => 'http://www.w3.org/2005/Atom') {
      xml.content(:type => 'application/xml') {
        xml.send("#{resource.to_s.capitalize}Description", 'xmlns' => 'http://schemas.microsoft.com/netservices/2010/10/servicebus/connect', 'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance') {
          if resource == :rule
            rule_to_xml xml, entry
          else
            entry.get_props.each do |p|
              xml.send(p[0], p[1].to_s)
            end
          end
        }
      }
    }
  end
  doc.to_xml
end

#resources_from_xml(resource, xml) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/azure/service_bus/serialization.rb', line 108

def resources_from_xml(resource, xml)
  feed = Nokogiri::XML(xml).remove_namespaces!
  values = (feed / 'entry').map {|node| resource_from_xml(resource, node) }
  values.class.module_eval { attr_accessor :next_link}
  values.next_link = feed.xpath("//link[@rel='next']/@href")
  values
end


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/azure/service_bus/serialization.rb', line 116

def resources_from_xml_with_next_link(resource, xml)
  feed = Nokogiri::XML(xml).remove_namespaces!
  values = Azure::Service::EnumerationResults.new((feed / 'entry').map {|node| resource_from_xml(resource, node) })

  next_token = nil
  next_uri = feed.xpath("//link[@rel='next']/@href")
  if next_uri != nil && next_uri.length > 0
    u = URI.parse(next_uri.to_s)
    p = CGI.parse(u.query)

    if p['skip'] || p['top']
      next_token = { }
      next_token[:top] = p['top'] if p['top']
      next_token[:skip] = p['skip'] if p['skip']
    end
  end

  values.continuation_token = next_token
  values
end

#rule_aspect_to_xml(xml, aspect_name, rule) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/azure/service_bus/serialization.rb', line 34

def rule_aspect_to_xml(xml, aspect_name, rule)
  aspect = rule.description[aspect_name].dup
  xml.send(aspect_name, "i:type" => aspect.delete(:type)) {
    aspect.each { |k,v|
      if k == :sql_expression
        k = "SqlExpression"
      elsif k == :compatibility_level
        k = "CompatibilityLevel"
      elsif k == :correlation_id
        k = "CorrelationId"
      end

      xml.send(k, v)
    }
  }
end

#rule_to_xml(xml, rule) ⇒ Object



29
30
31
32
# File 'lib/azure/service_bus/serialization.rb', line 29

def rule_to_xml(xml, rule)
  rule_aspect_to_xml xml, 'Filter', rule if rule.filter
  rule_aspect_to_xml xml, 'Action', rule if rule.action
end

#slopify(xml) ⇒ Object



141
142
143
144
145
146
# File 'lib/azure/service_bus/serialization.rb', line 141

def slopify(xml)
  node = (xml.is_a? String) ? Nokogiri.Slop(xml).root : xml
  node.slop! if node.is_a? Nokogiri::XML::Document unless node.respond_to? :method_missing
  node = node.root if node.is_a? Nokogiri::XML::Document
  node
end

#to_bool(s) ⇒ Object



137
138
139
# File 'lib/azure/service_bus/serialization.rb', line 137

def to_bool(s)
  (s || "").downcase == 'true'
end