0

I'd like to structure data I get pack from an Instagram API call:

{"attribution"=>nil,
 "tags"=>["loudmouth"],
 "location"=>{"latitude"=>40.7181015, "name"=>"Fontanas Bar", "longitude"=>-73.9922791, "id"=>31443955},
 "comments"=>{"count"=>0, "data"=>[]},
 "filter"=>"Normal",
 "created_time"=>"1444181565",
 "link"=>"https://instagram.com/p/8hJ-UwIDyC/",
 "likes"=>{"count"=>0, "data"=>[]},
 "images"=>
  {"low_resolution"=>{"url"=>"https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/12145134_169501263391761_636095824_n.jpg", "width"=>320, "height"=>320},
   "thumbnail"=>
    {"url"=>"https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e35/c135.0.810.810/12093266_813307028768465_178038954_n.jpg", "width"=>150, "height"=>150},
   "standard_resolution"=>
    {"url"=>"https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s640x640/sh0.08/e35/12145134_169501263391761_636095824_n.jpg", "width"=>640, "height"=>640}},
 "users_in_photo"=>
  [{"position"=>{"y"=>0.636888889, "x"=>0.398666667},
    "user"=>
     {"username"=>"ambersmelson",
      "profile_picture"=>"http://photos-h.ak.instagram.com/hphotos-ak-xfa1/t51.2885-19/11909108_1492226137759631_1159527917_a.jpg",
      "id"=>"194780705",
      "full_name"=>""}}],
 "caption"=>
  {"created_time"=>"1444181565",
   "text"=>"the INCOMPARABLE Amber Nelson closing us out! #loudmouth",
   "from"=>
    {"username"=>"alex3nglish",
     "profile_picture"=>"http://photos-f.ak.instagram.com/hphotos-ak-xaf1/t51.2885-19/s150x150/11906214_483262888501413_294704768_a.jpg",
     "id"=>"30822062",
     "full_name"=>"Alex English"}}

I'd like to structure it in this way:

    hash ={}
    hash {"item1"=>
:location => {"latitude"=>40.7181015, "name"=>"Fontanas Bar",      "longitude"=>-73.9922791, "id"=>31443955},
          :created_time => "1444181565",
          :images =>https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/12145134_169501263391761_636095824_n.jpg"
          :user =>"Alex English"}

I'm iterating over 20 objects, each with their location, images, etc... how can I get a hash structure like the one above ?

This is what I've tried:

array_images = Array.new
    # iterate through response object to extract what is needed

     response.each do |item|
        array_images << { :image => item.images.low_resolution.url,
            :location =>  item.location,:created_time => Time.at(item.created_time.to_i), :user => item.user.full_name}

     end

Which works fine. So what is the better way, the fastest one?

2
  • Please edit your question to include the code you've written so far, and an explanation of what problem you're having with that code. Commented Oct 7, 2015 at 3:37
  • Your hash at the end is not a valid hash. It looks like you nesting hashes but missing some braces. Commented Oct 7, 2015 at 3:39

1 Answer 1

1

The hash that you gave is one item in the array stored at the key "data" in a larger hash right? At least that's how it is for the tags/ endpoint so I'll assume it's the same here. (I'm referring to that array of hashes as data)

hash = {}
data.each_with_index do |h, idx|
  hash["item#{idx + 1}"] = {
    location: h["location"], #This grabs the entire hash at "location" because you are wanting all of that data
    created_time: h["created_time"],
    image: h["images"]["low_resolution"]["url"], # You can replace this with whichever resolution.
    caption: h["caption"]["from"]["full_name"]
  }
end

I feel like you want a more simple solution, but I'm not sure how that's going to happen as you want things nested at different levels and you are pulling things from diverse levels of nesting.

Sign up to request clarification or add additional context in comments.

2 Comments

What's the better way between your solution and mine?
I honestly think you don't really need to worry about which of those two is better. One thing though, yours ends with a slightly different structure than mine (and the hash that you said that you wanted) in that the code you wrote generates an array of hashes where mine creates a hash where each item is under a key. The reason that I don't think you need to worry is that you are only going through 20 items at a time and the real bottleneck will be waiting for the requests to come back from the instagram API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.