Skip to content

hash is not getting populated? #26

Open
@bunnymatic

Description

@bunnymatic

Hi

I'm working in a little Sinatra app and found that after deserializing with the Resource class defined I here, I can't seem to get to the attributes because the to_h method give me nothing.

The get_jsonapi method was defined elsewhere and does the http call and returns the response data that lives under "data" key.

As you can see, the deserializer properly pulls the attribute out but does not fill the @hash attribute so the to_h doesn't give me any data back.

# in a console

> jsonapi = get_jsonapi("studios/1890-bryant-street-studios.json")
=> {"id"=>"3",
 "type"=>"studio",
 "attributes"=>
  {"name"=>"1890 Bryant Street Studios",
   "street_address"=>"1890 bryant st",
   "city"=>"New Ginaberg",
   "map_url"=>"http://maps.google.com/maps?q=1890+bryant+st%2C+New+Ginaberg%2C+Kansas%2C+87776",
   "url"=>"/studios/1890-bryant-street-studios",
   "slug"=>"1890-bryant-street-studios"},
"relationships"=>{"artists"=>{"meta"=>{"count"=>27}}}}

> studio = JSONAPI::Deserializable::Resource.new(jsonapi)
=> #<JSONAPI::Deserializable::Resource:0x00007fc8663fcab0
 @attributes=
  {"name"=>"1890 Bryant Street Studios",
   "street_address"=>"1890 bryant st",
   "city"=>"New Ginaberg",
   "map_url"=>"http://maps.google.com/maps?q=1890+bryant+st%2C+New+Ginaberg%2C+Kansas%2C+87776",
   "url"=>"/studios/1890-bryant-street-studios",
   "slug"=>"1890-bryant-street-studios"},
 @data=
  {"id"=>"3",
   "type"=>"studio",
   "attributes"=>
    {"name"=>"1890 Bryant Street Studios",
     "street_address"=>"1890 bryant st",
     "city"=>"New Ginaberg",
     "map_url"=>"http://maps.google.com/maps?q=1890+bryant+st%2C+New+Ginaberg%2C+Kansas%2C+87776",
     "url"=>"/studios/1890-bryant-street-studios",
     "slug"=>"1890-bryant-street-studios"},
   "relationships"=>{"artists"=>{"meta"=>{"count"=>27}}}},
 @hash={},
 @id="3",
 @relationships={"artists"=>{"meta"=>{"count"=>27}}},
 @reverse_mapping={},
 @root="/data",
 @type="studio">


> studio.to_h
=> {}

Am I mis-using things? Or is this a bug?

I did a tiny bit of digging and it seems that in resource.rb where you define

      def deserialize_attr(key, val)
        block = self.class.attr_blocks[key] || self.class.default_attr_block
        return {} unless block

the block is actually nil. Is that expected? Am I supposed to define a default_attr_block?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions