gamache / hyperresource

A self-inflating Ruby client for hypermedia APIs. Not under active development.
http://hyperresource.com/doc
MIT License
304 stars 29 forks source link

Doesn't work with foxycart sandbox #3

Closed steveklabnik closed 11 years ago

steveklabnik commented 11 years ago

I don't have time to investigate further right now, but

$ irb
irb(main):001:0> require 'hyperresource'
=> true
irb(main):002:0> api = HyperResource.new(root: "https://api-sandbox.foxycart.com/")
=> #<HyperResource:0x3fcbbecbfd08 @root="https://api-sandbox.foxycart.com/" @href="" @loaded=false @namespace=nil ...>
irb(main):003:0> root = api.get
TypeError: no implicit conversion of String into Integer
    from /Users/steve/.gem/ruby/2.0.0/gems/hyperresource-0.1.1/lib/hyper_resource/objects.rb:7:in `[]'
    from /Users/steve/.gem/ruby/2.0.0/gems/hyperresource-0.1.1/lib/hyper_resource/objects.rb:7:in `init_from_hal'
    from /Users/steve/.gem/ruby/2.0.0/gems/hyperresource-0.1.1/lib/hyper_resource.rb:108:in `init_from_response_body!'
    from /Users/steve/.gem/ruby/2.0.0/gems/hyperresource-0.1.1/lib/hyper_resource/modules/http.rb:35:in `finish_up'
    from /Users/steve/.gem/ruby/2.0.0/gems/hyperresource-0.1.1/lib/hyper_resource/modules/http.rb:13:in `get'
    from (irb):3
    from /opt/rubies/2.0.0-p0/bin/irb:12:in `<main>'
pi3r commented 11 years ago

@steveklabnik It looks like an array is returned from https://api-sandbox.foxycart.com/ where a hash is expected.

Hence you're missing the required header, it should be:

api = HyperResource.new(root: "https://api-sandbox.foxycart.com/", headers: {'FOXYCART-API-VERSION' => '1'})
root = api.get
root.response_body # => {"_links"=>{"self"=>{"href"=>"https://api-sandbox.foxycart.com/", "title"=>"Your API starting point."}, "https://api.foxycart.com/rels/property_helpers"=>{"href"=>"https://api-sandbox.foxycart.com/property_helpers", "title"=>"Various helpers used for determing valid property values."}, "https://api.foxycart.com/rels"=>{"href"=>"https://api-sandbox.foxycart.com/rels", "title"=>"Custom Link Relationships supported by this API."}, "https://api.foxycart.com/rels/create_client"=>{"href"=>"https://api-sandbox.foxycart.com/clients", "title"=>"Create a client via POST."}, "https://api.foxycart.com/rels/token"=>{"href"=>"https://api-sandbox.foxycart.com/token", "title"=>"The OAuth endpoint for obtaining a new access_token using an existing refresh_token. Post www-form-url-encoded data as follows: grant_type=refresh_token&refresh_token={refresh_token}&client_id={client_id}&client_secret={client_secret}", "type"=>"application/json"}}, "message"=>"Welcome to the FoxyCart API! Our hope is to be as self-documenting and RESTful as possible. Please let us know if you have any questions by posting in our forum http://forum.foxycart.com or emailing us at helpdesk@foxycart.com. As a last resort, you could read the documentation at http://wiki.foxycart.com. Your first action should be to create an OAuth Client, then a user, followed by a store."}

Either way, it should raise if the response is not as expected.

gamache commented 11 years ago

Yes, right now error handling is nonexistent! I have some response code handling logic I'd like to push up later this week. I'll leave this issue open until I do.

Thanks for the eyes!

gamache commented 11 years ago

I added code to raise HR::ClientError or HR::ServerError, each of which is a HR::ResponseError subclass, upon 4xx/5xx errors. Closing this issue, thanks @steveklabnik and @pi3r!