Skip to content

Instantly share code, notes, and snippets.

@robbielamb
Created January 18, 2010 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbielamb/280409 to your computer and use it in GitHub Desktop.
Save robbielamb/280409 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -w
require 'net/http'
require 'uri'
require 'rubygems'
require 'json'
require 'ruby-debug'
SERVER = 'localhost'
PORT = 8080
HEADERS = { 'Accept' => 'application/json',
'Content-Type' => 'application/json'
}
@persevere = Net::HTTP.new(SERVER, PORT)
KNIGHT=<<-END
{ "id" : "Knight",
"properties": {
"name": {
"optional":true,
"type":"string"
}
},
"prototype":{
}
}
END
KNIGHT2=<<-END
{ "id" : "Knight2",
"properties": {
"name": {
"optional":true,
"type":"number"
}
},
"prototype":{
}
}
END
DRAGON=<<-END
{"id":"Dragon",
"properties":{
"birth_time":{
"optional":true,
"type":"string"
},
"is_fire_breathing":{
"optional":true,
"type":"boolean"
},
"toes_on_claw":{
"optional":true,
"type":"integer"
},
"birth_at":{
"optional":true,
"type":"string"
},
"name":{
"type":"string"
},
"birth_on":{
"optional":true,
"type":"string"
}
},
"prototype":{
}
}
END
COUNTRY=<<-END
{"id":"Country",
"properties":{
"population":{
"optional":true,
"type":"integer"
},
"birth_rate":{
"optional":true,
"type":"number"
},
"gold_reserve_tonnes":{
"optional":true,
"type":"number"
},
"name":{
"type":"string"
},
"gold_reserve_value":{
"optional":true,
"type":"number"
}
},
"prototype":{
}
}
END
HEFFALUMP=<<-END
{"id":"Heffalump",
"properties":{
"color":{
"optional":true,
"type":"string"
},
"num_spots":{
"optional":true,
"type":"integer"
},
"striped":{
"optional":true,
"type":"boolean"
},
"parent":{
"type":"string"
},
"cid":{
"type":"string"
},
"newdata":{
"type":"any"
},
"data":{
"type":"string"
}
},
"prototype":{
}
}
END
# @schemas = {'Knight' => KNIGHT, 'Dragon' => DRAGON, 'Country' => COUNTRY, 'Heffalump' => HEFFALUMP }
@schemas = {'Knight' => KNIGHT, 'Dragon' => DRAGON }
def validate_current_schemas(*except)
num_different = 0
@schemas.reject{|i,k| except.include?(i)}.each_pair do |id,cur_schema|
response = @persevere.send_request('GET', "/Class/#{id}", nil, HEADERS)
if JSON.parse(response.body) != JSON.parse(cur_schema)
num_different += 1
puts "#{id} is different."
puts "Expected: "
puts cur_schema
puts
puts "Recieved: "
puts response.body
puts
end
end
num_different
end
# First lets shove everything into persever
def setup
@schemas.each_pair do |id, cur_schema|
@persevere.send_request('POST', '/Class/', cur_schema, HEADERS)
end
end
setup
difference = validate_current_schemas
puts "Starting off there are #{difference} different."
puts
gets
# like auto_migrate!, remove schema, post schema, check for differences along the way.
1.times do |i|
puts "#{i} times"
@schemas.each_pair do |id, cur_schema|
@persevere.send_request('DELETE', "/Class/#{id}", nil, HEADERS)
puts "Deleted: #{id}"
validate_current_schemas(id)
puts
gets
puts "Creating #{id} with #{cur_schema}"
@persevere.send_request('POST', '/Class/', cur_schema, HEADERS)
puts "Created: #{id}"
validate_current_schemas
puts
gets
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment