Sha256: d52c3b4215371c63612b5e97cb204bc338b11733eb763e86b038a08617663ef7

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

require 'rest-core'

require 'cgi'

module RestCore::Middleware
  include RestCore

  def self.included mod
    mod.send(:include, RestCore)
    mod.send(:attr_reader, :app)
    return unless mod.respond_to?(:members)
    src = mod.members.map{ |member| <<-RUBY }
      def #{member} env
        if    env.key?('#{member}')
          env['#{member}']
        else
              @#{member}
        end
      end
    RUBY
    args      = [:app] + mod.members
    para_list = args.map{ |a| "#{a}=nil"}.join(', ')
    args_list = args                     .join(', ')
    ivar_list = args.map{ |a| "@#{a}"   }.join(', ')
    src << <<-RUBY
      def initialize #{para_list}
        #{ivar_list} = #{args_list}
      end
    RUBY
    accessor = Module.new
    accessor.module_eval(src.join("\n"), __FILE__, __LINE__)
    mod.const_set(:Accessor, accessor)
    mod.send(:include, accessor)
  end

  def call env     ; app.call(env)                               ; end
  def fail env, obj; env.merge(FAIL => (env[FAIL] || []) + [obj]); end
  def log  env, obj; env.merge(LOG  => (env[LOG]  || []) + [obj]); end

  module_function
  def request_uri env
    # compacting the hash
    if (query = (env[REQUEST_QUERY] || {}).select{ |k, v| v }).empty?
      env[REQUEST_PATH].to_s
    else
      q = if env[REQUEST_PATH] =~ /\?/ then '&' else '?' end
      "#{env[REQUEST_PATH]}#{q}" \
      "#{query.map{ |(k, v)|
        "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')}"
    end
  end
  public :request_uri
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rest-core-0.8.2 lib/rest-core/middleware.rb
rest-core-0.8.1 lib/rest-core/middleware.rb
rest-core-0.8.0 lib/rest-core/middleware.rb
rest-core-0.7.2 lib/rest-core/middleware.rb
rest-core-0.7.0 lib/rest-core/middleware.rb
rest-core-0.4.0 lib/rest-core/middleware.rb
rest-core-0.4.0.pre.2 lib/rest-core/middleware.rb
rest-core-0.4.0.pre.1 lib/rest-core/middleware.rb
rest-core-0.4.0.pre.0 lib/rest-core/middleware.rb