-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp_client.rb
42 lines (35 loc) · 964 Bytes
/
http_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
module KOSapiClient
class HTTPClient
def initialize(http_adapter, preprocessor = ResponsePreprocessor.new, converter = ResponseConverter.new)
@http_adapter = http_adapter
@preprocessor = preprocessor
@converter = converter
end
def send_request(verb, url, options = {})
absolute_url = get_absolute_url(url)
result = @http_adapter.send_request(verb, absolute_url, options)
process_response(result)
end
def process_response(result)
preprocessed = @preprocessor.preprocess(result)
response = KOSapiClient::KOSapiResponse.new preprocessed
@converter.convert response, create_context
end
def get_absolute_url(url)
if is_absolute(url)
url
else
"#{@http_adapter.base_url}/#{url}"
end
end
private
def is_absolute(url)
url.start_with?('http')
end
def create_context
{
client: self
}
end
end
end