-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.rb
61 lines (52 loc) · 1.42 KB
/
controller.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
require 'pry'
require 'json'
require 'open-uri'
require_relative 'view'
require_relative 'school_score'
require_relative 'school_score_table'
class Controller
attr_reader :view, :url, :score_data
def initialize
@view = View.new
@score_data = []
@url = 'https://data.cityofnewyork.us/resource/sg2c-qbjf.json'
run_interface
end
def run_interface
input = ""
view.display_welcome_message
until input == "exit"
input = view.get_input
command = input.split.shift
user_input = input.split[1..-1].join(' ')
case command
when 'Search'
handle_search(user_input)
when 'Top_test_takers'
handle_top_records(user_input)
when ''
else
view.display("Sorry, I don't know that command.") unless input == "exit"
end
end
view.display("End")
end
def handle_search(user_input)
matched = JSON.parse(open(url+'?$q='+user_input).read)
if matched.empty?
view.display('Please enter a valid school name to search')
else
show_results(matched)
end
end
def handle_top_records(user_input)
matched = JSON.parse(open("#{url}?$order=ap_test_takers_%20DESC&$where=ap_test_takers_%20IS%20NOT%20NULL&$limit=1").read)
show_results(matched)
end
private
def show_results(matched)
usable_scores = SchoolScore.new(matched[0])
view.display_school_info(usable_scores)
end
end
Controller.new