Class: Restfully::MediaType::ApplicationVndBonfireXml::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/restfully/media_type/application_vnd_bonfire_xml.rb

Class Method Summary collapse

Class Method Details

.dump(object, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/restfully/media_type/application_vnd_bonfire_xml.rb', line 37

def dump(object, opts = {})
  root_name = if opts[:serialization]
    opts[:serialization][HIDDEN_TYPE_KEY].gsub(/\_collection$/,'')
  elsif object[HIDDEN_TYPE_KEY]
    object[HIDDEN_TYPE_KEY]
  end
  fail "Can't infer a root element name for object: #{object.inspect}" if root_name.nil?
  xml = XML::Document.new
  xml.root = XML::Node.new(root_name)
  xml.root["xmlns"] = NS
  dump_object(object, xml.root)
  xml.to_s
end

.load(io, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/restfully/media_type/application_vnd_bonfire_xml.rb', line 15

def load(io, *args)
  if io.respond_to?(:read)
    io = io.read
  end
  xml = XML::Document.string(io.to_s)
  h = load_xml(xml.root)
  h[HIDDEN_TYPE_KEY] = if h['items']
    if xml.root.attributes["href"]
      xml.root.attributes["href"].
      split("/").last.gsub(/s$/,'')+"_collection"
    elsif h['items'].length > 0
      h['items'][0][HIDDEN_TYPE_KEY]
    else
      nil
    end
  else
    xml.root.name
  end
  h
end