app/models/apidae/selection.rb in apidae-0.7.8 vs app/models/apidae/selection.rb in apidae-0.8.0
- old
+ new
@@ -6,12 +6,10 @@
has_many :objects, class_name: 'Apidae::Obj', source: :apidae_object, through: :apidae_selection_objects
belongs_to :apidae_project, optional: true, class_name: 'Apidae::Project', foreign_key: :apidae_project_id
AGENDA_ENDPOINT = 'agenda/detaille/list-identifiants'
SELECTION_ENDPOINT = 'recherche/list-identifiants'
- OBJECTS_ENDPOINT = 'recherche/list-objets-touristiques'
-
MAX_COUNT = 100
MAX_LOOPS = 10
validates_presence_of :apidae_id, :reference
before_validation :generate_reference, on: :create
@@ -29,15 +27,11 @@
added = imported_objs - current_objs
removed = current_objs - imported_objs
added.each do |o|
obj = Obj.find_by_apidae_id(o)
- if obj
- SelectionObject.create(apidae_selection_id: apidae_sel.id, apidae_object_id: obj.id)
- else
- logger.error "Object #{o} referenced in selection #{apidae_sel.apidae_id} and project #{apidae_sel.apidae_project.apidae_id} is unknown"
- end
+ SelectionObject.create(apidae_selection_id: apidae_sel.id, apidae_object_id: obj.id)
end
removed_ids = Obj.where(apidae_id: removed).map {|o| o.id}
SelectionObject.where(apidae_selection_id: apidae_sel.id, apidae_object_id: removed_ids).delete_all
end
@@ -70,41 +64,10 @@
$apidae_cache.write(key, res)
end
res
end
- def api_objects(opts = {})
- key = cache_key(:objects)
- res = $apidae_cache.read(key)
- unless res
- query_args = build_args(OBJECTS_ENDPOINT, opts.merge({selection_ids: [apidae_id]}))
- res = query_objects_api(query_args, true)
- $apidae_cache.write(key, res)
- end
- res
- end
-
- def api_object(apidae_obj_id)
- query_args = build_args(OBJECTS_ENDPOINT, {obj_ids: [apidae_obj_id], fields: ["@all"]})
- query_objects_api(query_args, true)
- end
-
- def refresh_obj(apidae_obj_id)
- res = api_object(apidae_obj_id)
- if res[:results] && res[:results].length == 1
- obj_data = res[:results].first.deep_symbolize_keys
- obj = Obj.find_by_apidae_id(apidae_obj_id)
- if obj
- refreshed = Obj.update_object(obj, obj_data)
- if refreshed && Rails.application.config.respond_to?(:apidae_obj_refresh_callback)
- Rails.application.config.apidae_obj_refresh_callback.call(apidae_obj_id)
- end
- refreshed
- end
- end
- end
-
def as_text
"#{label} (#{apidae_id})"
end
private
@@ -141,36 +104,10 @@
else
result[:results].blank? ? 0 : result[:results].length
end
end
- def query_objects_api(query_args, all_results = false)
- query_result = {}
-
- if all_results
- loops = 0
- query_args[:first] = 0
- query_args[:count] = MAX_COUNT
- response = JSON.parse get_response(query_args), symbolize_names: false
- total = response['numFound']
- query_result[:results] = response['objetsTouristiques'] || {}
-
- while total > results_count(query_result) && loops < MAX_LOOPS
- loops += 1
- query_args[:first] += MAX_COUNT
- response = JSON.parse get_response(query_args), symbolize_names: false
- merge_objects_results(response, query_result)
- end
- query_result[:count] = total
- else
- response = JSON.parse get_response(query_args), symbolize_names: false
- query_result[:results] = response['objetsTouristiques'] || {}
- query_result[:count] = response['numFound']
- end
- query_result
- end
-
def get_response(args)
response = ''
query = JSON.generate args.except(:url)
logger.info "Apidae API query : #{args[:url]}?query=#{query}"
open("#{args[:url]}?query=#{CGI.escape query}") { |f|
@@ -194,27 +131,17 @@
result[:results] += ids
end
end
end
- def merge_objects_results(response, result)
- objects = response['objetsTouristiques']
- unless objects.blank?
- if result[:results] && result[:results].is_a?(Array)
- result[:results] += objects
- end
- end
- end
-
def build_args(endpoint, opts = {})
{
url: "#{Rails.application.config.apidae_api_url}/#{endpoint}",
apiKey: apidae_project ? apidae_project.api_key : '',
projetId: apidae_project.apidae_id,
first: opts[:first] || 0,
count: opts[:count] || MAX_COUNT,
selectionIds: opts[:selection_ids],
- identifiants: opts[:obj_ids],
dateDebut: opts[:from],
dateFin: opts[:to],
center: opts[:lat] && opts[:lng] ? {type: 'Point', coordinates: [opts[:lng], opts[:lat]]} : nil,
radius: opts[:radius] ? opts[:radius].to_i : nil,
responseFields: opts[:fields] || ['id']