-
Notifications
You must be signed in to change notification settings - Fork 0
APIS and JSON
to get a JSON object from an api we can use httparty (gem) and json (gem)
once installed in our .rb file we
require 'httparty'
require 'json'
Example
def get_something
@response = HTTparty.get("url")
JSON.parse(@response)
end
Examples of pulling key/values to the view
<%= @response["adult"] %>
this would display the value for the key "adult"
Examples of pulling out multiple key/values
<%= @response["adult"] %>
<%= @response["genres"].first["name"] %> #gets first record of an array within a key
Examples of pulling multiple values
<% @search["results"].each do |m| %>
<%= m["original_title"] %><br>
<% end %>
In this example this is the data returned in json format
{"header"=>{"status_code"=>200, "execute_time"=>0.089110136032104, "available"=>1982}, "body"=>{"track_list"=>[{"track"=>{"track_id"=>2532355, "track_mbid"=>"1a136a60-3841-4356-9f45-ec7c3cb1e0c8", "track_name"=>"Supernatural", "track_rating"=>38, "track_length"=>313, "commontrack_id"=>25634, "instrumental"=>0, "has_lyrics"=>1, "has_subtitles"=>0, "lyrics_id"=>1590099, "subtitle_id"=>0, "album_id"=>10425756, "album_name"=>"Cherish", "artist_id"=>61, "artist_mbid"=>"79239441-bfd5-4981-a70c-55c3f15c1287", "artist_name"=>"Madonna", "album_coverart_100x100"=>"http://static.musixmatch.com/images/albums/3/5/1/1/3/7/11731153.jpg", "album_coverart_350x350"=>"", "album_coverart_500x500"=>"", "album_coverart_800x800"=>"", "updated_time"=>"2011-06-10T14:53:02Z"}}, {"track"=>{"track_id"=>4350662, "track_mbid"=>"3eb862b8-ec0d-463d-9b95-215a4444a7d1", "track_name"=>"Let It Will Be", "track_rating"=>87, "track_length"=>259, "commontrack_id"=>25337, "instrumental"=>0, "has_lyrics"=>1, "has_subtitles"=>0, "lyrics_id"=>3427961, "subtitle_id"=>0, "album_id"=>10578775, "album_name"=>"Confessions on a Dance Floor", "artist_id"=>61, "artist_mbid"=>"79239441-bfd5-4981-a70c-55c3f15c1287", "artist_name"=>"Madonna", "album_coverart_100x100"=>"http://static.musixmatch.com/images/albums/3/7/9/2/4/4/11442973.jpg", "album_coverart_350x350"=>"http://static.musixmatch.com/images/albums/3/7/9/2/4/4/11442973_350_350.jpg", "album_coverart_500x500"=>"http://static.musixmatch.com/images/albums/3/7/9/2/4/4/11442973_500_500.jpg", "album_coverart_800x800"=>"http://static.musixmatch.com/images/albums/3/7/9/2/4/4/11442973_800_800.jpg", "updated_time"=>"2011-07-02T06:11:24Z"}},
What we need to do is pull out the objects and nest through until you get the the key you want. so in this example the objects are ["body"] and ["track_list"]. This then has another nested object within it called ["track"].. Then to access the key we need to ask for ["album_name"] in this example
So to call the first album when returning a search of madonna in this case we can nest through like so
<%= @results["body"]["track_list"].first["track"]["album_name"]%>
This will output the first album name
To output every album name we would arrange our block like so
<% @results["body"]["track_list"].each do |a| %>
<%= a["track"]["album_name"] %>
<% end %>