peritor / happening

An EventMachine based S3 client
Other
145 stars 21 forks source link

webmock for tests/non-blocking put for large files/default settings/callback ease #14

Open leoc opened 13 years ago

leoc commented 13 years ago

I stumbled upon the missing of MockHttpRequest for version 1.0.0 of em-http-request. That's why I changed the tests to use WebMock, which cleaned up the tests a bit and made them green again.

EDIT: Additional changes:

leoc commented 13 years ago

Added some more.

Now you can put like this:

@item = Happening::S3::Item.new('mybucket', 'bla.mp4')
@item.put(:file => '/path/to/local/bla.mp4')

This will pass the :file option directly to em-http-request, which will upload the file chunkwise with the magnificent EventMachine::FileStreamer. No more blocking when using File.read. Yay!

leoc commented 13 years ago

I changed the execute method to return the Happening::S3::Request object, for later callback methods. The EM::HttpRequest methods/callbacks #stream and #headers are delegated.

It makes more sense, because we create a request with all the put/get/head/delete methods. The response is an accessor of this request.

Next step would be to provide something like this:

@item = Happening::S3::Item.new('bucket', 'object_id')
upload = @item.put(:file => '/path/to/file')
upload.on_success do
  puts "success!"
end
upload.on_error do
  puts "error!"
end

Or even simpler with:

upload = Happening::S3::Item.new('bucket', 'object_id').put(:file => '/path/to/file')
upload.on_success do
  puts "success!"
end
upload.on_error do
  puts "error!"
end
leoc commented 13 years ago

You can now set defaults for all items:

Happening::AWS.set_defaults(:aws_access_key_id => 'key', 
  :aws_secret_access_key => 'secret', 
  :bucket => 'bucket')
# the bucket default removes the need for a bucket, when instantiating a new item:
upload = Happening::S3::Item.new('object-id').put(:file => '/my/large/file')
leoc commented 13 years ago

Taking the example from before, you can specify callbacks via blocks, no procs for options anymore.

Happening::AWS.set_defaults(:aws_access_key_id => 'key', 
  :aws_secret_access_key => 'secret', 
  :bucket => 'bucket')
# the bucket default removes the need for a bucket, when instantiating a new item:
upload = Happening::S3::Item.new('object-id').put(:file => '/my/large/file')
upload.on_success do |uploaded|
  puts 'Yay!'
end
upload.on_error do |error|
  puts "Error while uploading: #{error.response_header.status}"
end
leoc commented 13 years ago

That's it for now. The last commit added an on_retry callback. All tests are passing. I also cleaned up the Readme, fixed some typos and wrong examples.

Hope you gonna pull my changes and publish a new version of your excellent gem.