No commit activity in last 3 years
No release in over 3 years
BubbleWrap's deprecated HTTP library
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

BubbleWrap::HTTP

Build Status

Installation

Add this line to your application's Gemfile:

gem 'bubble-wrap-http'

And then execute:

$ bundle

Or install it yourself as:

gem install bubble-wrap-http

Usage

BubbleWrap::HTTP wraps NSURLRequest, NSURLConnection and friends to provide Ruby developers with a more familiar and easier to use API. The API uses async calls and blocks to stay as simple as possible.

To enable it add the following require line to your Rakefile:

require 'bubble-wrap-http'

Usage example:

BubbleWrap::HTTP.get("https://api.github.com/users/mattetti") do |response|
  p response.body.to_str
end
BubbleWrap::HTTP.get("https://api.github.com/users/mattetti", {credentials: {username: 'matt', password: 'aimonetti'}}) do |response|
  p response.body.to_str # prints the response's body
end
data = {first_name: 'Matt', last_name: 'Aimonetti'}
BubbleWrap::HTTP.post("http://foo.bar.com/", {payload: data}) do |response|
  if response.ok?
    json = BW::JSON.parse(response.body.to_str)
    p json['id']
  elsif response.status_code.to_s =~ /40\d/
    App.alert("Login failed")
  else
    App.alert(response.error_message)
  end
end

To upload files to a server, provide a files: hash:

data = {token: "some-api-token"}
avatar_data = UIImagePNGRepresentation(UIImage.imageNamed("some-image"))
avatar = { data: avatar_data, filename: "some-image.png", content_type: "image/png" }

BubbleWrap::HTTP.post("http://foo.bar.com/", {payload: data}, files: { avatar: avatar }) do |response|
  if response.ok?
    # files are uploaded
  end
end

A :download_progress option can also be passed. The expected object would be a Proc that takes two arguments: a float representing the amount of data currently received and another float representing the total amount of data expected.

Connections can also be cancelled. Just keep a refrence,

@conn = BubbleWrap::HTTP.get("https://api.github.com/users/mattetti") do |response|
  p response.body.to_str
end

and send the cancel method to it asynchronously as desired. The block will not be executed.

@conn.cancel

Gotchas

Because of how RubyMotion currently works, you sometimes need to assign objects as @instance_variables in order to retain their callbacks.

For example:

class HttpClient
  def get_user(user_id, &callback)
    BubbleWrap::HTTP.get(user_url(user_id)) do |response|
      # ..
    end
  end
end

This class should be invoked in your code as:

@http_client = HttpClient.new
@http_client.get_user(user_id) do |user|
  # ..
end

(instead of doing an instance-variable-less HttpClient.new.get_user)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request