Sha256: addbe35fbdcc1fea110834c95efc7c3c39aabf69b9550bc42fa59ae00ec3441b

Contents?: true

Size: 1.42 KB

Versions: 20

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 => 3000
)

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
paul-resourceful-0.5.3 spec/simple_sinatra_server.rb
paul-resourceful-0.5.4 spec/simple_sinatra_server.rb
almodovar-0.5.6 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.5.5 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.5.4 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.5.3 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.5.2 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.5.1 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.5.0 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.4.0 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.3.2 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.3.1 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.3.0 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.2.0 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
almodovar-0.1.2 vendor/resourceful-0.5.3-patched/spec/simple_sinatra_server.rb
resourceful-0.5.4 spec/simple_sinatra_server.rb
resourceful-0.5.0 spec/simple_sinatra_server.rb
resourceful-0.5.1 spec/simple_sinatra_server.rb
resourceful-0.5.2 spec/simple_sinatra_server.rb
resourceful-0.5.3 spec/simple_sinatra_server.rb