pezra / hal-client

Use HAL APIs easily
MIT License
45 stars 16 forks source link

Allow new hash syntax in Representation and Interpreter #62

Open jpserra opened 7 years ago

jpserra commented 7 years ago

Hi there,

I stumbled upon this issue while developing some specs for my project. I wanted to mock a HalClient::Representation by providing the parsed_json option to the constructor.

If the parsed_json hash keys are symbols instead of strings, things don't work quite like they should.

I'll leave a sample interaction for you to validate if this is something you'd like to see patched or not. Let me know !

[6] pry(main)> json = {
                        :plan => {
                          :id => "fake_plan_id", 
                          :name =>"Awesome Name", 
                          :amount => 0
                        },
                        :counter => 250
                      }
=> {:plan=>{:id=>"fake_plan_id", :name=>"Awesome Name", :amount=>0}, :counter=>250}
[7] pry(main)> resp = HalClient::Representation.new(parsed_json: json)
=> #<HalClient::Representation:0x75d402d4 @hal_client=nil, @href=nil, @raw={:plan=>{:id=>"fake_plan_id", :name=>"Awesome Name", :amount=>0}, :counter=>250}>
[8] pry(main)> resp.property(:plan)
KeyError: key not found: :plan
[9] pry(main)> resp.property('plan')
KeyError: key not found: :plan
[10] pry(main)> json = {
                          'plan' => {
                            'id' => "fake_plan_id", 
                            'name' => "Awesome Name", 
                            'amount' => 0
                          }, 
                          'counter' => 250
                        }
=> {"plan"=>{"id"=>"fake_plan_id", "name"=>"Awesome Name", "amount"=>0}, "counter"=>250}
[11] pry(main)> resp = HalClient::Representation.new(parsed_json: json)
=> #<HalClient::Representation:0x3d42b756 @hal_client=nil, @href=nil, @raw={"plan"=>{"id"=>"fake_plan_id", "name"=>"Awesome Name", "amount"=>0}, "counter"=>250}>
[12] pry(main)> resp.property('plan')
=> {"id"=>"fake_plan_id", "name"=>"Awesome Name", "amount"=>0}
[13] pry(main)> resp.property(:plan)
=> {"id"=>"fake_plan_id", "name"=>"Awesome Name", "amount"=>0}
pezra commented 7 years ago

With the advent of symbol garbage collection in Ruby 2.2 we could do this. It would be a nicer interface.

@jpserra, want to take a pass at implementing this feature? I merge such a PR.