Use Typhoeus in ApiGeo adapter
This commit is contained in:
parent
546b4b7b3d
commit
ed46e2c3be
8 changed files with 60 additions and 60 deletions
|
@ -1,23 +1,44 @@
|
|||
class ApiGeo::API
|
||||
TIMEOUT = 15
|
||||
|
||||
def self.regions
|
||||
url = [API_GEO_URL, "regions"].join("/")
|
||||
call(url)
|
||||
call(url, { fields: :nom })
|
||||
end
|
||||
|
||||
def self.departements
|
||||
url = [API_GEO_URL, "departements"].join("/")
|
||||
call(url)
|
||||
call(url, { fields: :nom })
|
||||
end
|
||||
|
||||
def self.pays
|
||||
File.open('app/lib/api_geo/pays.json').read
|
||||
parse(File.open('app/lib/api_geo/pays.json').read)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.call(url)
|
||||
RestClient.get(url, params: { fields: :nom })
|
||||
rescue RestClient::ServiceUnavailable
|
||||
nil
|
||||
def self.parse(body)
|
||||
JSON.parse(body, symbolize_names: true)
|
||||
end
|
||||
|
||||
def self.call(url, body, method = :get)
|
||||
response = Typhoeus::Request.new(
|
||||
url,
|
||||
method: method,
|
||||
params: method == :get ? body : nil,
|
||||
body: method == :post ? body : nil,
|
||||
timeout: TIMEOUT,
|
||||
accept_encoding: 'gzip',
|
||||
headers: {
|
||||
'Accept' => 'application/json',
|
||||
'Accept-Encoding' => 'gzip, deflate'
|
||||
}.merge(method == :post ? { 'Content-Type' => 'application/json' } : {})
|
||||
).run
|
||||
|
||||
if response.success?
|
||||
parse(response.body)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue