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



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

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



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

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



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

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



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

def resource_to_xml(resource, entry)
  doc = Nokogiri::XML::Builder.new(:encoding => "UTF-8") 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



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

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


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

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



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

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



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

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



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

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



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

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