forked from NEWROPE/scnnr-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.rb
56 lines (45 loc) · 1.39 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
51
52
53
54
55
56
# frozen_string_literal: true
module Scnnr
class Response
SUPPORTED_CONTENT_TYPE = 'application/jp.cubki.scnnr.v1+json'
def initialize(response)
raise UnexpectedError, response if response.content_type != SUPPORTED_CONTENT_TYPE
@response = response
end
def body
@body ||= @response.body
end
def parsed_body
@parsed_body ||= JSON.parse(self.body)
end
def build_recognition
raise_error_response unless @response.is_a? Net::HTTPSuccess
recognition = Recognition.new(self.parsed_body)
handle_recognition(recognition)
end
def build_coordinate
raise_error_response unless @response.is_a? Net::HTTPSuccess
Coordinate.new(self.parsed_body)
end
private
def handle_recognition(recognition)
return recognition unless recognition.error?
case recognition.error['type']
when 'unexpected-content', 'bad-request'
raise RequestFailed, recognition.error
when 'download-timeout', 'invalid-image'
raise RecognitionFailed, recognition
else raise UnexpectedError, @response
end
end
def raise_error_response
case @response
when Net::HTTPNotFound
raise RecognitionNotFound, self.parsed_body
when Net::HTTPUnprocessableEntity
raise RequestFailed, self.parsed_body
else raise UnexpectedError, @response
end
end
end
end