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
|
# File 'lib/dadata_ru/http_client/base.rb', line 7
def submit url:, headers: {}, params: {}, method: 'GET', type: 'json', body: ''
uri = URI.parse(url)
uri.query = URI.encode_www_form(params) unless params == {}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.read_timeout = 10
.merge! case type
when 'json'
{'Content-Type' => 'application/json'}
when 'text'
{'Content-Type' => 'text/html'}
else
raise 'unknown type'
end
response = case method
when 'GET'
http.get(uri.request_uri, )
when 'POST'
http.post(uri.request_uri, body, )
else
raise 'Unknown http method'
end
raise "API error #{response.code}" if response.code != "200"
result = case type
when 'json'
JSON.parse(response.body)
when 'text'
response.body
end
OpenStruct.new({status: "success", result: result})
rescue => error
OpenStruct.new({ status: "error", result: error })
end
|