This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
/
movie.rb
executable file
·66 lines (57 loc) · 1.51 KB
/
movie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
# CLI app that searched movies and returns info
require 'json'
require 'uri'
require 'net/http'
# Word wrapping for consise output
def wrap(s, width = 78)
s.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n| ")
end
def movie
# This is a live API key, don't absue it
api_key = ENV['OMDBAPI_API_KEY'] || '946f500a'
movie_name = ARGV
# Program escape statements
if movie_name == 'quit' || movie_name == 'exit'
puts
exit(1)
else
uri = URI("http://www.omdbapi.com/?t=#{movie_name}&apikey=#{api_key}")
response = Net::HTTP.get(uri)
info = JSON.parse(response)
end
# Exception for invalid response
if info['Response'] == 'False'
puts
puts 'No Movie Found'
puts
exit(1)
else
# Rescue if no tomato score
# Word wrap added to plot and actors
begin
title = info['Title']
year = info['Year']
score = info['Ratings'][1]['Value']
rescue
score = 'No Score Found'
end
rated = info['Rated']
genre = info['Genre']
director = info['Director']
actors = wrap(info['Actors'], 48)
plot_unformatted = info['Plot']
plot = wrap(plot_unformatted, 48)
puts '=================================================='
puts "| Title: #{title}"
puts "| Year: #{year}"
puts "| Tomato: #{score}"
puts "| Rated: #{rated}"
puts "| Genre: #{genre}"
puts "| Director: #{director}"
puts "| Actors: #{actors}"
puts "| Plot: #{plot}"
puts '=================================================='
end
end
movie