Permalink
Browse files

no more being lazy and some tweaks and polish

- all Pingback properties are NOT lazy.. it was causing testing of
  the /next.json interface to be tricky and its an optimization we
  don't need
- renamed latest => next and changed implementation to treat
  pingbacks as a FIFO instead of LIFO
- Pingback#parsed returns an empty Hash if the source attribute is nil
  • Loading branch information...
1 parent 1f8f2d8 commit 317a10c1dbb045f10297c3c0d1df03dc96c52fe7 @greatseth committed Aug 31, 2010
Showing with 61 additions and 37 deletions.
  1. +7 −9 pingback.rb
  2. +3 −3 pingback_debugger.rb
  3. +20 −12 pingback_replayer.rb
  4. +21 −8 test/pingback_debugger_test.rb
  5. +9 −5 test/pingback_test.rb
  6. +1 −0 test/test_helper.rb
View
@@ -10,10 +10,10 @@ class Pingback
include DataMapper::Resource
property :id, Serial
- property :headers, Text
- property :params, Text
- property :body, Text
- property :md5, Text
+ property :headers, Text, :lazy => false
+ property :params, Text, :lazy => false
+ property :body, Text, :lazy => false
+ property :md5, Text, :lazy => false
validates_presence_of :headers, :params, :body, :md5
@@ -31,8 +31,8 @@ def self.setup_db!
DataMapper.auto_upgrade!
end
- def self.latest
- first :order => :id.desc
+ def self.next
+ first :order => :id.asc
end
def to_json
@@ -44,9 +44,7 @@ def to_json
end
def parsed(attribute)
- if attribute_value = send(attribute)
- JSON.parse(attribute_value)
- end
+ (attribute_value = send(attribute)) ? JSON.parse(attribute_value) : {}
end
# private
View
@@ -19,8 +19,8 @@ class PingbackDebugger < Sinatra::Base
%{<pre>#{output}</pre>}
end
- get "/latest.json" do
- @pingback = Pingback.latest
+ get "/next.json" do
+ @pingback = Pingback.next
if @pingback
content_type "application/json"
@@ -36,7 +36,7 @@ class PingbackDebugger < Sinatra::Base
post "/" do
@pingback = Pingback.new \
:params => params.to_json,
- :headers => headers.to_json,
+ :headers => request.env.to_json,
:body => request.body.read
# TODO figure out how to use dm-validations and callbacks :\
View
@@ -54,17 +54,24 @@ def fetch
puts response.code
puts "ETag: #{response["Etag"]}"
- if pingback_stored?
- if new_pingback?(response)
- save_pingback(response)
- else
- @received_new_pingback = false
- end
- else
- save_pingback(response)
- end
+ # if pingback_stored?
+ # if new_pingback?(response)
+ # save_pingback(response)
+ # else
+ # @received_new_pingback = false
+ # end
+ # else
+ # save_pingback(response)
+ # end
puts latest_pingback.inspect
+
+ if response.code == 200
+ save_pingback(response)
+ true
+ else
+ false
+ end
end
def received_new_pingback?
@@ -89,16 +96,17 @@ def save_pingback(response)
if __FILE__ == $0
fetcher = PingbackFetcher.new
- player = PingbackReplayer.new "http://pingback-debugger.heroku.com"
+ player = PingbackReplayer.new "http://localhost:"
loop do
puts "fetching latest pingback"
- fetcher.fetch
- if fetcher.received_new_pingback?
+ if fetcher.fetch
+ # if fetcher.received_new_pingback?
puts "fetched pingback: #{fetcher.latest_pingback.inspect}",
"replaying.."
response = player.replay! fetcher.latest_pingback
puts "result of replay: #{response.inspect}"
+ # end
end
sleep 20
end
@@ -20,27 +20,27 @@ def teardown
assert_equal 1, Pingback.count
end
- test "getting latest pingback" do
- get '/latest.json'
+ test "getting next pingback" do
+ get '/next.json'
assert last_response.not_found?
ping!
assert last_response.ok?
- pingback = Pingback.latest
+ pingback = Pingback.next
assert_not_nil pingback
- get '/latest.json'
+ get '/next.json'
assert last_response.ok?
json = nil
assert_nothing_raised { json = JSON.parse last_response.body }
assert_equal pingback.parsed(:params), json["params"]
- assert_equal pingback.parsed(:headers), json["headers"]
+ assert_equal pingback.parsed(:headers)["Content-Type"], json["headers"]["Content-Type"]
assert_equal pingback.body, json["body"]
assert_equal %{"#{pingback.md5}"}, last_response.headers["ETag"]
- get '/latest.json'
+ get '/next.json'
assert last_response.not_found?
ping!
@@ -66,11 +66,24 @@ def teardown
def ping!(options = {})
params = options[:params] || {}
- rack_env = options[:headers] || {}
+ rack_env = get_default_request_headers.merge(options[:headers] || {})
+
+ # without this, Rack parses the body into params..
+ # seems like a bug, but haven't probed Rack enought to know for sure
+ rack_env["CONTENT_TYPE"] = rack_env["Content-Type"]
+
rack_env["rack.input"] = StringIO.new(
- options[:body] || File.read("encoding-dot-com.xml")
+ options[:body] || get_default_pingback_body
)
post "/", params, rack_env
end
+
+ def get_default_pingback_body
+ File.read("encoding-dot-com.xml")
+ end
+
+ def get_default_request_headers
+ { "Content-Type" => "application/xml" }
+ end
end
View
@@ -9,8 +9,8 @@ def teardown
test "parsed accessors" do
pingback = Pingback.new
- assert_nil pingback.parsed(:params)
- assert_nil pingback.parsed(:headers)
+ assert_equal Hash.new, pingback.parsed(:params)
+ assert_equal Hash.new, pingback.parsed(:headers)
stuff = { "foo" => "bar" }
@@ -22,12 +22,16 @@ def teardown
end
test "latest" do
- assert_nil Pingback.latest
+ assert_nil Pingback.next
pingback1 = Pingback.create!
- assert_equal pingback1.id, Pingback.latest.id
+ assert_equal pingback1.id, Pingback.next.id
pingback2 = Pingback.create!
- assert_equal pingback2.id, Pingback.latest.id
+ assert_equal pingback1.id, Pingback.next.id
+
+ pingback1.destroy
+
+ assert_equal pingback2.id, Pingback.next.id
end
end
View
@@ -4,6 +4,7 @@
Bundler.setup :test
require 'test/unit'
+require 'redgreen'
class Test::Unit::TestCase
def self.test(description, &block);

0 comments on commit 317a10c

Please sign in to comment.