Map object and nested object to model using Ruby on Rails - ruby-on-rails

I have an object like
{"Result":[{
"Links":[{
"UrlTo":"http://www.example.com/",
"Visited":1364927598,
"FirstSeen":1352031217,
"PrevVisited":1362627231,
"Anchor":"example.com",
"Type":"Text",
"Flag":[],
"TextPre":"",
"TextPost":""
}],
"Index":0,
"Rating":0.001416,
"UrlFrom":"http://www.exampletwo.com",
"IpFrom":"112.213.89.105",
"Title":"Example title",
"LinksInternal":91,
"LinksExternal":51,
"Size":5735
}]}
And I have a model with all of the keys.
UrlTo, Visited, FirstSeen, PrevVisited, Anchor, Type, TextPre, TextPost, Index, Rating, UrlFrom, IpFrom, Title, LinksInternal, LinksExternal, Size
I understand how to save this to the database without this bit below...
"Links":[{
"UrlTo":"http://example.com/",
"Visited":1364927598,
"FirstSeen":1352031217,
"PrevVisited":1362627231,
"Anchor":"example.com",
"Type":"Text",
"Flag":[],
"TextPre":"",
"TextPost":""
}],
Not sure how to save it with a nested object as well.
I had a search on Google and SO and couldn't find anything, what is the correct way to do this? Should I move the nested object into the one above? I have no need for it to be nested...
Thanks in advance

it looks like you want to save links, so I would loop over the Result/Links in the json provided, and create a new hash based on the links.
I've pretended below that your json is in a file called input.json -- but you'd obviously just parse the text or use an existing JSON object
require 'json'
json = JSON.parse File.read("input.json")
links = json["Result"].map do |result|
result["Links"].map {|link| link }
end.flatten
hash = {"Links" => links}
puts hash
This creates the object:
{"Links"=>[{"UrlTo"=>"http://www.example.com/", "Visited"=>1364927598, "FirstSeen"=>1352031217, "PrevVisited"=>1362627231, "Anchor"=>"example.com", "Type"=>"Text", "Flag"=>[], "TextPre"=>"", "TextPost"=>""}]}

Related

Extracting only specific members from a Ruby hash

I would like to extract only the team_member_id and display_name from the below hash returned from dropbox api.
{"members"=>[{"profile"=>{"team_member_id"=>"dbmid:AACHnGyTHHna44224Ad-vVewFDJ9nzS7be9GJ6Tc", "account_id"=>"dbid:AAD234234234x2yGXHsG3guC04Fp8QPwUF9NDO55w", "email"=>"name#domain.com", "email_verified"=>true, "secondary_emails"=>[], "status"=>{".tag"=>"active"}, "name"=>{"given_name"=>"firstname", "surname"=>"lastname", "familiar_name"=>"first", "display_name"=>"firstname lastname", "abbreviated_name"=>"MM"}, "membership_type"=>{".tag"=>"full"}, "joined_on"=>"2017-01-02T20:58:20Z", "groups"=>["g:6fe6b5a111cfc0400000000000000005", "g:6fe6b5a111cfc04000000000000015a1", "g:6fe6b5a111cfc0400000000000002299", "g:6fe6b5a111cfc04000000000000043cb", "g:6fe6b5a111cfc0400000000000056eb3", "g:6fe6b5a111cfc0400000000000058122", "g:6fe6b5a111cfc0400000000000058248", "g:6fe6b5a111cfc04000000000002dd0d9", "g:6fe6b5a111cfc04000000000002dd650"], "member_folder_id"=>"1384307556"}, "role"=>{".tag"=>"team_admin"}}], "cursor"=>"AAAXuLKhvS_T97ZjALBH4A2MCQW_xOp9bu0tAwqGuY1zBg_C-UKVZoBDTQkhU4Hok8nGT15IHN64ZE-88-dlT3242341WD8RgPBg5zfIOQqAdgJlc-Aw", "has_more"=>true}
I tried using the below code and returned nil.
puts hash['profile']['team_member_id']
What is the recommended way to extract individual data from the above hash.
hash["members"] returns an array, so you would have to do:
hash["members"][0]["profile"]["team_member_id"]
hash["members"][0]["profile"]["name"]["display_name"]
You could also take advantage of the method called dig which works on both hash and array. The advantage with this one is that it returns nil if any key is not found:
hash.dig('members', 0, 'profile', 'team_member_id')
hash.dig('members', 0, 'profile', 'name', 'display_name')

How do I access a hash value that only exists in 1 attribute out of many?

So this one is a bit tricky.
I have an attribute that looks like this:
[22] pry(main)> n.media.meta_info[:response][:outputs]
=> [{"id"=>486,
"url"=>"http://some-video.com/by-fire.mp4",
"label"=>"webmp4",
"state"=>"finished",
"format"=>"mpeg4",
"type"=>"standard",
"frame_rate"=>30.06,
{"id"=>488848287,
"url"=>"http://some-video.com/by-fire.webm",
"label"=>"webwebm",
"state"=>"finished",
"format"=>"webm",
"type"=>"standard",
"frame_rate"=>30.06,
{"id"=>488848288,
"url"=>"http://some-video.com/by-fire.ogv",
"label"=>"webogv",
"state"=>"finished",
"format"=>"ogg",
"type"=>"standard",
"frame_rate"=>30.059,
{"id"=>488848289,
"url"=>
"https://zencoder-temp-storage-us-east-1.s3.amazonaws.com/",
"label"=>nil,
"state"=>"finished",
"format"=>"mpeg4",
"type"=>"standard",
"frame_rate"=>30.06,
"thumbnails"=>
[{"label"=>nil,
"images"=>
[{"dimensions"=>"56x100",
"file_size_bytes"=>15142,
"format"=>"PNG",
"url"=>"https://some-video.s3.amazonaws.com/uploads/video/video_file/id/by-fire.png"}]}],
"md5_checksum"=>nil}]
I am trying to access the thumbnails info, specifically the URL for the thumbnails.
I can't figure out how to get there though.
When I try to go the nested hash key of thumbnails it doesn't work:
[23] pry(main)> n.media.meta_info[:response][:outputs][:thumbnails]
TypeError: no implicit conversion of Symbol into Integer
from (pry):22:in `[]'
Thoughts?
The [{ at the beginning of the output indicates that an array is returned. You first need to find a element in the array that contains a thumbnails key:
outputs = n.media.meta_info[:response][:outputs]
output_with_thumbnail = outputs.find { |elem| elem.keys.include?('thumbnails') }
Then continue like this:
output_with_thumbnail['thumbnails']
If you're just trying to find the thumbnail, and don't care about the rest of the outputs, you can use #find like so:
thumbnails = n.media.meta_info[:response][:outputs].find {|it| it[:thumbnails] }[:thumbnails]
You have an array of hashes, thumbnails are in the 3rd:
n.media.meta_info[:response][:outputs][3][:thumbnails]
It looks like
outputs = n.media.meta_info[:response][:outputs]
is an Array of hashes. So, you need to iterate over them first:
outputs.each do |output|
# deal with each output here
end
You can check for :thumbnails like so:
if (thumbnails = output[:thumbnails])
# we've got thumbnails, deal with it here
end

How to store each key or element from array to database in rails

I'm stuck on trying to store keys or elements from a returned array into the database. It looks something like:
{"id"=>"28898790358_10152709083080359",
"from"=>{"category"=>"Tv channel",
"category_list"=>[{"id"=>"169056916473899",
"name"=>"Broadcasting & Media Production"}],
"name"=>"WGRZ - Channel 2, Buffalo",
"id"=>"28898790358"}
I'm trying to grab stuff like 'id', 'category_list', all of their values and store it into a column of a database table.
I've got it before but this time only certain values get in and usually I get an error:
undefined method[]
You're getting the undefined method [] error when you try to use the [] method on a class that doesn't have that method defined. My guess is that you are running into this problem while parsing the above hash (e.g. calling [] on Nil Class, and getting the error undefined method [] for nil:NilClass).
Long Story Short: You need to parse the JSON and save the items you need to the appropriate spots in the database
I'm a little unclear about the example you gave, so I'll try to use a simplified one.
Suppose you are querying for a TvShow, which belongs_to a TvChannel. TvChannel also has_many_and_belongs_to a certain Category. So perhaps this query you are doing returns the TvShow, along with the TvChannel and Category to which the TvShow belongs. Suppose you get back something like the json below:
json_response = { 'name': 'Desperate Housewives',
'id': '12345-67890',
'tvChannel': {
'name': 'ABC',
'id': '123-456-789',
'categories': [
{ 'name': 'Comedy',
'id': '000-000' },
{ 'name': 'Drama',
'id': '111-111' }
]
}
}
So now you want to save the TvShow Desperate Housewives to the TvChannel ABC, and save ABC to the Categories Comedy and Drama. This is how I would go about parsing:
new_show = TvShow.new
new_show.name = json_response["name"]
new_show.external_id = json_response["id"] # assuming you want to save their identifier
channel = TvChannel.find_or_create_by(:name, json_response["tvChannel"]["name"]) # you might want to do this by the id, but it's up to you.
new_show.tv_channel = channel # this saves the TvChannel's ID to the new_show's tv_channel_id foreign key field
new_show.save # save your new TV Show to the database, with the name, id, and tv_channel_id.
# Next we parse the "categories" part of the JSON, finding each Category by name (creating a new Category if one doesn't exist by that name),
# And then adding our TvChannel to that Category
json_response["tvChannel"]["categories"].each do |category|
c = Category.find_or_create_by(:name, category["name'])
channel.categories << c
channel.save
end
I hoped this helped show you how to parse JSON responses, and how to use ActiveRecord to create or find instances in your database. It is useful to store the JSON response to a variable (json_response in my example above), for easier parsing.
Let me know if I can clarify anything!

Ruby On Rails: Accessing Array?

I have an array stored in a variable temp which looks like this:
temp.inspect output :
[#"Marlana Letelier", "completed_at"=>nil, "status"=>"incomplete", "name"=>nil, "lead_move_date"=>"2012-06-17 00:00:00", "archive_time"=>nil, "stop_time"=>nil, "priority"=>"2", "assigned_to_user_firstname"=>"Vanessa", "notes"=>"", "created_by_id"=>nil, "id"=>"804005", "assigned_to_id"=>"1", "dn_email_id"=>nil, "outcomes_string"=>"other", "lead_id"=>"101139", "flavor"=>"PhonecallTask", "stringified_parameters"=>"{\n'purpose' => 'continued contact attempt',\n'phone_number' => '361-946-9905',\n}", "created_at"=>"2011-12-21 13:29:07", "start_time"=>"2012-04-04 17:00:00"}>]
temp.class specifies it as an array but temp[1] doesn't output anything.
How do I access the elements ?
EDIT:
1) Temp either had nothing, 1 object or multiple objects
2) Check for nil
3) Get each object out
4) access the attributes
Although your inspect output looks wrong (I think you're missing some text that came out in <...> tags) it looks like you have an array with a single item. Verify this assumption by outputting temp.length.
Since Arrays in Ruby are 0-indexed, try temp[0] or temp.first.

Extracting JSON objects from JSON string

I want to break down a JSON string into smaller objects. I have two servers, one acting as the web-app interface to the whole application and the other is a repository/database.
I'm able to retrieve information from the repository to the web-app as JSON, but after that I don't know how to return it.
Here's a sample of the JSON being returned:
{"respPages":[{"page":{"page_url":"http://www.google.com/","created_at":"2011-08-10T11:00:19Z","website_id":1,"updated_at":"2011-08-10T11:00:19Z","id":1}},{"page":{"page_url":"http://www.blank.com/services/content_services/","created_at":"2011-08-10T11:02:46Z","website_id":1,"updated_at":"2011-08-10T11:02:46Z","id":2}}],"respSite":{"website":{"created_at":"2011-08-10T11:00:19Z","website_id":null,"updated_at":"2011-08-10T11:00:19Z","website_url":null,"id":1}},"respElementTypes":[{"element_type":{"created_at":"2011-08-10T11:00:19Z","updated_at":"2011-08-10T11:00:19Z","id":1,"tag_name":"head"}},
There are four tags in the JSON:
page
website
elementType
elementData
I would like to create four arrays and populate them with the object that matches these tags.
I would image the code is something like this:
#Get the json from repo using net/http
uri = URI.parse("http://127.0.0.1:3007/repository/infoid/1.json")
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(Net::HTTP::Get.new(uri.request_uri))
#x = response.to_hash
#pages = Array.new
#websites= Array.new
#elementDatas = Array.new
#elementTypes = Array.new
#enter code here`#For every bit of the hash, find out what it is and allocate it accordingly
#x.each_with_index do |e,index|
if e.tagName == pages #Getting real javascripty here. There must be someway to check the tag or title of the element
#pages[index]=e
end
My goal for the returned value is to have four arrays, each containing different types of objects:
#pagesArray[1]
should contain the first occurrence of a page object in the JSON string. Then do the same for the other ones.
Of course I'd need to break down the object further but once I can break down the top level and categorize them, then I can go deeper.
In the JSON there are already tag titles respPages and respWebsites which group all the objects.
How do I turn JSON back into objects in Ruby and reference them using something like the tag name?
You should be able to decode anything in JSON format using the standard JSON library:
JSON.load(...)
It will throw exceptions on malformed JSON data, so be sure to test it thoroughly and make sure it can handle all the important cases.
If you're trying to navigate the structure of the JSON itself, you probably need to write a series of recursive methods that handle each case along the way. A good pattern to start with is this:
#data.each do |key, value|
case (key)
when 'someKey'
handle_some_key(value)
when 'otherKey'
handle_other_key(value)
end
end
You can either break out the behavior into methods as in this example, or inline it if the logic is fairly straightforward.
As a note, an alternative to Array.new is simply [ ] as it is in JavaScript. For example:
#pages = [ ]
You'll see this used frequently in most Ruby examples. The alternative to Hash.new is { }.
The following works:
json = {"respPages"=>[{"page"=>{"page_url"=>"http://www.google.com", "created_at"=>"2011-08-10T11:00:19Z", "website_id"=>1, "updated_at"=>"2011-08-10T11:00:19Z", "id"=>1}}, {"page"=>{"page_url"=>"http://www.blank.com/services/content_services/", "created_at"=>"2011-08-10T11:02:46Z", "website_id"=>1, "updated_at"=>"2011-08-10T11:02:46Z", "id"=>2}}],
"respSite"=>{"website"=>{"created_at"=>"2011-08-10T11:00:19Z", "website_id"=>nil, "updated_at"=>"2011-08-10T11:00:19Z", "website_url"=>nil, "id"=>1}},
"respElementTypes"=>[{"element_type"=>{"created_at"=>"2011-08-10T11:00:19Z", "updated_at"=>"2011-08-10T11:00:19Z", "id"=>1, "tag_name"=>"head"}}]}
#respPages, #respSite, #respElementTypes = [], [], []
json.each do |key_category, group_category|
group_category.each do |hash|
if group_category.is_a? Array
eval("##{key_category}") << hash.values.first
elsif group_category.is_a? Hash
eval("##{key_category}") << hash[1]
end
end
end
there weren't any respData in your sample but you've got the idea.

Resources