JsonApiClient / json_api_client

Build client libraries compliant with specification defined by jsonapi.org
MIT License
361 stars 186 forks source link

no implicit conversion of string to integer error #169

Open mfrederickson opened 8 years ago

mfrederickson commented 8 years ago

It's a simple response,

[
  {"id":7,"type":"Tech","displayName":"Redacted Carter"},
  {"id":9,"type":"Tech","displayName":"Michael Redacted"}
]

with a simple model

class Tech < JsonApiClient::Resource
  self.site = "https://helpdesk/helpdesk/WebObjects/Helpdesk.woa/ra"

  connection_options[:ssl] = { verify: false }
  connection_options[:params] = { "apiKey" => 'Redacted' }

  self.connection do |conn|
    conn.use Faraday::Response::Logger
  end

  def self.table_name 
    "Techs"
  end
end
$ rails c
Running via Spring preloader in process 31863
Loading development environment (Rails 4.2.5.1)
2.1.5 :001 > Tech.all
I, [2016-02-25T15:12:05.971271 #31863]  INFO -- : get https://helpdesk/helpdesk/WebObjects/Helpdesk.woa/ra/Techs?apiKey=Redacted
D, [2016-02-25T15:12:05.971415 #31863] DEBUG -- request: User-Agent: "Faraday v0.9.2"
Content-Type: "application/vnd.api+json"
Accept: "application/vnd.api+json"
I, [2016-02-25T15:12:05.993385 #31863]  INFO -- Status: 200
D, [2016-02-25T15:12:05.993495 #31863] DEBUG -- response: server: "Apache-Coyote/1.1"
x-webobjects-loadaverage: "47"
x-webobjects-servlet: "YES"
content-type: "application/json;charset=UTF-8"
content-length: "951"
date: "Fri, 26 Feb 2016 00:12:33 GMT"
connection: "close"
TypeError: no implicit conversion of String into Integer
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/parsers/parser.rb:59:in `fetch'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/parsers/parser.rb:59:in `handle_json_api'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/parsers/parser.rb:11:in `block in parse'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/parsers/parser.rb:8:in `tap'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/parsers/parser.rb:8:in `parse'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/query/requestor.rb:63:in `request'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/query/requestor.rb:26:in `get'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/query/builder.rb:95:in `find'
    from /usr/local/rvm/gems/ruby-2.1.5/gems/json_api_client-1.1.1/lib/json_api_client/query/builder.rb:83:in `to_a'

What am I doing wrong?

mfrederickson commented 8 years ago

I suppose the API that I'm hitting isn't conforming to the JSON API... the specs say that the response must be an object { } that contains a data key... Bummer, I was really hoping to use this gem...

groyoh commented 8 years ago

@mfrederickson Indeed, the response you receive is not a JSONAPI response:

  1. The response does not match the schema of JSONAPI
  2. The content type states that it's not a JSONAPI response e.g. content-type: "application/json;charset=UTF-8"

Maybe the gem could raise a proper error when the content-type is not valid.