module Tennpipes

Manages current Tennpipes version for use in gem generation.

We put this in a separate file so you can get tennpipes version without include full tennpipes core.

Constants

TENNPIPES_IGNORE_CALLERS

List of callers in a Tennpipes application that should be ignored as part of a stack trace.

VERSION

The version constant for the current version of Tennpipes.

Attributes

mounted_root[W]

Public Class Methods

add_middleware(router) click to toggle source

Creates Rack stack with the router added to the middleware chain.

# File lib/tennpipes-base.rb, line 120
def add_middleware(router)
  builder = Rack::Builder.new
  middleware.each{ |mw,args,block| builder.use(mw, *args, &block) }
  builder.run(router)
  builder.to_app
end
application() click to toggle source

The resulting rack builder mapping each 'mounted' application.

@return [Tennpipes::Router]

The router for the application.

@raise [ApplicationLoadError]

No applications were mounted.
# File lib/tennpipes-base.rb, line 68
def application
  warn 'WARNING! No apps are mounted. Please, mount apps in `config/apps.rb`' unless Tennpipes.mounted_apps.present?
  router = Tennpipes::Router.new
  Tennpipes.mounted_apps.each { |app| app.map_onto(router) }
  middleware.present? ? add_middleware(router) : router
end
bin(*args) click to toggle source

This method return the correct location of tennpipes bin or exec it using Kernel#system with the given args.

@param [Array] args

command or commands to execute

@return [Boolean]

@example

Tennpipes.bin('start', '-e production')
# File lib/tennpipes-base/command.rb, line 16
def self.bin(*args)
  @_tennpipes_bin ||= [self.ruby_command, File.expand_path("../../../bin/tennpipes", __FILE__)]
  args.empty? ? @_tennpipes_bin : system(args.unshift(@_tennpipes_bin).join(" "))
end
clear_middleware!() click to toggle source

Clears all previously configured middlewares.

@return [Array]

An empty array
# File lib/tennpipes-base.rb, line 144
def clear_middleware!
  @middleware = []
end
configure_apps(&block) click to toggle source

Configure Global Project Settings for mounted apps. These can be overloaded in each individual app's own personal configuration. This can be used like:

@yield []

The given block will be called to configure each application.

@example

Tennpipes.configure_apps do
  enable  :sessions
  disable :raise_errors
end
# File lib/tennpipes-base.rb, line 88
def configure_apps(&block)
  return  unless block_given?
  global_configurations << block
end
env() click to toggle source

Helper method that return {RACK_ENV}.

@return [Symbol]

The Tennpipes Environment.
# File lib/tennpipes-base.rb, line 55
def env
  @_env ||= RACK_ENV.to_s.downcase.to_sym
end
gem(name, main_module) click to toggle source

Registers a gem with tennpipes. This relieves the caller from setting up loadpaths by itself and enables Tennpipes to look up apps in gem folder.

The name given has to be the proper gem name as given in the gemspec.

@param [String] name

The name of the gem being registered.

@param [Module] main_module

The main module of the gem.

@returns The root path of the loaded gem

# File lib/tennpipes-base.rb, line 177
def gem(name, main_module)
  _, spec = Gem.loaded_specs.find{|spec_pair| spec_pair[0] == name }
  gems << spec
  modules << main_module
  spec.full_gem_path
end
gems() click to toggle source

@returns [Gem::Specification]

# File lib/tennpipes-base.rb, line 186
def gems
  @gems ||= []
end
global_configurations() click to toggle source

Stores global configuration blocks.

# File lib/tennpipes-base.rb, line 96
def global_configurations
  @_global_configurations ||= []
end
insert_mounted_app(mounter) click to toggle source

Inserts a Mounter object into the mounted applications (avoids duplicates).

@param [Tennpipes::Mounter] mounter

# File lib/tennpipes-base/mounter.rb, line 292
def insert_mounted_app(mounter)
  Tennpipes.mounted_apps.push(mounter) unless Tennpipes.mounted_apps.include?(mounter)
end
logger() click to toggle source

@return [Tennpipes::Logger]

@example

logger.debug "foo"
logger.warn "bar"
# File lib/tennpipes-base/logger.rb, line 17
def self.logger
  Tennpipes::Logger.logger
end
logger=(value) click to toggle source

Set the tennpipes logger.

@param [Object] value

an object that respond to <<, write, puts, debug, warn etc..

@return [Object]

The given value.

@example using ruby default logger

require 'logger'
Tennpipes.logger = Logger.new(STDOUT)

@example using ActiveSupport

require 'active_support/buffered_logger'
Tennpipes.logger = Buffered.new(STDOUT)
# File lib/tennpipes-base/logger.rb, line 38
def self.logger=(value)
  Tennpipes::Logger.logger = value
end
middleware() click to toggle source

A Rack::Builder object that allows to add middlewares in front of all Tennpipes applications.

@return [Array<Array<Class, Array, Proc>>]

The middleware classes.
# File lib/tennpipes-base.rb, line 134
def middleware
  @middleware ||= []
end
modules() click to toggle source

@returns [<Tennpipes::Module>]

# File lib/tennpipes-base.rb, line 192
def modules
  @modules ||= []
end
mount(name, options={}) click to toggle source

Mounts a new sub-application onto Tennpipes project.

@see Tennpipes::Mounter#new

@example

Tennpipes.mount("blog_app").to("/blog")
# File lib/tennpipes-base/mounter.rb, line 304
def mount(name, options={})
  Mounter.new(name, options)
end
mounted_apps() click to toggle source

@return [Array]

the mounted tennpipes applications (MountedApp objects)
# File lib/tennpipes-base/mounter.rb, line 283
def mounted_apps
  @mounted_apps ||= []
end
mounted_root(*args) click to toggle source

@param [Array] args

@return [String]

the root to the mounted apps base directory.
# File lib/tennpipes-base/mounter.rb, line 275
def mounted_root(*args)
  Tennpipes.root(@mounted_root ||= "", *args)
end
root(*args) click to toggle source

Helper method for file references.

@param [Array<String>] args

The directories to join to {TENNPIPES_ROOT}.

@return [String]

The absolute path.

@example

# Referencing a file in config called settings.yml
Tennpipes.root("config", "settings.yml")
# returns TENNPIPES_ROOT + "/config/setting.yml"
# File lib/tennpipes-base.rb, line 45
def root(*args)
  File.expand_path(File.join(TENNPIPES_ROOT, *args))
end
ruby_command() click to toggle source

Return the path to the ruby interpreter taking into account multiple installations and windows extensions.

@return [String]

path to ruby bin executable
# File lib/tennpipes-base/command.rb, line 28
def self.ruby_command
  @ruby_command ||= begin
    ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
    ruby << RbConfig::CONFIG['EXEEXT']

    # escape string in case path to ruby executable contain spaces.
    ruby.sub!(/.*\s.*/m, '"\&"')
    ruby
  end
end
run!(options={}) click to toggle source

Runs the Tennpipes apps as a self-hosted server using: thin, mongrel, or WEBrick in that order.

@example

Tennpipes.run! # with these defaults => host: "127.0.0.1", port: "3000", adapter: the first found
Tennpipes.run!("0.0.0.0", "4000", "mongrel") # use => host: "0.0.0.0", port: "4000", adapter: "mongrel"
# File lib/tennpipes-base/server.rb, line 10
def self.run!(options={})
  Tennpipes.load!
  Server.start(*detect_application(options))
end
set_encoding() click to toggle source

Set Encoding.default_internal and Encoding.default_external to Encoding::UFT_8.

Please note that in 1.9.2 with some template engines like haml you should turn off Encoding.default_internal to prevent problems.

@see github.com/rtomayko/tilt/issues/75

@return [NilClass]

# File lib/tennpipes-base.rb, line 111
def set_encoding
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
  nil
end
use(mw, *args, &block) click to toggle source

Convenience method for adding a Middleware to the whole tennpipes app.

@param [Class] m

The middleware class.

@param [Array] args

The arguments for the middleware.

@yield []

The given block will be passed to the initialized middleware.
# File lib/tennpipes-base.rb, line 160
def use(mw, *args, &block)
  middleware << [mw, args, block]
end
version() click to toggle source

The current Tennpipes version.

@return [String]

The version number.
# File lib/tennpipes-base/version.rb, line 17
def self.version
  VERSION
end

Private Class Methods

caller_files() click to toggle source

Like +Kernel#caller+ but excluding certain magic entries and without line / method information; the resulting array contains filenames only.

@return [Array<String>]

The files of the calling methods.
# File lib/tennpipes-base/caller.rb, line 47
def self.caller_files
  caller(1).
    map    { |line| line.split(/:(?=\d|in )/)[0,2] }.
    reject { |file,line| TENNPIPES_IGNORE_CALLERS.any? { |pattern| file =~ pattern } }.
    map    { |file,line| file }
end
detect_application(options) click to toggle source
# File lib/tennpipes-base/server.rb, line 19
def self.detect_application(options)
  default_config_file = 'config.ru'
  if (config_file = options.delete(:config)) || File.file?(default_config_file)
    config_file ||= default_config_file
    fail "Rack config file `#{config_file}` must have `.ru` extension" unless config_file =~ /\.ru$/
    rack_app, rack_options = Rack::Builder.parse_file(config_file)
    [rack_app, rack_options.merge(options)]
  else
    [Tennpipes.application, options]
  end
end
first_caller() click to toggle source

The filename for the file that is the direct caller (first caller).

@return [String]

The file the caller method exists in.
# File lib/tennpipes-base/caller.rb, line 36
def self.first_caller
  caller_files.first
end