-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rb
27 lines (21 loc) · 976 Bytes
/
client.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
require 'net/http'
require 'json'
require 'terminal-table'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on('-f', '--file FILE', 'File with csv batting data')
opts.on('-y', '--year YEAR', 'Year for filtering results')
opts.on('-t', '--team-name TEAM_NAME', 'Team name for filtering results')
end.parse!(into: options)
return puts 'File missing. Use -f file_path.' if options[:file].nil?
url = URI("http://localhost:4567/api/v1/batting")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)
form_data = [['file', File.open(options[:file])]]
form_data << ['year', options[:year]] if options[:year]
form_data << ['year', options[:team_name]] if options[:team_name]
request.set_form form_data, 'multipart/form-data'
response = http.request(request)
parsed_response = JSON.parse(response.read_body)
puts Terminal::Table.new rows: parsed_response.map(&:values), headings: ['PlayerID', 'yearId','Team name(s)', 'Batting Average']