Skip to content

Instantly share code, notes, and snippets.

@oferreiro
Last active January 1, 2016 04:19
Show Gist options
  • Save oferreiro/8091355 to your computer and use it in GitHub Desktop.
Save oferreiro/8091355 to your computer and use it in GitHub Desktop.
require "json"
require "hashie"
instagram_json='{
"data": {
"type": "image",
"users_in_photo": [{
"user": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"position": {
"x": 0.315,
"y": 0.9111
}
}],
"filter": "Walden",
"tags": [],
"comments": {
"data": [{
"created_time": "1279332030",
"text": "Love the sign here",
"from": {
"username": "mikeyk",
"full_name": "Mikey Krieger",
"id": "4",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg"
},
"id": "8"
},
{
"created_time": "1279341004",
"text": "Chilako taco",
"from": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"id": "3"
}],
"count": 2
},
"caption": null,
"likes": {
"count": 1,
"data": [{
"username": "mikeyk",
"full_name": "Mikeyk",
"id": "4",
"profile_picture": "..."
}]
},
"link": "http://instagr.am/p/D/",
"user": {
"username": "kevin",
"full_name": "Kevin S",
"profile_picture": "...",
"bio": "...",
"website": "...",
"id": "3"
},
"created_time": "1279340983",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_7.jpg",
"width": 612,
"height": 612
}
},
"id": "3",
"location": null
}
}'
hr = "-------------------------------------------"
instagram_hash = JSON.parse instagram_json
@instagram = Hashie::Mash.new instagram_hash
puts @instagram
puts hr
puts @instagram.data
puts hr
puts @instagram.data.comments
puts hr
puts @instagram.data.comments.data
puts hr
puts "each in @instagram.data.comments"
puts hr
@instagram.data.comments.data.each do |comment|
puts comment
puts hr
end
puts "each in @instagram.data.comments puts comment.from.username"
puts hr
@instagram.data.comments.data.each do |comment|
puts comment.from.username
puts hr
end
puts @instagram.data.comments.data[0].from.username
puts hr
puts @instagram.data.comments.data[1].from.username
puts hr
# Error. Don't exists…
# puts instagram.data.comments.data[2].from.username
puts "User data"
puts hr
puts @instagram.data.user
puts hr
puts "User username"
puts @instagram.data.user.username
irb(main):001:0> require "./instagr_json_to_hashie.rb"
#<Hashie::Mash data=#<Hashie::Mash caption=nil comments=#<Hashie::Mash count=2 data=[#<Hashie::Mash created_time="1279332030" from=#<Hashie::Mash full_name="Mikey Krieger" id="4" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg" username="mikeyk"> id="8" text="Love the sign here">, #<Hashie::Mash created_time="1279341004" from=#<Hashie::Mash full_name="Kevin S" id="3" profile_picture="..." username="kevin"> id="3" text="Chilako taco">]> created_time="1279340983" filter="Walden" id="3" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_5.jpg" width=150>> likes=#<Hashie::Mash count=1 data=[#<Hashie::Mash full_name="Mikeyk" id="4" profile_picture="..." username="mikeyk">]> link="http://instagr.am/p/D/" location=nil tags=[] type="image" user=#<Hashie::Mash bio="..." full_name="Kevin S" id="3" profile_picture="..." username="kevin" website="..."> users_in_photo=[#<Hashie::Mash position=#<Hashie::Mash x=0.315 y=0.9111> user=#<Hashie::Mash full_name="Kevin S" id="3" profile_picture="..." username="kevin">>]>>
-------------------------------------------
#<Hashie::Mash caption=nil comments=#<Hashie::Mash count=2 data=[#<Hashie::Mash created_time="1279332030" from=#<Hashie::Mash full_name="Mikey Krieger" id="4" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg" username="mikeyk"> id="8" text="Love the sign here">, #<Hashie::Mash created_time="1279341004" from=#<Hashie::Mash full_name="Kevin S" id="3" profile_picture="..." username="kevin"> id="3" text="Chilako taco">]> created_time="1279340983" filter="Walden" id="3" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_6.jpg" width=306> standard_resolution=#<Hashie::Mash height=612 url="http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_7.jpg" width=612> thumbnail=#<Hashie::Mash height=150 url="http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_5.jpg" width=150>> likes=#<Hashie::Mash count=1 data=[#<Hashie::Mash full_name="Mikeyk" id="4" profile_picture="..." username="mikeyk">]> link="http://instagr.am/p/D/" location=nil tags=[] type="image" user=#<Hashie::Mash bio="..." full_name="Kevin S" id="3" profile_picture="..." username="kevin" website="..."> users_in_photo=[#<Hashie::Mash position=#<Hashie::Mash x=0.315 y=0.9111> user=#<Hashie::Mash full_name="Kevin S" id="3" profile_picture="..." username="kevin">>]>
-------------------------------------------
#<Hashie::Mash count=2 data=[#<Hashie::Mash created_time="1279332030" from=#<Hashie::Mash full_name="Mikey Krieger" id="4" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg" username="mikeyk"> id="8" text="Love the sign here">, #<Hashie::Mash created_time="1279341004" from=#<Hashie::Mash full_name="Kevin S" id="3" profile_picture="..." username="kevin"> id="3" text="Chilako taco">]>
-------------------------------------------
#<Hashie::Mash created_time="1279332030" from=#<Hashie::Mash full_name="Mikey Krieger" id="4" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg" username="mikeyk"> id="8" text="Love the sign here">
#<Hashie::Mash created_time="1279341004" from=#<Hashie::Mash full_name="Kevin S" id="3" profile_picture="..." username="kevin"> id="3" text="Chilako taco">
-------------------------------------------
each in @instagram.data.comments
-------------------------------------------
#<Hashie::Mash created_time="1279332030" from=#<Hashie::Mash full_name="Mikey Krieger" id="4" profile_picture="http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg" username="mikeyk"> id="8" text="Love the sign here">
-------------------------------------------
#<Hashie::Mash created_time="1279341004" from=#<Hashie::Mash full_name="Kevin S" id="3" profile_picture="..." username="kevin"> id="3" text="Chilako taco">
-------------------------------------------
each in @instagram.data.comments puts comment.from.username
-------------------------------------------
mikeyk
-------------------------------------------
kevin
-------------------------------------------
mikeyk
-------------------------------------------
kevin
-------------------------------------------
User data
-------------------------------------------
#<Hashie::Mash bio="..." full_name="Kevin S" id="3" profile_picture="..." username="kevin" website="...">
-------------------------------------------
User username
kevin
=> true
irb(main):002:0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment