Class: Lingo::App

Inherits:
Sinatra::Bells
  • Object
show all
Defined in:
lib/lingo/app.rb

Direct Known Subclasses

Srv, Web

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.init_app(file, *args, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/lingo/app.rb', line 38

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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lingo/app.rb', line 45

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) ⇒ Object



75
76
77
78
# File 'lib/lingo/app.rb', line 75

def rackup(name)
  file = File.join(File.dirname(__FILE__), name, 'config.ru')
  file if File.readable?(file)
end

Instance Method Details

#to_json(q, r) ⇒ Object



82
83
84
85
86
87
# File 'lib/lingo/app.rb', line 82

def to_json(q, r)
  q, r = 'q', 'Required parameter -- Input string' unless q

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