Sha256: 47209236e3bd055389ce331aac9a0b5db0ac72e81f9ba819cee7852da6f2ddeb

Contents?: true

Size: 911 Bytes

Versions: 1

Compression:

Stored size: 911 Bytes

Contents

require 'cgi'

module Angelo

  module ParamsParser

    EMPTY_JSON = '{}'.freeze
    SEMICOLON = ';'.freeze

    def parse_formencoded str
      str.split('&').reduce(Responder.symhash) do |p, kv|
        key, value = kv.split('=').map {|s| CGI.unescape s}
        p[key] = value
        p
      end
    end

    def parse_query_string
      parse_formencoded(request.query_string || '')
    end

    def parse_post_body
      body = request.body.to_s
      case
      when form_encoded?
        body = parse_formencoded body
      when json?
        body = EMPTY_JSON if body.empty?
        body = JSON.parse body
      end
      parse_query_string.merge! body
    end

    def form_encoded?
      content_type? FORM_TYPE
    end

    def json?
      content_type? JSON_TYPE
    end

    def content_type? type
      request.headers[CONTENT_TYPE_HEADER_KEY].split(SEMICOLON).include? type
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
angelo-0.0.7 lib/angelo/params_parser.rb