Module: Tennpipes::Routing::InstanceMethods

Defined in:
lib/tennpipes-base/application/routing.rb

Overview

Instance methods related to recognizing and processing routes and serving static files.

Instance Method Summary collapse

Instance Method Details

#absolute_url(*args) ⇒ Object

Returns absolute url. Calls Sinatra::Helpers#uri to generate protocol version, hostname and port.

Examples:

absolute_url(:show, :id => 1)  # => http://example.com/show?id=1
absolute_url(:show, 24)        # => https://example.com/admin/show/24
absolute_url('/foo/bar')       # => https://example.com/admin/foo/bar
absolute_url('baz')            # => https://example.com/admin/foo/baz


810
811
812
813
814
815
816
# File 'lib/tennpipes-base/application/routing.rb', line 810

def absolute_url(*args)
  url_path = args.shift
  if url_path.is_a?(String) && !url_path.start_with?('/')
    url_path = request.env['PATH_INFO'].rpartition('/').first << '/' << url_path
  end
  uri url(url_path, *args), true, false
end

#content_type(type = nil, params = {}) ⇒ Object

Return the request format, this is useful when we need to respond to a given Content-Type.

Examples:

get :index, :provides => :any do
  case content_type
    when :js    then ...
    when :json  then ...
    when :html  then ...
  end
end

Parameters:

  • type (Symbol, nil) (defaults to: nil)
  • params (Hash) (defaults to: {})


888
889
890
891
892
893
# File 'lib/tennpipes-base/application/routing.rb', line 888

def content_type(type=nil, params={})
  return @_content_type unless type
  params.delete(:charset) if type == :json
  super(type, params)
  @_content_type = type
end

#current_path(*path_params) ⇒ Object

Returns the current path within a route from specified path_params.



825
826
827
828
829
830
831
832
833
834
# File 'lib/tennpipes-base/application/routing.rb', line 825

def current_path(*path_params)
  if path_params.last.is_a?(Hash)
    path_params[-1] = params.merge(path_params[-1].with_indifferent_access)
  else
    path_params << params
  end

  path_params[-1] = path_params[-1].symbolize_keys
  @route.path(*path_params)
end

#recognize_path(path) ⇒ Object



818
819
820
# File 'lib/tennpipes-base/application/routing.rb', line 818

def recognize_path(path)
  settings.recognize_path(path)
end

#routeObject

Returns the current route

Examples:

-if route.controller == :press
  %li=show_article


843
844
845
# File 'lib/tennpipes-base/application/routing.rb', line 843

def route
  @route
end

#static!(options = {}) ⇒ Object

Method for deliver static files.



863
864
865
866
867
868
869
# File 'lib/tennpipes-base/application/routing.rb', line 863

def static!(options = {})
  if path = static_file?(request.path_info)
    env['sinatra.static_file'] = path
    cache_control(*settings.static_cache_control) if settings.static_cache_control?
    send_file(path, options)
  end
end

#static_file?(path_info) ⇒ Boolean

This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory.

Returns:

  • (Boolean)


851
852
853
854
855
856
857
858
# File 'lib/tennpipes-base/application/routing.rb', line 851

def static_file?(path_info)
  return unless public_dir = settings.public_folder
  public_dir = File.expand_path(public_dir)
  path = File.expand_path(public_dir + unescape(path_info))
  return unless path.start_with?(public_dir)
  return unless File.file?(path)
  return path
end

#url(*args) ⇒ Object Also known as: url_for

Instance method for URL generation.

Examples:

url(:show, :id => 1)
url(:show, :name => :test)
url(:show, 1)
url("/foo", false, false)

See Also:



784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/tennpipes-base/application/routing.rb', line 784

def url(*args)
  if args.first.is_a?(String)
    url_path = settings.rebase_url(args.shift)
    if args.empty?
      url_path
    else
      # Delegate sinatra-style urls to Sinatra. Ex: url("/foo", false, false)
      # http://www.sinatrarb.com/intro#Generating%20URLs
      super url_path, *args
    end
  else
    # Delegate to Tennpipes named route URL generation.
    settings.url(*args)
  end
end