Revert "Revert "Merge pull request #4552 from tchak/champ-communes""

This reverts commit 4373cb22cb.
This commit is contained in:
Paul Chavard 2020-01-14 18:46:07 +01:00
parent 8dd2ecfd2c
commit 14295db9ad
63 changed files with 260 additions and 1119 deletions

View file

@ -1,33 +0,0 @@
class ApiAdresse::Adapter
private
def initialize(address, limit, blank_return)
@address = address
@limit = limit
@blank_return = blank_return
end
def features
@features ||= get_features
end
def get_features
response = ApiAdresse::API.call(@address, @limit)
result = JSON.parse(response)
result['features']
rescue RestClient::Exception, JSON::ParserError, TypeError
@blank_return
end
def handle_result
if features.present?
process_features
else
@blank_return
end
end
def process_features
raise NoMethodError
end
end

View file

@ -1,17 +0,0 @@
class ApiAdresse::AddressAdapter < ApiAdresse::Adapter
def initialize(address)
super(address, 5, [])
end
def get_suggestions
handle_result
end
private
def process_features
features.map do |feature|
feature['properties']['label']
end
end
end

View file

@ -1,12 +0,0 @@
class ApiAdresse::API
def self.call(address, limit = 1)
search_url = [API_ADRESSE_URL, "search"].join("/")
RestClient::Request.execute(method: :get,
url: search_url,
timeout: 5,
headers: { params: { q: address, limit: limit } })
rescue RestClient::ServiceUnavailable
nil
end
end

View file

@ -1,15 +0,0 @@
class ApiAdresse::PointAdapter < ApiAdresse::Adapter
def initialize(address)
super(address, 1, nil)
end
def geocode
handle_result
end
private
def process_features
RGeo::GeoJSON.decode(features[0]['geometry'], json_parser: :json)
end
end

View file

@ -1,57 +0,0 @@
class ApiGeo::API
TIMEOUT = 15
CACHE_DURATION = 1.day
def self.regions
url = [API_GEO_URL, "regions"].join("/")
call(url, { fields: :nom })
end
def self.departements
url = [API_GEO_URL, "departements"].join("/")
call(url, { fields: :nom })
end
def self.pays
parse(File.open('app/lib/api_geo/pays.json').read)
end
def self.search_rpg(geojson)
url = [API_GEO_SANDBOX_URL, "rpg", "parcelles", "search"].join("/")
call(url, geojson, :post)
end
private
def self.parse(body)
JSON.parse(body, symbolize_names: true)
end
def self.call(url, body, method = :get)
# The cache engine is stored, because as of Typhoeus 1.3.1 the cache engine instance
# is included in the computed `cache_key`.
# (Which means that when the cache instance changes, the cache is invalidated.)
@typhoeus_cache ||= Typhoeus::Cache::SuccessfulRequestsRailsCache.new
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' } : {}),
cache: @typhoeus_cache,
cache_ttl: CACHE_DURATION
).run
if response.success?
parse(response.body)
else
nil
end
end
end

View file

@ -1,27 +0,0 @@
class ApiGeo::RPGAdapter
def initialize(coordinates)
@coordinates = GeojsonService.to_json_polygon_for_rpg(coordinates)
end
def data_source
@data_source ||= ApiGeo::API.search_rpg(@coordinates)
end
def results
data_source[:features].map do |feature|
feature[:properties]
.stringify_keys
.transform_keys(&:underscore)
.symbolize_keys
.slice(
:culture,
:code_culture,
:surface,
:bio
).merge({
geometry: feature[:geometry],
geo_reference_id: feature[:properties][:id]
})
end
end
end