rubyarraysruby-on-rails-4hashfql.multiquery

Compare three arrays of hashes and get the result without duplicates in ruby?


I m using the fql gem to retrieve the data from facebook. The original array of hashes is like this. Here. When i compare these three arrays of hashes then i want to get the final result in this way:

{
      "photo" => [
        [0] {
                   "owner" : "1105762436",
                 "src_big" : "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xap1/t31.0-8/q71/s720x720/10273283_10203050474118531_5420466436365792507_o.jpg",
                 "caption" : "Rings...!!\n\nView Full Screen.",
                 "created" : 1398953040,
                "modified" : 1398953354,
               "like_info" : {
                  "can_like" : true,
                "like_count" : 22,
                "user_likes" : true
            },
            "comment_info" : {
                  "can_comment" : true,
                "comment_count" : 2,
                "comment_order" : "chronological"
            },
               "object_id" : "10203050474118531",
                     "pid" : "4749213500839034982"
        }
    ],
    "comment" => [
        [0] {
                 "text" : "Wow",
            "text_tags" : [],
                 "time" : 1398972853,
                "likes" : 1,
               "fromid" : "100001012753267",
            "object_id" : "10203050474118531"
        },
        [1] {
                 "text" : "Woww..",
            "text_tags" : [],
                 "time" : 1399059923,
                "likes" : 0,
               "fromid" : "100003167704574",
            "object_id" : "10203050474118531"
        }
    ],
     "users" =>[
         [0] {
                  "id": "1105762436",
                "name": "Nilanjan Joshi",
            "username": "NilaNJan219"
         },
         [1] {
                  "id": "1105762436",
                "name": "Ashish Joshi",
            "username": "NilaNJan219"
         }
    ]
}

Here is my attempt:

datas = File.read('source2.json')
all_data = JSON.parse(datas)
photos = all_data[0]['fql_result_set'].group_by{|x| x['object_id']}.to_a 
comments = all_data[1]['fql_result_set'].group_by{|x| x['object_id']}.to_a 
@photos_comments = []
@comments_users = []
@photo_users = []
photos.each do |a|
 comments.each do |b|
   if a.first == b.first
    @photos_comments << {'photo' => a.last, 'comment' => b.last}   
   else
    @comments_users << {'photo' => a.last, 'comment' => ''} unless @photos_comments.include? (a.last)
   end
 end
end

@photo_users = @photos_comments | @comments_users
@photo_comment_users = {photos_comments: @photo_users }

Here is what i'm getting final result

Still there are duplicates in the final array. I've grouped by the array by object id which is common between the photo and the comment array. But the problem it is only taking those photos which has comments. I'm not getting the way how to find out the photos which don't have the comments. Also in order to find out the details of the person who has commented, ive users array and the common attribute between comments and users is fromid and id. I'm not able to understand how to get the user details also.


Solution

  • I think this is what you want:

    photos = all_data[0]['fql_result_set']
    comments = all_data[1]['fql_result_set'].group_by{|x| x['object_id']}
    @photo_comment_users = photos.map do |p| 
      { 'photo' => p, 'comment' => comments[p['object_id']] || '' }
    end
    

    For each photo it takes all the comments with the same object_id, or if none exist - returns ''.


    If you want to connect the users too, you can map them by id, and select the relevant ones by the comment:

    users = Hash[all_data[2]['fql_result_set'].map {|x| [x['id'], x]}]
    @photo_comment_users = photos.map do |p| 
      { 'photo' => p, 'comment' => comments[p['object_id']] || '', 
        'user' => (comments[p['object_id']] || []).map {|c| users[c['formid']]} }
    end