Module: Azure::Storage::Table::Serialization

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

Overview

This is a class that serializes the request/response body for table service.

Class Method Summary collapse

Class Method Details

.entities_from_hash(h) ⇒ Object


78
79
80
81
82
83
84
# File 'lib/azure/storage/table/serialization.rb', line 78

def self.entities_from_hash(h)
  entities = []
  h["value"].each { |entity_hash|
    entities.push(entity_from_hash(entity_hash))
  }
  entities
end

.entities_from_json(json) ⇒ Object


73
74
75
76
# File 'lib/azure/storage/table/serialization.rb', line 73

def self.entities_from_json(json)
  entities_hash = hash_from_json(json)
  entities_hash["value"].nil? ? [entity_from_hash(entities_hash)] : entities_from_hash(entities_hash)
end

.entity_from_hash(h) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/azure/storage/table/serialization.rb', line 86

def self.entity_from_hash(h)
  Entity.new do |entity|
    entity.etag = h.delete(TableConstants::ODATA_ETAG)
    properties = {}
    h.each do |k, v|
      next if k.end_with? TableConstants::ODATA_TYPE_SUFFIX
      type = h[k + TableConstants::ODATA_TYPE_SUFFIX]
      properties[k] = EdmType::deserialize_value(v, type.nil? ? EdmType::property_type(v) : type)
    end
    entity.properties = properties
  end
end

.entity_from_json(json) ⇒ Object


69
70
71
# File 'lib/azure/storage/table/serialization.rb', line 69

def self.entity_from_json(json)
  entity_from_hash(hash_from_json(json))
end

.get_accept_string(accept_type = :min_meta) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/azure/storage/table/serialization.rb', line 99

def self.get_accept_string(accept_type = :min_meta)
  case accept_type
  when :no_meta
    Azure::Storage::Common::HeaderConstants::ODATA_NO_META
  when :min_meta
    Azure::Storage::Common::HeaderConstants::ODATA_MIN_META
  when :full_meta
    Azure::Storage::Common::HeaderConstants::ODATA_FULL_META
  when nil
    Azure::Storage::Common::HeaderConstants::ODATA_MIN_META
  else
    accept_type
  end
end

.hash_from_json(json) ⇒ Object


53
54
55
# File 'lib/azure/storage/table/serialization.rb', line 53

def self.hash_from_json(json)
  JSON.parse(json)
end

.hash_to_json(h) ⇒ Object


38
39
40
41
42
43
44
45
46
47
# File 'lib/azure/storage/table/serialization.rb', line 38

def self.hash_to_json(h)
  newhash = {}
  h.map do |key, val|
    type = Table::EdmType.property_type(val)
    type_key = key.is_a?(Symbol) ? key.to_s + TableConstants::ODATA_TYPE_SUFFIX : key + TableConstants::ODATA_TYPE_SUFFIX
    newhash[key] = EdmType::serialize_value(type, val)
    newhash[type_key] = type unless type.nil? || type.empty? || h.key?(type_key)
  end
  JSON newhash
end

.table_entries_from_hash(h) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/azure/storage/table/serialization.rb', line 57

def self.table_entries_from_hash(h)
  values = []
  if h["value"]
    h["value"].each do |name|
      values.push(name)
    end
  elsif h["TableName"]
    values = h
  end
  values
end

.table_entries_from_json(json) ⇒ Object


49
50
51
# File 'lib/azure/storage/table/serialization.rb', line 49

def self.table_entries_from_json(json)
  table_entries_from_hash(hash_from_json(json))
end