Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

leoc
Copy link

@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
Copy link
Author

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!

…bject for callback chaining

* Happening::S3::Request delegates #stream and #headers to the http-request object/response
@leoc
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant