2016-10-07 7 views
0

これはこれまでにない最も疑問な質問ですが、このShopify APIレスポンスから値を引き出すにはどうすればよいですか?Shopifyレスポンス配列から値を引き出す

orders = [#<ShopifyAPI::Order:0x007fb987a4f900 @attributes={"id"=>4141918472, "email"=>"[email protected]", "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"=>"[email protected]", "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>] 

私はorders.classを使用しました。それは配列だと言います。これはハッシュの配列ですか?私は.to_jsonを使うことができますが、注文[0] .to_json ['email']はメールを返します。私は注文[0] [1]と注文[0] ['id']のようなものを試しました。しかし、私は何もすることはできません。私は何が欠けていますか?

+1

とあなたが得るものを見ます。 –

+0

また、# Adam12344

+1

を試してみました。 'orders.first.email'を試してみてください。 APIは、属性を直接メソッドにマップする必要があります。したがって、 'orders.first.email/order_number/billing_address'などはおそらく動作します。 – Casper

答えて

3

あなたはすべての注文のデータを取得するには(Shopifyはattributesインスタンス変数にattr_readerを定義するかどうかに応じて)

orders.first.attributes 

または

order.first.instance_variable_get(:@attributes) 

のいずれかを実行する必要があります。

orders.map(&:attributes) 
# or, if above is not working, but it should 
orders.map {|order| order.instance_variable_get(:@attributes) } 

編集

キャスパーさんのコメント@を1として

、APIが属性に読者に提供する場合、次の操作を行うことができるように論理的な次のようになります。コンソールやデバッガ、タイプ `orders.first`に

orders.first.email # or any other attribute 
+1

私はAPIがメソッドに直接属性をマップすると思います。だから、 'orders.first.email'はそれを行うべきです。これは100%ではありませんが、そうしなければばかげているようです。 – Casper

+1

@Casper確かにそれは完全に意味があります(私はshopifyのAPIのどちらかに慣れていません) –

+0

うん、それはうまくいった。ありがとう! – Adam12344

関連する問題