Sha256: 33f6e618354421c60ce1525ecf251b1393d18eec4ed837e8c89f4bbf09d4b593

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

require "bundler/setup"
require "sinatra/base"
require "roar/representer/json"

class FakeServer < Sinatra::Base
  set :raise_errors, false

  module BandRepresenter
    include Roar::Representer::JSON
    
    property :name
    property :label
  end
  
  class Band
    attr_reader :name, :label
    
    def name=(value)
      @name = value.upcase
    end
    
    def label=(value)
      @label = value.upcase
    end
  end
  
  def consume_band
    Band.new.extend(BandRepresenter).from_json(request.body.string)
  end
  
  
  get "/method" do
    "<method>get</method>"
  end

  post "/method" do
    "<method>post</method>"
  end
  
  put "/method" do
    "<method>put</method>"
  end
  
  delete "/method" do
    "<method>delete</method>"
  end
  
  patch "/method" do
    "<method>patch</method>"
  end

  get '/deliberate-error' do
    raise 'this error was deliberate'
  end

  post "/bands" do
    #if request.content_type =~ /xml/
    body consume_band.to_json
    
    status 201
  end
  
  put "/bands/strungout" do
    # DISCUSS: as long as we don't agree on what to return in PUT/PATCH, let's return an updated document.
    body consume_band.to_json
    #status 204
  end

  patch '/bands/strungout' do
    # DISCUSS: as long as we don't agree on what to return in PUT/PATCH, let's return an updated document.
    body consume_band.to_json
    #status 204
  end

  get "/bands/slayer" do
    {:name => "Slayer", :label => "Canadian Maple"}.to_json
  end

  delete '/banks/metallica' do
    status 204
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roar-0.11.4 test/fake_server.rb
roar-0.11.3 test/fake_server.rb
roar-0.11.2 test/fake_server.rb