Sha256: c1fd840ddd2fe5a78eb12b7decc879b3e055402cce0f8c40618b1a20abdfacd6

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

module ExtDirect
  class Request < Rack::Request
    def initialize(env)
      update_env(env)
      super
    end
    
    def action
      params.fetch("extAction", params.fetch("action", "unknown"))
    end
    
    def method
      params.fetch("extMethod", params.fetch("method", "unknown"))
    end
    
    def data
      if type == :event
        event_params = params.dup
        event_params.delete("action")
        event_params.delete("method")
        event_params
      else
        params.fetch("data", [])
      end
    end
    
    def tid
      params.fetch("extTID", params.fetch("tid", 0))
    end
    
    def type
      if get?
        :event
      elsif post? and params.include? "type"
        params.fetch("type").to_sym
      else
        :unknown
      end
    end
    
    def event?
      (type == :event)
    end
    
    def rpc?
      (type == :rpc)
    end
    
    def upload?
      params.include? "extUpload"
    end
    
    private
    
    def update_env(env)
      if env["CONTENT_TYPE"] and env["CONTENT_TYPE"].match("^application/json")
        env["rack.input"].rewind
        env.update("rack.request.form_hash" => JSON.parse(env["rack.input"].read), "rack.request.form_input" => env["rack.input"])
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ext_direct-0.3.0 lib/ext_direct/request.rb
ext_direct-0.2.0 lib/ext_direct/request.rb
ext_direct-0.1.1 lib/ext_direct/request.rb