Sha256: 21fcbda9bb55733cd206afeb2ecf1284f7485578287dbc903d13eebd4df1ec6d

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents


require 'cuba'
require 'da99_rack_protect'
require 'multi_json'

Cuba.use Da99_Rack_Protect.config { |c|
  c.config :host, [:localhost, 'www_app.com']
}

Cuba.use Rack::ShowExceptions

Cuba.use(Class.new {
  def initialize app
    @app = app
  end

  def call env
    results = @app.call(env)
    if results.first == 404
      if env['PATH_INFO'] == '/'
        return [
          200,
          {"Content-Type" => "text/html"},
          [File.read("./specs/client-side/index.html").gsub("{token}", env['rack.session']['csrf'])]
        ]
      end

      [
        Rack::Directory.new(File.expand_path('./specs/lib')),
        Rack::Directory.new(File.expand_path('./specs/client-side')),
        Rack::Directory.new(File.expand_path('./lib/public'))
      ].detect { |r|
        status, headers, body = r.call(env)
        return [status, headers, body] if status !=  404
        false
      }
    end

    results
  end
})

Cuba.define do

  on post do
    data = (req.env["rack.request.form_hash"]).dup
    data.delete('authenticity_token')

    res['Content-Type'] = 'application/json';

    on "repeat/error_msg" do
      res.write MultiJson.dump({
        'data' => data,
        'clean_html' => {
          'error_msg' => "<span>#{data['error_msg'] || 'Unknown error.'}</span>"
        }
      })
    end

    on "repeat/success_msg" do
      res.write MultiJson.dump({
        'data' => data,
        'clean_html' => {
          'success_msg' => "<span>#{ data['success_msg'] || 'Unknown success'}</span>"
        }
      })
    end

    on "repeat/vals" do
      res.write MultiJson.dump({
        'data' => data.select { |k,v| k['val'] },
        'clean_html' => {
          'success_msg' => 'Success in repeating vals.'
        }
      })
    end

    on(default) {
      res.status = 404
      res.write 'Missing'
    }
  end

  on get do

    on(default) {
      res.status = 404
      res.write 'Missing'
    }

  end # === on get

end # === Cuba.define

run Cuba

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
www_app-1.3.0 specs/lib/config.ru
www_app-1.2.1 specs/lib/config.ru
www_app-1.2.0 specs/lib/config.ru
www_app-1.1.0 specs/lib/config.ru
www_app-1.0.1 specs/lib/config.ru
www_app-1.0.0 specs/lib/config.ru