1

I'm trying to work with what I think is a JSON response from Shopify. It looks like this:

=> #<ActiveResource::Collection:0x007f61f45c3840
 @elements=
  [#<ShopifyAPI::Product:0x007f61f45c3638
    @attributes=
     {"id"=>2156425793,
      "title"=>"Hue Women's Python Net Tight, Black, Small-Medium",
      "body_html"=>"Python pattern",
      "vendor"=>"HUE",
      "product_type"=>"Apparel",
      "created_at"=>"2015-10-02T09:42:07-04:00",
      "handle"=>"hue-womens-python-net-tight-black-small-medium",
      "updated_at"=>"2015-10-02T09:42:07-04:00",
      "published_at"=>"2015-10-02T09:42:07-04:00",
      "template_suffix"=>nil,
      "published_scope"=>"global",
      "tags"=>"",
  "variants"=>
   [#<ShopifyAPI::Variant:0x007f61f45c11a8
     @attributes=
      {"id"=>6989658561,
       "title"=>"First",
       "price"=>"18.00",
       "sku"=>"123",
       "position"=>1,
       "grams"=>0,
       "inventory_policy"=>"deny",
       "compare_at_price"=>"18.00",
       "fulfillment_service"=>"amazon_marketplace_web",
       "inventory_management"=>"shopify",
       "option1"=>"First",
       "option2"=>nil,
       "option3"=>nil,
       "created_at"=>"2015-10-02T09:42:07-04:00",
       "updated_at"=>"2015-10-02T09:42:07-04:00",
       "requires_shipping"=>true,
       "taxable"=>true,
       "barcode"=>nil,
       "inventory_quantity"=>1,
       "old_inventory_quantity"=>1,
       "image_id"=>nil,
       "weight"=>0.0,
       "weight_unit"=>"lb"},
     @persisted=true,
     @prefix_options={:product_id=>2156425793}>,
    #<ShopifyAPI::Variant:0x007f61f45b4d18
     @attributes=
      {"id"=>6989658625,
       "title"=>"Second",
       "price"=>"18.00",
       "sku"=>"345",
       "position"=>2,
       "grams"=>0,
       "inventory_policy"=>"deny",
       "compare_at_price"=>"18.00",
       "fulfillment_service"=>"amazon_marketplace_web",
       "inventory_management"=>"shopify",
       "option1"=>"Second",
       "option2"=>nil,
       "option3"=>nil,
       "created_at"=>"2015-10-02T09:42:07-04:00",
       "updated_at"=>"2015-10-02T09:42:07-04:00",
       "requires_shipping"=>true,
       "taxable"=>true,
       "barcode"=>nil,
       "inventory_quantity"=>3,
       "old_inventory_quantity"=>3,
       "image_id"=>nil,
       "weight"=>0.0,
       "weight_unit"=>"lb"},
     @persisted=true,
     @prefix_options={:product_id=>2156425793}>],
  "options"=>
   [#<ShopifyAPI::Option:0x007f61f45a92b0
     @attributes={"id"=>2550138369, "product_id"=>2156425793, 
     "name"=>"Title", "position"=>1, "values"=>["First", "Second"]},
     @persisted=true,
     @prefix_options={}>],
  "images"=>
   [#<ShopifyAPI::Image:0x007f61f459b390
     @attributes=
      {"id"=>5116928641,
       "position"=>1,
       "created_at"=>"2015-10-02T09:42:07-04:00",
       "updated_at"=>"2015-10-02T09:42:07-04:00",
       "src"=>"https://cdn.shopify.com/s/files/1/0842/7077/products
       /41ooqKhDYRL._UL1500.jpeg?v=1443793327",
       "variant_ids"=>[]},
     @persisted=true,
     @prefix_options={:product_id=>2156425793}>],
  "image"=>
   #<ShopifyAPI::Image:0x007f61f4598050
    @attributes=
     {"id"=>5116928641,
      "position"=>1,
      "created_at"=>"2015-10-02T09:42:07-04:00",
      "updated_at"=>"2015-10-02T09:42:07-04:00",
      "src"=>"https://cdn.shopify.com/s/files/1/0842/7077/products
       /41ooqKhDYRL._UL1500.jpeg?v=1443793327",
      "variant_ids"=>[]},
    @persisted=true,
    @prefix_options={:product_id=>2156425793}>},
@persisted=true,
@prefix_options={}>],
@original_params={:title=>"Hue Women's Python Net Tight"},
@resource_class=ShopifyAPI::Product>

The response I get from searching for a product via the Shopify API is of the class:

ActiveResource::Collection

I first tried turning that to JSON with .to_json but thats just a string and I can't loop thru it easily.

I then tried to turn it to an array with .to_a but now I can't get into the data..

The original response is in the x variable and now its an array.. If I try

x[0] - I get the original response back
x[1] - nil
X[0]["id] - NoMethodError: undefined method `[]' for 
            #<ShopifyAPI::Product:0x007f61f45c3638>
x["id"] - TypeError: no implicit conversion of String into Integer
x[0][0] - NoMethodError: undefined method `[]' for 
          #<ShopifyAPI::Product:0x007f61f45c3638>
x[0].class - Shopify::Product which is a ActiveResource::Collection
x[0].to_array - NoMethodError: undefined method `to_array' for 
                #<ShopifyAPI::Product:0x007f61f45c3638>
ToddT
  • 3,084
  • 4
  • 39
  • 83

1 Answers1

1

The data you show is an array of Product instances. Simply take the data, which I'll call data, and loop through it to get each product.

data.each do |product|

  puts product.title
  puts product.vendor

end

You can obviously extend your functionality from here.

Phil
  • 2,797
  • 1
  • 24
  • 30
  • ok, thanks for pointing me in the right direction.. your answer doesn't work, but this does data[0].title and THAT gets me to the title.. geez.. and to get to the variants it looks like x[0].variants[0].id if someone else runs into this.. – ToddT Oct 02 '15 at 15:30
  • I can't imagine why my code snippet didn't work, as your `data[0].title` example is just what this does the first time around the loop. But I'm pleased you got it to work. Maybe you could comment what didn't work with my snippet so I can correct it for future reference. – Phil Oct 02 '15 at 15:51
  • You know I wasn't looping thru it and instead just using the terminal to access the first element, so yes your answer is correct! Don't know what I was thinking! – ToddT Oct 05 '15 at 15:57