Sha256: 4acfbce3c0989970fd6e70a27978f68ced19b6954ce6eaf1099e75b73b3c3d2c

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief
# @author ongaeshi
# @date   2011/07/13

require 'rubygems'
require 'rack'
include Rack::Utils

module Milkode
  class Mkurl
    def initialize(path, params)
      @path = escape_path(path)
      @params = params
    end

    def inherit_query_shead_offset
      create_url(query_param(true, true, true))
    end

    def inherit_query_shead
      create_url(query_param(true, true, false))
    end

    def inherit_query_shead_set_sort(sort_kind)
      create_url(query_param(true, true, false, sort_kind))
    end

    def inherit_query_shead
      create_url(query_param(true, true, false))
    end

    def inherit_shead
      create_url(query_param(false, true, false))
    end

    private

    def escape_path(src)
      escape(src).gsub("%2F", '/')
    end

    def create_url(qp)
      if (qp == "")
        @path
      else
        "#{@path}?#{qp}"
      end
    end

    def query_param(query_inherit, shead_inherit, offset_inherit, sort_kind = nil)
      qparam = []
      qparam << "query=#{escape(@params[:query])}" if (query_inherit and @params[:query])
      qparam << "shead=#{escape(@params[:shead])}" if (shead_inherit and @params[:shead])
      qparam << "onematch=#{escape(@params[:onematch])}" if (shead_inherit and @params[:onematch])
      qparam << "offset=#{escape(@params[:offset])}" if (offset_inherit and @params[:offset])
      qparam << "line=#{escape(@params[:line])}" if (offset_inherit and @params[:line])
      qparam << "sort=#{sort_kind}" if sort_kind
      qparam.join('&')
    end
  end
end



Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
milkode-0.8.1 lib/milkode/cdweb/lib/mkurl.rb
milkode-0.8.0 lib/milkode/cdweb/lib/mkurl.rb