class Lingo::App

Public Class Methods

init_app(file, *args, &block) click to toggle source
# File lib/lingo/app.rb, line 37
def init_app(file, *args, &block)
  set_root(file)
  parse_options(*args, &block)

  get('/about') { to_json(self.class, version: VERSION) }
end
parse_options(lingo_options = false) { || ... } click to toggle source
# File lib/lingo/app.rb, line 44
def parse_options(lingo_options = false)
  argv, banner = [], "Usage: #{$0} [-h|--help] [sinatra-options]"
  while arg = ARGV.shift and arg != '--'; argv << arg; end

  if lingo_options || block_given?
    banner << ' [-- lingo-options]'

    opts = ENV["LINGO_#{name.split('::').last.upcase}_OPTS"]
    ARGV.unshift(*Shellwords.shellsplit(opts)) if opts

    ARGV.unshift(*lingo_options) if lingo_options.is_a?(Array)
  end

  OptionParser.new(banner, 12) { |o|
    o.on('-p port',   'set the port (default is 4567)')                { |v| set :port, Integer(v) }
    o.on('-o addr',   'set the host (default is 0.0.0.0)')             { |v| set :bind, v }
    o.on('-e env',    'set the environment (default is development)')  { |v| set :environment, v.to_sym }
    o.on('-s server', 'specify rack server/handler (default is thin)') { |v| set :server, v }
    o.on('-x',        'turn on the mutex lock (default is off)')       {     set :lock, true }
  }.parse!(argv)

  argv.pop if File.basename($0) == 'rackup'  # rackup config

  abort "Unrecognized arguments: #{argv}\n#{banner}" unless argv.empty?

  ARGV.unshift(*yield) if block_given?
rescue OptionParser::ParseError => err
  abort "#{err}\n#{banner}"
end
rackup(name) click to toggle source
# File lib/lingo/app.rb, line 74
def rackup(name)
  file = File.join(File.dirname(__FILE__), name, 'config.ru')
  file if File.readable?(file)
end

Public Instance Methods

to_json(q, r) click to toggle source
# File lib/lingo/app.rb, line 81
def to_json(q, r)
  q, r = 'q', 'Required parameter -- Input string' unless q

  content_type :json
  { q => r }.to_json
end