Created
October 20, 2014 16:13
-
-
Save stujo/d06fac850d0d7fdc210e to your computer and use it in GitHub Desktop.
OMBDAPI Client Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'typhoeus' | |
require 'json' | |
OMDBAPI_URL = 'http://www.omdbapi.com/' | |
def search_movies(search_string) | |
body = JSON.parse(Typhoeus.get(OMDBAPI_URL, params: {s: search_string} ).body) | |
body['Search'] | |
end | |
get '/' do | |
<<-HTML | |
<html> | |
<body> | |
<form action="/search" method="POST"> | |
<label for="search_q">Title Search</label> | |
<input id="search_q" name="q" placeholder="Movie Title Search" type="text"/> | |
<input id="search_go" value="Search" type="submit"/> | |
</form> | |
</body> | |
</html> | |
HTML | |
end | |
post '/search' do | |
results = search_movies(params[:q]) | |
header = <<-HTML_HEADER | |
<html> | |
<body> | |
<ul> | |
HTML_HEADER | |
content = results.map(){ |movie| | |
'<li><a href="http://www.imdb.com/title/' + | |
movie['imdbID'] + '/">' + movie['Title'] + '</a></li>' | |
}.join() | |
footer = <<-HTML_FOOTER | |
</ul> | |
<div><a href="/">Search!</a></div> | |
<div><h1>Debugging</h1> | |
<pre> | |
#{results.join("\n")} | |
</pre> | |
</div> | |
</body> | |
</html> | |
HTML_FOOTER | |
header + content + footer | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment