-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram_client.rb
66 lines (52 loc) · 1.51 KB
/
anagram_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
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
require 'json'
require 'optparse'
require 'net/http'
class AnagramClient
def initialize(args=[])
options = parse_opts(args)
@dictionary = options[:dictionary] || 'dictionary.txt'
@host = options[:host] || 'localhost'
@port = options[:port] || '3000'
end
def build_uri(path, query=nil)
URI::HTTP.new('http', nil, @host, @port, nil, path, nil, query, nil)
end
def hello()
puts "hello"
end
def post(path, query=nil, body=nil)
puts "Post"
uri = build_uri(path, query)
req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
req.body = body.to_json
http = Net::HTTP.new(uri.host, uri.port)
res = http.request(req)
end
def get(path, query=nil)
Net::HTTP.get_response(build_uri(path, query))
end
def delete(path)
uri = build_uri(path)
req = Net::HTTP::Delete.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
http = Net::HTTP.new(uri.host, uri.port)
res = http.request(req)
end
private
def parse_opts(args)
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby anagram_test.rb -- [options]"
opts.on("-n", "--hostname HOSTNAME", "defaults to localhost") do |h|
options[:host] = h
end
opts.on("-p", "--port PORT_NUMBER", "defaults to 3000") do |p|
options[:port] = p
end
opts.on_tail("-h", "--help", "Show help message") do
puts opts
exit
end
end.parse!(args)
options
end
end