Sha256: 4476b2e2bde40e8bf9bb7ed2bbad3db3d1c4aa4aee7d28f7f423bd032d6dc670

Contents?: true

Size: 1.03 KB

Versions: 10

Compression:

Stored size: 1.03 KB

Contents

# encoding: utf-8
#
# Copyright (C) 2011-2012  AdMaster, Inc.
#
# @author: sunxiqiu@admaster.com.cn

module Apimaster::Helpers
  module Request

    # Convert a hash to a querystring for form population
    def hash_to_query_string(hash)
      hash.collect {|k,v| "#{k}=#{v}"}.join("&")
    end

    def query_string_modifier(hash)
      hash_to_query_string(CGI::parse(request.query_string).merge(hash))
    end

    def posts
      @posts ||= to_symbol_key_hash(request_json)
    end

    private

    def to_symbol_key_hash(hash)
      return hash unless hash.is_a? Hash
      result = {}
      hash.map do |key, val|
        val = to_symbol_key_hash(val) if val.is_a? Hash
        result[key.to_sym] = val
      end
      result
    end

    def request_json
      begin
        @request_json ||= parse_json
      rescue JSON::ParserError => e
        raise Apimaster::RequestError, "Problems parsing JSON"
      end
    end

    def parse_json
      body_data = request.body.read
      body_data.empty? ? {} : JSON.parse(body_data)
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
apimaster-0.1.4 lib/apimaster/helpers/request.rb
apimaster-0.1.3 lib/apimaster/helpers/request.rb
apimaster-0.1.2 lib/apimaster/helpers/request.rb
apimaster-0.1.1 lib/apimaster/helpers/request.rb
apimaster-0.0.7 lib/apimaster/helpers/request.rb
apimaster-0.0.6 lib/apimaster/helpers/request.rb
apimaster-0.0.5 lib/apimaster/helpers/request.rb
apimaster-0.0.4 lib/apimaster/helpers/request.rb
apimaster-0.0.3 lib/apimaster/helpers/request.rb
apimaster-0.0.2 lib/apimaster/helpers/request.rb