Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hash is not getting populated? #26

Open
bunnymatic opened this issue Sep 6, 2023 · 1 comment
Open

hash is not getting populated? #26

bunnymatic opened this issue Sep 6, 2023 · 1 comment

Comments

@bunnymatic
Copy link

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?

@bunnymatic
Copy link
Author

bunnymatic commented Sep 8, 2023

I found that if I make a little thing I can subclass from, and I define the default_attr_block the same way it's defined in the deserializable/resource/dsl.rb in this gem, things work out.

With this

require 'jsonapi/deserializable'

class JsonApiResource < JSONAPI::Deserializable::Resource
  self.default_attr_block = proc { |v, k| { k.to_sym => v } }
end

Then I can do this

studio = JsonApiResource.new(jsonapi)
studio.to_h

and get the expected hash of attributes like

{: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"}

Is this the recommended use? it kind of looks like in the DSL, you try to define the defaults but they don't seem to be coming through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant