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

Open
wants to merge 17 commits into
from

Projects

None yet

1 participant

@leoc
leoc commented Oct 9, 2011

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:

  • S3::Request#execute returns S3::Request object for later callback chaining (important EM::HttpRequest methods delegated)
  • set defaults (access_key_id, secret_access_key, bucket) with Happening::AWS.set_defaults({ ... })
  • added on_retry callback
  • simplified adding callbacks from blocks
@leoc
leoc commented Oct 9, 2011

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 added some commits Oct 9, 2011
@leoc leoc updated readme 1e85745
@leoc leoc shortened lines for smaller screens 9cc1e92
@leoc leoc *** empty log message *** 81d0593
@leoc leoc * Happening::S3::Request.execute returns the Happening::S3::Request o…
…bject for callback chaining

* Happening::S3::Request delegates #stream and #headers to the http-request object/response
931e40c
@leoc
leoc commented Oct 9, 2011

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
leoc commented Oct 10, 2011

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
leoc commented Oct 10, 2011

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
leoc commented Oct 10, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment