HTTParty
An old article from when I was in Omaha Code School. Playing with an API.
Implementing HTTParty in Sinatra
In this article we will be covering how to implement the HTTParty Ruby Gem to view movie posters found on http://www.omdbapi.com/
In this article we will be accessing the OMDb API via JSON request. Our example movie will be ‘Top Gun’.
Note: API (application program interface) is a set of routines, protocols, and tools for building software applications. The API specifies how software components should interact and APIs are used when programming graphical user interface (GUI) components.
JSON defintion: JavaScript Object Notation
The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.
- Install
In terminal enter:
gem install httparty
Then in the Gemfile include:
gem 'httparty'
- Access the API
Using our example movie “Top Gun”.
http://www.omdbapi.com/?t=Top+Gun&y=&plot=short&r=json
Here we have chosen a ‘json’ response, which returns the following keys and values:
{"Title":"Top Gun","Year":"1986","Rated":"PG","Released":"16 May 1986","Runtime":"110 min","Genre":"Action, Drama, Romance","Director":"Tony Scott","Writer":"Jim Cash, Jack Epps Jr., Ehud Yonay (magazine article \"Top Guns\")","Actors":"Tom Cruise, Kelly McGillis, Val Kilmer, Anthony Edwards","Plot":"As students at the United States Navy's elite fighter weapons school compete to be best in the class, one daring young pilot learns a few things from a civilian instructor that are not taught in the classroom.","Language":"English","Country":"USA","Awards":"Won 1 Oscar. Another 9 wins & 5 nominations.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTY3ODg4OTU3Nl5BMl5BanBnXkFtZTYwMjI1Nzg4._V1_SX300.jpg","Metascore":"N/A","imdbRating":"6.9","imdbVotes":"213,242","imdbID":"tt0092099","Type":"movie","Response":"True"}
In this example we are only interested in the poster image, whose key and value are returned as Strings:
"Poster":"http://ia.media-imdb.com/images/M/MV5BMTY3ODg4OTU3Nl5BMl5BanBnXkFtZTYwMjI1Nzg4._V1_SX300.jpg"
- Post movie poster link to our movies database
In this example, the only scenario in which we are required to post a movie poster link to the database is when a movie is being reviewed for the first time.
In order to do this, we defined the following controller action in our movies_controller.rb file.
# Creates new movie object in movies table
# Retrieves the poster for movie title from api
# Saves new movie title and poster link
MyApp.post "/submit_new_movie" do
@api = HTTParty.get("http://www.omdbapi.com/?t=#{params['title']}&y=&plot=short&r=json")
@movie = Movie.new
@movie.title = params[:title]
@movie.image_name = @api["Poster"]
@movie.save
redirect "/review_movie/#{@movie.id}"
end
On the front-end, a user submits a search by movie title String. Notice how we pass params[:title]
into our HTTParty get request and set the result as the variable @api.
We then simply post the image_name as the @api["Poster"]
String.
Now the image_name String in our movies database is set to the poster link in OMDb API.
4. Display the movie poster
To display the image in a user’s view, all we have to do is specify the Movie object in the corresponding controller action.
@movie = Movie.find_by_id(params[:id])
Then in the view file we include the following code wherever we want the poster image displayed.:
<%= @movie.image_name %>