require "bundler/setup"
require 'sinatra/base'
require 'sinatra/reloader'
require 'roar/representer/xml'
# Usually shared in a gem between service and clients.
module BandRepresenter
include Roar::Representer::XML
property :name
property :label
end
Band = Struct.new(:name, :label)
Band.class_eval do
include Roar::Representer::XML
include BandRepresenter
end
class FakeServer < Sinatra::Base
BANDS = {"belvedere" => Band.new("Belvedere", "canadian maple")}
get "/bands/:id" do
BANDS[params[:id]].to_xml
end
get "/method" do
"get"
end
post "/method" do
"post"
end
put "/method" do
"put"
end
delete "/method" do
"delete"
end
#patch "/method" do
# "patch"
#end
post "/band" do
if request.content_type =~ /xml/
%{Strung Out}
else
'{"band": {"label": "n/a", "name": "Strung Out", "links": [{"href":"http://search", "rel": "search"}, {"href":"http://band/strungout", "rel": "self"}]}}'
end
end
put "/band/strungout" do
%{Strung Out}
end
require Dir.pwd + '/order_representers'
JSON::Order.class_eval do
def items_url
"http://localhost:9999/orders/1/items"
end
def order_url(order)
"http://localhost:9999/orders/#{order}"
end
def represented
1
end
end
post "/orders" do
incoming = JSON::Order.deserialize(request.body.string)
# create new record
# render new record
JSON::Order.from_attributes(incoming.to_attributes).serialize
end
post "/orders/1/items" do
incoming = JSON::Item.deserialize(request.body.string)
JSON::Item.from_attributes(incoming.to_attributes).serialize
end
get "/orders/1" do
JSON::Order.new(:client_id => 1, :items => [JSON::Item.new(:article_id => "666-S", :amount => 1)]).serialize
end
end
FakeServer.run! :host => 'localhost', :port => 9999