Sha256: 387f6181b68a4f90ee3371e36ba63b96e1b571c12ce0bb88f153f90dc6f18ffe

Contents?: true

Size: 1.42 KB

Versions: 35

Compression:

Stored size: 1.42 KB

Contents

require 'sinatra'

def any(path, opts={}, &blk)
  %w[head get post put delete].each do |verb|
    send verb, path, opts, &blk
  end
end

def set_request_params_as_response_header!
  params.each { |k,v| response[k] = v }
end

def set_request_header_in_body!
  response['Content-Type'] ||= "application/yaml"
  headers = request.env.reject { |k,v| !v.is_a?(String) }
  headers.to_yaml
end

get '/' do
  "Hello, world!"
end

post '/' do
  request.body
end

put '/' do
  request.body
end

delete '/' do
  "Deleted"
end

# Responds with the method used for the request
any '/method' do
  request.env['REQUEST_METHOD']
end

# Responds with the response code in the url
any '/code/:code' do
  status params[:code]
  set_request_params_as_response_header!
  set_request_header_in_body!
end

# Sets the response header from the query string, and
# dumps the request header into the body as yaml for inspection
any '/header' do
  set_request_params_as_response_header!
  set_request_header_in_body!
end
  
# Takes a modified=httpdate as a query param, and a If-Modified-Since header, 
# and responds 304 if they're the same
get '/cached' do
  set_request_params_as_response_header!
  set_request_header_in_body!

  response['Last-Modified'] = params[:modified]

  modtime = params[:modified]
  imstime = request.env['HTTP_IF_MODIFIED_SINCE']
  
  if modtime && imstime && modtime == imstime
    status 304
  end
end

Sinatra::Default.set(
  :port => 42682
)

Version data entries

35 entries across 35 versions & 5 rubygems

Version Path
paul-resourceful-0.6.0 spec/simple_sinatra_server.rb
pezra-resourceful-0.5.4 spec/simple_sinatra_server.rb
pezra-resourceful-0.6.0 spec/simple_sinatra_server.rb
pezra-resourceful-0.7.0 spec/simple_sinatra_server.rb
abiquo-etk-0.6.4 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.6.3 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.6.2 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.6.1 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.6.0 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.5.9 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.5.8 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.5.3 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.42 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.33 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.32 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.29 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.25 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.24 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.23 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb
abiquo-etk-0.4.22 vendor/resourceful-1.0.1/spec/simple_sinatra_server.rb