Module: Azure::Table::Serialization

Includes:
Service::Serialization
Defined in:
lib/azure/table/serialization.rb

Class Method Summary collapse

Methods included from Service::Serialization

included

Methods included from Service::Serialization::ClassMethods

#access_policy_from_xml, #ary_from_node, #cors_from_xml, #cors_rule_from_xml, #cors_rule_to_xml, #cors_to_xml, #enumeration_results_from_xml, #expect_node, #hour_metrics_to_xml, #logging_from_xml, #logging_to_xml, #metadata_from_headers, #metadata_from_xml, #metrics_from_xml, #metrics_to_xml_children, #minute_metrics_to_xml, #retention_policy_from_xml, #retention_policy_to_xml, #service_properties_from_xml, #service_properties_to_xml, #signed_identifier_from_xml, #signed_identifiers_from_xml, #signed_identifiers_to_xml, #slopify, #to_bool

Class Method Details

.entries_from_feed_xml(xml) ⇒ Object



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

def self.entries_from_feed_xml(xml)
  xml = slopify(xml)
  expect_node('feed', xml)

  return nil unless (xml > 'entry').any?

  results = []

  if (xml > 'entry').count == 0
    results.push hash_from_entry_xml((xml > 'entry'))
  else
    (xml > 'entry').each do |entry|
      results.push hash_from_entry_xml(entry)
    end
  end

  results
end

.hash_from_entry_xml(xml) ⇒ Object



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

def self.hash_from_entry_xml(xml)
  xml = slopify(xml)
  expect_node('entry', xml)
  result = {}
  result[:etag] = xml['etag']
  result[:updated] = Time.parse((xml > 'updated').text) if (xml > 'updated').any?
  properties = {}
  if (xml > 'content').any?
    (xml > 'content').first.first_element_child.element_children.each do |prop|
      properties[prop.name] = prop.text != '' ? Azure::Table::EdmType.unserialize_query_value(prop.text, prop['m:type']) : prop['null'] ? nil : ''
    end
  end
  result[:properties] = properties
  result
end

.hash_to_content_xml(hash, xml = Nokogiri::XML::Builder.new(:encoding => 'UTF-8')) ⇒ Object



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

def self.hash_to_content_xml(hash, xml=Nokogiri::XML::Builder.new(:encoding => 'UTF-8'))
  xml.send('content', :type => 'application/xml') do |content|
    content.send('m:properties') do |properties|
      hash.each do |key, val|
        key = key.encode('UTF-8') if key.is_a? String and !key.encoding.names.include?('BINARY')
        val = val.encode('UTF-8') if val.is_a? String and !val.encoding.names.include?('BINARY')

        type = Azure::Table::EdmType.property_type(val)
        attributes = {}
        attributes['m:type'] = type unless type.nil? || type.empty?

        if val.nil?
          attributes['m:null'] = 'true'
          properties.send("d:#{key}", attributes)
        else
          properties.send("d:#{key}", Azure::Table::EdmType.serialize_value(type, val), attributes)
        end
      end
    end
  end

  xml
end

.hash_to_entry_xml(hash, id = nil, xml = Nokogiri::XML::Builder.new(:encoding => 'UTF-8')) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/azure/table/serialization.rb', line 28

def self.hash_to_entry_xml(hash, id=nil, xml=Nokogiri::XML::Builder.new(:encoding => 'UTF-8'))
  entry_namespaces = {
    'xmlns' => 'http://www.w3.org/2005/Atom',
    'xmlns:m' => 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata',
    'xmlns:d' => 'http://schemas.microsoft.com/ado/2007/08/dataservices'
  }

  xml.entry entry_namespaces do |entry|
      id ? entry.id(id): entry.id
      entry.updated Time.now.xmlschema
      entry.title
      entry.author do |author|
        author.name
      end
    hash_to_content_xml(hash, entry)
  end

  xml
end