Jax
Jax

Reputation: 1843

How to access hash value stored in local variable

I'm doing an external API query ussing HTTParty, the resultant of that query is a hash that gets stored in an instance variable in my controller. Without saving it to my database I need to access the contents of the hash to send it as a string to another external app.

Here is my controller HTTParty call

@api_response = HTTParty.get("http://xxxxxxxxx.xx/vehicle/reg/#{@user.reg_number}/xxxxxxxxxxxxxxxxxxxxx")

Here is the response I get that is stored in @api_response:

 {"response"=>
         {"basic"=>
              {"reg"=>"xxx", "make"=>"xxxx", "model"=>"xxxx", "version"=>"xxxxx", "body"=>"xxxxxx", "doors"=>"x", "reg_date"=>"xxxxxx", "engine_cc"=>"xxxxxx", "colour"=>"xxxxx", "fuel"=>"xxxxxx", "transmission"=>"x", "data_type"=>"x", "co2_emissions"=>"xxx"}
         }
}

As it is I'm able to display the contents of @api_response in my views, but I need to retrieve the info and pass it on.

Upvotes: 1

Views: 816

Answers (1)

Brian Campbell
Brian Campbell

Reputation: 333294

You access values in a hash using square brackets surrounding the hash key. For example, to access reg out of that response, you would do:

@api_response["response"]["basic"]["reg"]

Is that all you're looking for, or did you need to do something else with it?

Upvotes: 2

Related Questions