module OpenAPI::Route::ClassMethods

def static

match '/track_coupon/:source/:id' => 'mobile_banner#track_coupon', :as_ => :track_coupon, :defaults => { :format => 'png' }

Public Instance Methods

create_proc(klass, kmethod, urlpath, opts, client=nil) click to toggle source
# File lib/openapi/route.rb, line 27
def create_proc(klass, kmethod, urlpath, opts, client=nil)
  proc = Proc.new() do |args={}|
    headers = args[:headers] || {}
    body = args[:body]
    path = args[:path]
    [:body, :path, :headers].each{|k| args.delete(k)}
    if args.key?(:params)
      params = args[:params] || {}
    else
      params = args
    end
    client = OpenAPI::Route.get_client() if client.nil?

    if opts.has_key?(:default)
      opts[:defaults].each do |k,v|
        if !params.has_key?(k)
          params[k] = v
        end
      end
    end

    OpenAPI.logger.debug params

    path = OpenAPI::Route.replace_uri_vars(path || urlpath, params)

    #1. soon.wrap = do_request opts[:body] => postjson
    OpenAPI.logger.debug(path)
    response = client.do_request(opts[:via], path, {params: params, body: body, headers: headers, options: opts[:options] || {}})
    #2. callback
    return klass.send kmethod.to_s.to_sym, response, {params: params, body: body, headers: headers, options: opts[:options] || {}}
  end
  return proc
end
draw(client=nil, &block) click to toggle source
# File lib/openapi/route.rb, line 78
def draw(client=nil, &block)
  @client = client
  class_eval &block
end
get_client() click to toggle source
# File lib/openapi/route.rb, line 74
def get_client
  @client ||= OpenAPI::Client
end
match(path, callback, name, options={:via => :get}, client=nil) click to toggle source
# File lib/openapi/route.rb, line 61
def match(path, callback, name, options={:via => :get}, client=nil)
  klass_name, klass_method = callback.split("#")
  klass_name = klass_name.classify
  klass = Object.const_get(klass_name)
  if client == nil
    client = get_client()
  end
  proc = create_proc(klass, klass_method, path, options , client)
  client.api_methods << name
  client.create_method(name.to_s, proc)
  return true
end
replace_uri_vars(path, params) click to toggle source
# File lib/openapi/route.rb, line 9
def replace_uri_vars(path, params)
  new_path = path.clone()
  parts = path.split("/")
  parts.each do |part|
    if part.start_with?(":")
      key = part[1..-1].to_sym
      if params.has_key?(key)
        new_path.gsub!(Regexp.compile(":" + key.to_s), CGI.escape(params[key].to_s))
        params.delete(key)
      else
        # @todo raise real errors
        raise "Missing params: set '#{part}' to complete '#{path}' request"
      end
    end
  end
  return new_path
end