trailblazer / roar

Parse and render REST API documents using representers.
http://www.trailblazer.to/gems/roar
MIT License
1.85k stars 138 forks source link

JSON-API representer does not work with Roar::Client #119

Closed ekosz closed 7 years ago

ekosz commented 9 years ago

Given the code

module SongsRepresenter
  include Roar::JSON::JSONAPI
  type :songs

  property :id
  property :name
end

class Song < OpenStruct
  include Roar::JSON::JSONAPI
  include SongRepresenter
  include Roar::Client
end

When I run

Song.new.get(uri: 'http://example.com/songs/1', as: 'application/json')

I get the error

NoMethodError: undefined method `deserialize' for #<Song>
    from /roar-1.0.0/lib/roar/http_verbs.rb:65:in `handle_response'
    from /roar-1.0.0/lib/roar/http_verbs.rb:40:in `get'

I then tried replacing Roar::JSON::JSONAPI with Roar::JSON in the client (as it does have #deserialize there) but I then got this error instead:

TypeError: no implicit conversion of String into Integer
    from /roar-1.0.0/lib/roar/json/json_api.rb:45:in `[]'
    from /roar-1.0.0/lib/roar/json/json_api.rb:45:in `from_hash'
    from /roar-1.0.0/lib/roar/json/json_api.rb:126:in `from_hash'
    from /representable-2.1.3/lib/representable/json.rb:30:in `from_json'
    from /roar-1.0.0/lib/roar/json.rb:21:in `from_json'
    from /roar-1.0.0/lib/roar/json.rb:30:in `deserialize'
    from /roar-1.0.0/lib/roar/http_verbs.rb:65:in `handle_response'
    from /roar-1.0.0/lib/roar/http_verbs.rb:40:in `get'

As an aside, I also noticed the current README is out of date in a few locations regarding JSON-API. For example the README refers to the JSON-API module as Roar::JSON::JsonApi but in the source its Roar::JSON::JSONAPI.

apotonick commented 9 years ago

@ekosz Can you add a test to client_test.rb? What happens if you include Roar::JSON and then Roar::JSON::JSONAPI?

Feel free to PR fixed README, that would be greatly appreciated! :heart:

myabc commented 7 years ago

This issue was moved to trailblazer/roar-jsonapi#15