contextio/contextio-ruby — pull request #8 by benhamill (2013-02-04 02:02:21+00:00)
Title: Creating a token for a new user fails

I have a registered user.
User clicks a button to connect his/her first mailbox.
Using a `button_to` I link to my controller.

But I run into an error.

Upon inspection of the params etc. callback_url IS passed
and contains a valid value ( `http://dubdub.bla/endpoint` ).

The default `:post` value in `connect_token_collection` crashes the app.

After some trial and error I replace `:post` with a hardcoded `:get`.
Result: `:get` returns a valid response containing a json object ( `[{token:"...",email:"..."},...]` )
but also results in an error ( see below ).

This piece of code calls the API request.
The request raises an error.

```ruby
# connect_token_collection.rb

def create(callback_url, options={})
# upon execution of this line things go wrong in connect_token.rb
result_hash = api.request(
:post,
resource_url,
options.merge(callback_url: callback_url)
)

result_hash.delete('success')

resource_class.new(api, result_hash)
end

```

with `:get`
```
NoMethodError - undefined method `keys' for #:
(gem) contextio-1.2.1/lib/contextio/api/resource.rb:91:in `validate_options'
(gem) contextio-1.2.1/lib/contextio/api/resource.rb:20:in `initialize'
(gem) contextio-1.2.1/lib/contextio/connect_token_collection.rb:41:in `new'
(gem) contextio-1.2.1/lib/contextio/connect_token_collection.rb:41:in `create'
```

with `:post`
````
Started GET "/users/connect_mailbox" for xx.xx.xx.xx
Processing by RegistrationsController#connect_mailbox as HTML ( also tried as JS )
Completed 500 Internal Server Error in 686ms

ContextIO::API::Error - Missing required argument callback_url:
(gem) contextio-1.2.1/lib/contextio/api.rb:104:in `request'
(gem) contextio-1.2.1/lib/contextio/connect_token_collection.rb:33:in `create'
app/controllers/registrations_controller.rb:11:in `connect_mailbox'
```

This is how I'm calling the code.
```ruby
class RegistrationsController < Devise::RegistrationsController
# not sure if this is necessary - didn't get that far :)
protect_from_forgery :except => :connect_mailbox # stop rails CSRF protection for this action

respond_to :json

def connect_mailbox
contextio = ContextIO.new(ENV['GIN_CONTEXT_IO_KEY'],ENV['GIN_CONTEXT_IO_SECRET'])
callback_url = "http://xxxxxxx/providers/connect-mailbox"
tokens = contextio.connect_tokens
response = tokens.create(callback_url)
respond_to do |f|
f.js
end
end
end
```