Giter Club home page Giter Club logo

rack-app's Introduction

rack-app


https://github.com/rack-app/rack-app

gem 'rack-app'
bundle
gem install rack-app
require 'rack/app'
class App < Rack::App
  desc 'some hello endpoint'
  get '/hello' do
    'Hello'
  end
end

require 'rack/app'
class App < Rack::App
  mount SomeAppClass
    headers 'Access-Control-Allow-Origin' => '*',
            'Access-Control-Expose-Headers' => 'X-My-Custom-Header, X-Another-Custom-Header'
    serializer do |object|
      object.to_s
    end
    desc 'some hello endpoint'
      required 'words', :class => Array, :of => String, desc => 'some word', :example => ['pug']
      optional 'word', :class => String, :desc => 'one word', :example => 'pug'
      optional 'boolean', :class => :boolean, :desc => 'boolean value', :example => true
    end
    get '/hello' do
      puts(params['words'])
      'Hello'
    end
  end
  namespace '/users' do
    desc 'some restful endpoint'
    get '/:user_id' do
      response.status = 201
      params['user_id']
      say
    end
  end
  desc 'some endpint that has error and will be rescued'
  get '/make_error' do
    raise(StandardError,'error block rescued')
  end
  def say
    "hello #{params['user_id']}"
  end
  error StandardError, NoMethodError do |ex|
    {:error=>ex.message}
  end
  root '/hello'
  get '/stream' do
    stream do |out|
      out << 'data row'
    end
  end
end

require 'rack/app'
require 'rack/app/front_end'
class App < Rack::App
  apply_extensions :front_end
  helpers do
    def method_that_can_be_used_in_template
      'hello!'
    end
  end
  layout 'layout.html.erb'
  get '/' do
    render 'index.html'
  end
end

require 'spec_helper'
require 'rack/app/test'
describe App do
  include Rack::App::Test
  rack_app described_class
  describe '/hello' do
    subject { get(url: '/hello', params {'dog' => 'meat'}, headers: {'X-Cat' => 'fur'}, payload: 'some string') }
    it { expect(subject.status).to eq 200 }
    it { expect(subject.body).to eq "Hello!" }
  end
  describe '' do
    subject { get(url: '/users/1234') }
    it { expect(subject.body).to eq 'hello'}
    it { expect(subject.status).to eq 201 }
  end
  describe '/make_error' do
    subject { get(url: '/make_error') }
    it { expect(subject.body).to eq '{:error=>"error block rescued"}' }
  end
end

rack-app's People

Contributors

takagotch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.