Adam12344 Adam12344 - 1 year ago 78
Ruby Question

Pull values from Shopify response array

This has to be the stupidest question ever, but how do I pull values out of this Shopify API response?

orders = [#<ShopifyAPI::Order:0x007fb987a4f900 @attributes={"id"=>4141918472, "email"=>"", "created_at"=>"2016-09-28T14:12:49-05:00", "total_price"=>"109.99", "order_number"=>1001, "billing_address"=>#<ShopifyAPI::BillingAddress:0x007fb987a4e410 @attributes={"first_name"=>"John", "address1"=>"111 Fake St", "phone"=>nil, "city"=>"Los Angeles", "zip"=>"90210", "province"=>"California", "country"=>"United States", "last_name"=>"Doe", "address2"=>"", "company"=>nil, "latitude"=>34.0670499, "longitude"=>-118.2714165, "name"=>"John Doe", "country_code"=>"US", "province_code"=>"CA"}, @prefix_options={}, @persisted=true>}, @prefix_options={}, @persisted=true>, #<ShopifyAPI::Order:0x007fb987a46b98 @attributes={"id"=>4141925128, "email"=>"", "created_at"=>"2016-09-28T14:14:25-05:00", "total_price"=>"309.97", "order_number"=>1002, "billing_address"=>#<ShopifyAPI::BillingAddress:0x007fb987a456f8 @attributes={"first_name"=>"afd", "address1"=>"744 Fake", "phone"=>nil, "city"=>"Chicago", "zip"=>"60021", "province"=>"Illinois", "country"=>"United States", "last_name"=>"adsfasd", "address2"=>"", "company"=>nil, "latitude"=>42.1976923, "longitude"=>-88.2120315, "name"=>"afd adsfasd", "country_code"=>"US", "province_code"=>"IL"}, @prefix_options={}, @persisted=true>}, @prefix_options={}, @persisted=true>]

I used orders.class and it says it's an array. Is this an array of a hash? I can use .to_json but orders[0].to_json['email'] returns email. I've tried things like orders[0][1] and orders[0]['id']. But I cant get anything to work. What am I missing?

Answer Source

You should do (depending on whether Shopify defines attr_reader on attributes instance variable) either




To get all orders' data:
# or, if above is not working, but it should {|order| order.instance_variable_get(:@attributes) }


As per @Casper's comment, it would be logical to be able to do the following if API provides readers to attributes: # or any other attribute
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download