Module: Restfully::Parsing

Included in:
HTTP::Request, HTTP::Response, Session
Defined in:
lib/restfully/parsing.rb

Defined Under Namespace

Classes: ParserNotFound

Constant Summary collapse

PARSERS =
[
  {
    :supported_types => [/^application\/.*?json/i], 
    :parse => lambda{|object, options| 
      require 'json'
      JSON.parse(object)
    }, 
    :dump => lambda{|object, options|
      require 'json' 
      JSON.dump(object)
    },
    :object => true
  },
  {
    :supported_types => [/^text\/.*?(plain|html)/i], 
    :parse => lambda{|object, options| object}, 
    :dump => lambda{|object, options| object}
  },
  { # just store the binary data in a 'raw' property
    :supported_types => ["application/zip"],
    :parse => lambda{|object, options| {'raw' => object}},
    :dump => lambda{|object, options| object['raw']}
  }
]

Instance Method Summary collapse

Instance Method Details

#select_parser_for(content_type) ⇒ Object

Raises:



56
57
58
59
60
61
62
63
# File 'lib/restfully/parsing.rb', line 56

def select_parser_for(content_type)
  raise ParserNotFound.new("The Content-Type HTTP header of the resource is empty. Cannot find a parser.") if content_type.nil? || content_type.empty?
  content_type.split(",").each do |type|
    parser = PARSERS.find{|parser| parser[:supported_types].find{|supported_type| type =~ (supported_type.kind_of?(String) ? Regexp.new(supported_type) : supported_type) }}
    return parser unless parser.nil?
  end
  nil
end

#serialize(object, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/restfully/parsing.rb', line 45

def serialize(object, options = {})
  content_type = options[:content_type]
  content_type ||= object.headers['Content-Type'] if object.respond_to?(:headers)
  parser = select_parser_for(content_type)
  if parser
    parser[:dump].call(object, options)
  else
    raise ParserNotFound.new("Cannot find a parser to dump object into '#{content_type}' content.")
  end
end

#unserialize(object, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/restfully/parsing.rb', line 34

def unserialize(object, options = {})
  content_type = options[:content_type]
  content_type ||= object.headers['Content-Type'] if object.respond_to?(:headers)
  parser = select_parser_for(content_type)
  if parser
    parser[:parse].call(object, options)
  else
    raise ParserNotFound.new("Cannot find a parser to parse '#{content_type}' content.")
  end
end