/
response.rb
50 lines (39 loc) · 1.17 KB
/
response.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
class Bark
class Response
# All OT responses are in json format. The json is stored here as a hash (dictionary).
attr_reader :json
def initialize(request: {})
raise 'No request passed' if request == {} || request.nil?
# warn Bark::Error, 'Warning request is not valid, making it anyway.' if !request.valid?
@json = {}
if [:get_study, :get_study_tree ].include?( request.method )
req = Net::HTTP::Get.new(request.uri)
else
req = Net::HTTP::Post.new(request.uri, initheader = {'Content-Type' =>'application/json'})
end
res = Net::HTTP.start(request.uri.hostname, request.uri.port) do |http|
req.body = request.json_payload
http.request(req)
end
parse_json(res.body)
end
# Parse the json, and store it in @json.
def parse_json(string)
begin
@json = JSON.parse(string)
rescue JSON::ParserError => e
puts e.message
ap request
end
end
def request_succeeded?
!@json['exception'] && !@json['error']
end
def request_failure_message
@json['message']
end
def result
@json
end
end
end