Use Typhoeus in ApiGeo adapter

This commit is contained in:
Paul Chavard 2018-10-23 15:35:03 +02:00
parent 546b4b7b3d
commit ed46e2c3be
8 changed files with 60 additions and 60 deletions

View file

@ -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

View file

@ -1,5 +1,5 @@
class Champs::DepartementChamp < Champs::TextChamp
def self.departements
JSON.parse(ApiGeo::API.departements).map { |liste| "#{liste['code']} - #{liste['nom']}" }.push('99 - Étranger')
ApiGeo::API.departements.map { |liste| "#{liste[:code]} - #{liste[:nom]}" }.push('99 - Étranger')
end
end

View file

@ -1,5 +1,5 @@
class Champs::PaysChamp < Champs::TextChamp
def self.pays
JSON.parse(ApiGeo::API.pays).pluck("nom")
ApiGeo::API.pays.pluck(:nom)
end
end

View file

@ -1,5 +1,5 @@
class Champs::RegionChamp < Champs::TextChamp
def self.regions
JSON.parse(ApiGeo::API.regions).sort_by { |e| e['nom'] }.pluck("nom")
ApiGeo::API.regions.sort_by { |e| e[:nom] }.pluck(:nom)
end
end

View file

@ -0,0 +1 @@
Typhoeus::Config.user_agent = "demarches-simplifiees.fr"

View file

@ -7,21 +7,23 @@ http_interactions:
encoding: US-ASCII
string: ''
headers:
User-Agent:
- demarches-simplifiees.fr
Accept:
- "*/*"
- application/json
Accept-Encoding:
- gzip, deflate
User-Agent:
- rest-client/2.0.0 (darwin15.6.0 x86_64) ruby/2.3.1p112
Expect:
- ''
response:
status:
code: 200
message: OK
headers:
Server:
- nginx
- nginx/1.10.3 (Ubuntu)
Date:
- Fri, 16 Dec 2016 09:25:41 GMT
- Tue, 23 Oct 2018 13:11:36 GMT
Content-Type:
- application/json; charset=utf-8
Transfer-Encoding:
@ -30,13 +32,12 @@ http_interactions:
- keep-alive
Vary:
- Accept-Encoding
- Accept-Encoding
X-Powered-By:
- Express
Access-Control-Allow-Origin:
- "*"
Etag:
- W/"cc1-lXUcIlpPmDfvrGONl0WWsQ"
- W/"cc1-jlb3C7xpXUEaq56Wojrp9rAkoH8"
Strict-Transport-Security:
- max-age=15552000
Content-Encoding:
@ -44,27 +45,7 @@ http_interactions:
body:
encoding: ASCII-8BIT
string: !binary |-
H4sIAAAAAAAAA22Wy27bMBBFfyXwJpsKqF96dOfYrZsibow4MFAUXTARGxOQ
xZYSjbpF/yfZ5w/0YyVlpDq0sxTmajg8vJzh1z+9Um9773oTVfbe9O51Lt3H
237v75suUpUSsQFjRaGkQXAYBH/IKspl9FHYWkZLo3eyvGeqEdStqIomhfsJ
CccnCRfCqFptA1VMlcmbx/sN10nCsCzL4O80CKvm8YE/Z4zaO4T6b4OQQ/ef
YD8guJN7o8G3T4YX2rpqHSgb3WyaZ7LuE+dUFDuRa8Dpk99UlLUoUAHJTTfC
yLJmgUT2Eo5e2CIN2U03POw+uU21Mc3jb65AcC5cSb/Flc275IPJsQGiVgjF
BRTT5tn5KD+/huMGBN0KHMnzidlqish7aqQNliDkmTa5fuAZDAh5pu0dDmBA
xjPTPG9ZOgm/t4YhQvWhSNbRlVYsmXA/qFJVdfMY5CDeuTDAOqQvD5fPCbRz
fQd2SG5zabCrIWnNlfuRzh4S18fmyQhb1MhLXJdF0W5trQqhgtUJ7rLMubEh
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0seyJub20iOiJWYXVjbHVzZSIsImNvZGUiOiI4NCJ9LHsibm9tIjoiVmVuZMOpZSIsImNvZGUiOiI4NSJ9LHsibm9tIjoiVmllbm5lIiwiY29kZSI6Ijg2In0seyJub20iOiJIYXV0ZS1WaWVubmUiLCJjb2RlIjoiODcifSx7Im5vbSI6IlZvc2dlcyIsImNvZGUiOiI4OCJ9LHsibm9tIjoiWW9ubmUiLCJjb2RlIjoiODkifSx7Im5vbSI6IlRlcnJpdG9pcmUgZGUgQmVsZm9ydCIsImNvZGUiOiI5MCJ9LHsibm9tIjoiRXNzb25uZSIsImNvZGUiOiI5MSJ9LHsibm9tIjoiSGF1dHMtZGUtU2VpbmUiLCJjb2RlIjoiOTIifSx7Im5vbSI6IlNlaW5lLVNhaW50LURlbmlzIiwiY29kZSI6IjkzIn0seyJub20iOiJWYWwtZGUtTWFybmUiLCJjb2RlIjoiOTQifSx7Im5vbSI6IlZhbC1kJ09pc2UiLCJjb2RlIjoiOTUifSx7Im5vbSI6Ikd1YWRlbG91cGUiLCJjb2RlIjoiOTcxIn0seyJub20iOiJNYXJ0aW5pcXVlIiwiY29kZSI6Ijk3MiJ9LHsibm9tIjoiR3V5YW5lIiwiY29kZSI6Ijk3MyJ9LHsibm9tIjoiTGEgUsOpdW5pb24iLCJjb2RlIjoiOTc0In0seyJub20iOiJNYXlvdHRlIiwiY29kZSI6Ijk3NiJ9XQ==
http_version:
recorded_at: Fri, 16 Dec 2016 09:25:37 GMT
recorded_with: VCR 3.0.3
recorded_at: Tue, 23 Oct 2018 13:11:36 GMT
recorded_with: VCR 4.0.0

View file

@ -7,21 +7,23 @@ http_interactions:
encoding: US-ASCII
string: ''
headers:
User-Agent:
- demarches-simplifiees.fr
Accept:
- "*/*"
- application/json
Accept-Encoding:
- gzip, deflate
User-Agent:
- rest-client/2.0.0 (darwin15.6.0 x86_64) ruby/2.3.1p112
Expect:
- ''
response:
status:
code: 200
message: OK
headers:
Server:
- nginx
- nginx/1.10.3 (Ubuntu)
Date:
- Fri, 16 Dec 2016 09:24:28 GMT
- Tue, 23 Oct 2018 13:11:36 GMT
Content-Type:
- application/json; charset=utf-8
Transfer-Encoding:
@ -30,13 +32,12 @@ http_interactions:
- keep-alive
Vary:
- Accept-Encoding
- Accept-Encoding
X-Powered-By:
- Express
Access-Control-Allow-Origin:
- "*"
Etag:
- W/"2dd-2SAFdhiM3SIQ2017gqYxbw"
- W/"28d-5OgIzgwL+0K2UaO0foKduqMMBrA"
Strict-Transport-Security:
- max-age=15552000
Content-Encoding:
@ -44,14 +45,7 @@ http_interactions:
body:
encoding: ASCII-8BIT
string: !binary |-
H4sIAAAAAAAAA11Ry2rDMBD8FeNLLxE0rybpzTE0F6eYHHopPSyWSASyNllJ
Abf0F/ofvucP/GO1aIvXPQm0M7MzO68fqcU6fUx3AaQyGM4qnaQVStX/3U/T
z8kfYA/ktdWXwAEzBtiFBiwfztmwgOTQtcFqtEx/MdJv0HvOf2DT7ssoIZV4
IrAVA025xVxZT0q8gEmkSgrUxJAzvmyLgY54tL+CJyVyrH3XDt5mK7b9GakG
KzWXW4/nUpTgosMcDGgnSl0BjRhzfqzMOKh68AnqM0QfGUll+7dAItD8jgtu
vITGxXAG/udbcvktKR9lhzxL3kZ2CdrHLaLQNQanrShRewzREPVXVG5grpaj
Hu0xKImVOPQ8p41BK/ZaalE21LW2a0dUXmEWropi1MOpu8XE5syx61FMwqvq
i/4Biby7eZXIu+w90GBswyPlSI7F3fRqb99L+pyA3QIAAA==
W3sibm9tIjoiR3VhZGVsb3VwZSIsImNvZGUiOiIwMSJ9LHsibm9tIjoiTWFydGluaXF1ZSIsImNvZGUiOiIwMiJ9LHsibm9tIjoiR3V5YW5lIiwiY29kZSI6IjAzIn0seyJub20iOiJMYSBSw6l1bmlvbiIsImNvZGUiOiIwNCJ9LHsibm9tIjoiTWF5b3R0ZSIsImNvZGUiOiIwNiJ9LHsibm9tIjoiw45sZS1kZS1GcmFuY2UiLCJjb2RlIjoiMTEifSx7Im5vbSI6IkNlbnRyZS1WYWwgZGUgTG9pcmUiLCJjb2RlIjoiMjQifSx7Im5vbSI6IkJvdXJnb2duZS1GcmFuY2hlLUNvbXTDqSIsImNvZGUiOiIyNyJ9LHsibm9tIjoiTm9ybWFuZGllIiwiY29kZSI6IjI4In0seyJub20iOiJIYXV0cy1kZS1GcmFuY2UiLCJjb2RlIjoiMzIifSx7Im5vbSI6IkdyYW5kIEVzdCIsImNvZGUiOiI0NCJ9LHsibm9tIjoiUGF5cyBkZSBsYSBMb2lyZSIsImNvZGUiOiI1MiJ9LHsibm9tIjoiQnJldGFnbmUiLCJjb2RlIjoiNTMifSx7Im5vbSI6Ik5vdXZlbGxlLUFxdWl0YWluZSIsImNvZGUiOiI3NSJ9LHsibm9tIjoiT2NjaXRhbmllIiwiY29kZSI6Ijc2In0seyJub20iOiJBdXZlcmduZS1SaMO0bmUtQWxwZXMiLCJjb2RlIjoiODQifSx7Im5vbSI6IlByb3ZlbmNlLUFscGVzLUPDtHRlIGQnQXp1ciIsImNvZGUiOiI5MyJ9LHsibm9tIjoiQ29yc2UiLCJjb2RlIjoiOTQifV0=
http_version:
recorded_at: Fri, 16 Dec 2016 09:24:25 GMT
recorded_with: VCR 3.0.3
recorded_at: Tue, 23 Oct 2018 13:11:36 GMT
recorded_with: VCR 4.0.0

View file

@ -4,18 +4,21 @@ describe ApiGeo::API do
describe '.regions', vcr: { cassette_name: 'api_geo_regions' } do
subject { described_class.regions }
it { expect(subject.code).to eq 200 }
it { expect(subject.size).to eq 18 }
end
describe '.departements', vcr: { cassette_name: 'api_geo_departements' } do
subject { described_class.departements }
it { expect(subject.code).to eq 200 }
it { expect(subject.size).to eq 101 }
end
describe '.pays' do
subject { described_class.pays }
let(:pays) {
JSON.parse(File.open('app/lib/api_geo/pays.json').read, symbolize_names: true)
}
it { is_expected.to eq File.open('app/lib/api_geo/pays.json').read }
it { is_expected.to eq pays }
end
end