Skip to content

Instantly share code, notes, and snippets.

@redjohn
Created May 10, 2011 17:14
Show Gist options
  • Select an option

  • Save redjohn/964928 to your computer and use it in GitHub Desktop.

Select an option

Save redjohn/964928 to your computer and use it in GitHub Desktop.
task :default do |t|
require './ruby/rest_service'
RestService.run!
end
task :client, :cmd do |t, args|
classpath = '.'
Dir.glob('lib/*.jar').each {|file| classpath += ":#{file}" }
puts `scala -cp #{classpath} RestClient.scala #{args[:cmd]}`
end
#a simple REST service that replicates the one used in Scala in Action 2nd ed., Ch. 2
require 'sinatra/base'
class Object
def dump_info
#need to filter out Rack variables
self.inject('') {|str, elem| elem[0] !~ /^rack\./ ? "#{str}|#{elem[0]}->#{elem[1]}" : str }
end
end
class RestService < Sinatra::Base
def get_request_info params, request
["parameters: #{params.dump_info}", "headers: #{request.env.dump_info}"]
end
get '/' do
ret = ['GET method called']
(ret + get_request_info(params, request)).join "\n"
end
post '/' do
ret = ['POST method called']
(ret + get_request_info(params, request)).join "\n"
end
delete '/' do
"DELETE method called\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment