Skip to content

Instantly share code, notes, and snippets.

@stujo
Created October 17, 2014 22:46
Show Gist options
  • Save stujo/c28d2b297b4c76139d71 to your computer and use it in GitHub Desktop.
Save stujo/c28d2b297b4c76139d71 to your computer and use it in GitHub Desktop.
OMDBAPI Single File Example
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