Class: Apidae::Selection

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/apidae/selection.rb

Constant Summary collapse

AGENDA_ENDPOINT =
'agenda/detaille/list-identifiants'
SELECTION_ENDPOINT =
'recherche/list-identifiants'
OBJECTS_ENDPOINT =
'recherche/list-objets-touristiques'
MAX_COUNT =
100
MAX_LOOPS =
10

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_or_update(selection_data, apidae_proj_id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/apidae/selection.rb', line 19

def self.add_or_update(selection_data, apidae_proj_id)
  apidae_sel = Selection.where(apidae_id: selection_data[:id]).first_or_initialize
  apidae_sel.label = selection_data[:nom]
  apidae_sel.apidae_project_id = apidae_proj_id
  apidae_sel.save!

  # Note : should be done with basic collection assignment, but can't make it work...
  current_objs = apidae_sel.objects.collect {|obj| obj.apidae_id}
  imported_objs = selection_data[:objetsTouristiques].blank? ? [] : selection_data[:objetsTouristiques].collect {|obj| obj[:id]}

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

Instance Method Details

#add_or_refresh(obj_data) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/apidae/selection.rb', line 133

def add_or_refresh(obj_data)
  obj = Obj.find_by_apidae_id(obj_data[:id])
  if obj
    obj = Obj.update_object(obj, obj_data, apidae_project.locales, apidae_project.versions)
  else
    obj = Obj.add_object(obj_data, apidae_project.locales, apidae_project.versions)
  end
  SelectionObject.create(apidae_selection_id: id, apidae_object_id: obj.id) unless objects.include?(obj)

  if Rails.application.config.respond_to?(:apidae_obj_refresh_callback)
    Rails.application.config.apidae_obj_refresh_callback.call(obj.apidae_id)
  end
  obj
end

#add_or_refresh_obj(apidae_obj_id, fields = ["@all"]) ⇒ Object

Note : WARNING - updated obj will only contain the provided fields



110
111
112
113
114
115
116
117
118
# File 'app/models/apidae/selection.rb', line 110

def add_or_refresh_obj(apidae_obj_id, fields = ["@all"])
  if valid_api?
    res = api_object(apidae_obj_id, fields)
    if res[:results] && res[:results].length == 1
      obj_data = res[:results].first.deep_symbolize_keys
      add_or_refresh(obj_data)
    end
  end
end

#add_or_refresh_objs(fields = ["@all"]) ⇒ Object

Note : WARNING - updated objs will only contain the provided fields



121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/apidae/selection.rb', line 121

def add_or_refresh_objs(fields = ["@all"])
  if valid_api?
    res = api_objects({fields: fields})
    if res[:results] && res[:results].length > 0
      res[:results].each do |result|
        obj_data = result.deep_symbolize_keys
        add_or_refresh(obj_data)
      end
    end
  end
end

#api_agenda(from, to) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'app/models/apidae/selection.rb', line 82

def api_agenda(from, to)
  key = cache_key(:agenda, from, to)
  res = $apidae_cache.read(key)
  unless res
    query_args = build_args(AGENDA_ENDPOINT, {selection_ids: [apidae_id], from: from, to: to, count: 200})
    res = query_api(query_args, true)
    $apidae_cache.write(key, res)
  end
  res
end

#api_object(apidae_obj_id, fields = ["@all"]) ⇒ Object



104
105
106
107
# File 'app/models/apidae/selection.rb', line 104

def api_object(apidae_obj_id, fields = ["@all"])
  query_args = build_args(OBJECTS_ENDPOINT, {obj_ids: [apidae_obj_id], fields: fields})
  query_api(query_args, true, false)
end

#api_objects(opts = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'app/models/apidae/selection.rb', line 93

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_api(query_args, true, false)
    $apidae_cache.write(key, res)
  end
  res
end

#api_results(opts = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/models/apidae/selection.rb', line 71

def api_results(opts = {})
  key = cache_key(:results)
  res = $apidae_cache.read(key)
  unless res
    query_args = build_args(SELECTION_ENDPOINT, opts.merge({selection_ids: [apidae_id]}))
    res = query_api(query_args, true)
    $apidae_cache.write(key, res)
  end
  res
end

#as_textObject



148
149
150
# File 'app/models/apidae/selection.rb', line 148

def as_text
  "#{label} (#{apidae_id})"
end

#cleanupObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/apidae/selection.rb', line 45

def cleanup
  obsolete_count = apidae_selection_objects
    .joins("LEFT JOIN apidae_objs ON apidae_objs.id = apidae_selection_objects.apidae_object_id")
    .where("apidae_objs.id IS NULL")
    .delete_all
  logger.info "Cleaned up #{obsolete_count} obsolete selection-objects associations for selection #{apidae_id}"

  dups = apidae_selection_objects.reload.group(:apidae_object_id)
                                 .select("COUNT(id), apidae_object_id, ARRAY_AGG(id) AS so_ids")
                                 .having("COUNT(id) > ?", 1).map {|so| so.so_ids}
  dups_count = apidae_selection_objects.where(id: dups.map {|d| d.sort[1..-1]}.flatten).delete_all
  logger.debug "Cleaned up #{dups_count} duplicate selection-objects associations for selection #{apidae_id}"
end

#results(where_clause, offset, size) ⇒ Object



59
60
61
# File 'app/models/apidae/selection.rb', line 59

def results(where_clause, offset, size)
  objects.includes(:town).limit(size).offset(offset).where(where_clause)
end

#total(where_clause) ⇒ Object



63
64
65
# File 'app/models/apidae/selection.rb', line 63

def total(where_clause)
  objects.where(where_clause).count
end

#valid_api?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/apidae/selection.rb', line 67

def valid_api?
  apidae_project && !apidae_project.api_key.blank? && !apidae_project.apidae_id.blank?
end