Using an API with Ruby on Rails in 8 Steps

Rails
Rails
Photo Credit David Herron via Upsplash
{"Title":"Starship Troopers","Year":"1997","Rated":"R","Released":"07 Nov 1997","Runtime":"129 min","Genre":"Action, Adventure, Sci-Fi, Thriller","Director":"Paul Verhoeven","Writer":"Edward Neumeier (screenplay), Robert A. Heinlein (book)","Actors":"Casper Van Dien, Dina Meyer, Denise Richards, Jake Busey","Plot":"Humans in a fascist, militaristic future wage war with giant alien bugs.","Language":"English","Country":"USA","Awards":"Nominated for 1 Oscar. Another 3 wins & 15 nominations.","Poster":"https://m.media -amazon.com/images/M/MV5BNWExNzg3MmMtYjc3MS00MzFlLWJiOWQtNWYxZTgxNjhlZTQ2XkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"7.2/10"},{"Source":"Rotten Tomatoes","Value":"64%"},{"Source":"Metacritic","Value":"51/100"}],"Metascore":"51","imdbRating":"7.2","imdbVotes":"263,248","imdbID":"tt0120201","Type":"movie","DVD":"19 May 1998","BoxOffice":"N/A","Production":"Sony Pictures Home Entertainment","Website":"N/A","Response":"True"}
http://www.omdbapi.com/?apikey=[yourkey]&
http://www.omdbapi.com/?t=Starship+Troopers
http://www.omdbapi.com/?t=Starship+Troopers&apikey=YOURKEYHERE
BASE_URL = "http://www.omdbapi.com/?t="  
API_PARTIAL_URL = "&apikey=#{ENV['OMDB_API_KEY']}"

class OmdbService

attr_reader :results

def self.search(search)
@results = HTTParty.get(BASE_URL + "#{search}" + API_PARTIAL_URL)
@results
end
"Title":"Starship Troopers","Year":"1997","Rated":"R","Released":"07 Nov 1997","Runtime":"129 min","Genre":"Action, Adventure, Sci-Fi, Thriller","Director":"Paul Verhoeven","Writer":"Edward Neumeier (screenplay), Robert A. Heinlein (book)","Actors":"Casper Van Dien, Dina Meyer, Denise Richards, Jake Busey","Plot":"Humans in a fascist, militaristic future wage war with giant alien bugs.","Language":"English","Country":"USA","Awards":"Nominated for 1 Oscar. Another 3 wins & 15 nominations.","Poster":"https://m.media -amazon.com/images/M/MV5BNWExNzg3MmMtYjc3MS00MzFlLWJiOWQtNWYxZTgxNjhlZTQ2XkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"7.2/10"},{"Source":"Rotten Tomatoes","Value":"64%"},{"Source":"Metacritic","Value":"51/100"}],"Metascore":"51","imdbRating":"7.2","imdbVotes":"263,248","imdbID":"tt0120201","Type":"movie","DVD":"19 May 1998","BoxOffice":"N/A","Production":"Sony Pictures Home Entertainment","Website":"N/A","Response":"True"}
def self.find_or_create_from_api(search)
movie = OmdbService.search(search)
@movie = Movie.find_or_create_by(title: movie["Title"],
runtime: movie["Runtime"],
genre: movie["Genre"],
release_date: movie["Released"],
poster: movie["Poster"],
actors: movie["Actors"],
plot: movie["Plot"],
imdbRating: movie["imdbRating"],
director: movie["Director"])

end
def index 
@search = params[:search]
if @search
@movie = Movie.find_or_create_from_api(params[:search])
else
@movies = Movie.all
end
end
<%= form_tag(movies_path, method: :get) do %>   
<%= text_field_tag :search, nil, placeholder: 'Search by Title' %>
<%= submit_tag ("Search")%>
<% end %>
Image for post
Image for post
Image for post
Image for post

Written by

Full Stack Developer. Designs solutions combining analytical, technical, and problem-solving skills to make ideas come to life.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store