Sha256: 9d95b95a1f261cb29befe7659c38410c56edc5ff8c27e57f193024d8042e436e

Contents?: true

Size: 909 Bytes

Versions: 1

Compression:

Stored size: 909 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.escape 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.4 lib/angelo/params_parser.rb