Class: Apidae::ApidaeDataParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/apidae/apidae_data_parser.rb

Constant Summary collapse

PHONE =
201
ALT_PHONE =
206
EMAIL =
204
WEBSITE =
205
GOOGLE =
3789
FACEBOOK =
207
TWITTER =
3755
YELP =
4007
TRIP_ADVISOR =
4000
FAX =
202
MOBILE_WEBSITE =
3769
SHORTY_URL =
4923
INSTAGRAM =
7221
CONTACTS_MAP =
{
    'telephone' => PHONE,
    'email' => EMAIL,
    'website' => WEBSITE,
    'facebook' => FACEBOOK,
    'google' => GOOGLE,
    'trip_advisor' => TRIP_ADVISOR,
    'twitter' => TWITTER,
    'yelp' => YELP,
    'fax' => FAX,
    'mobile_website' => MOBILE_WEBSITE,
    'shorty_url' => SHORTY_URL,
    'instagram' => INSTAGRAM
}
MODE_AUTO =
'auto'
MODE_MANUAL =
'manual'
MONDAY =
'MON'
TUESDAY =
'TUE'
WEDNESDAY =
'WED'
THURSDAY =
'THU'
FRIDAY =
'FRI'
SATURDAY =
'SAT'
SUNDAY =
'SUN'
ALL_DAYS =
[MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
WEEKDAYS_MAP =
{
    'LUNDI' => MONDAY,
    'MARDI' => TUESDAY,
    'MERCREDI' => WEDNESDAY,
    'JEUDI' => THURSDAY,
    'VENDREDI' => FRIDAY,
    'SAMEDI' => SATURDAY,
    'DIMANCHE' => SUNDAY
}

Class Method Summary collapse

Class Method Details

.node_id(node, key) ⇒ Object



365
366
367
368
369
370
371
# File 'app/models/apidae/apidae_data_parser.rb', line 365

def self.node_id(node, key)
  if node && node[key]
    node[key].is_a?(Array) ? node[key][0][:id] : node[key][:id]
  else
    nil
  end
end

.parse_attachments_data(attachments_array, *locales) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/models/apidae/apidae_data_parser.rb', line 152

def self.parse_attachments_data(attachments_array, *locales)
  atts_data = {}
  unless attachments_array.blank?
    l = locales.blank? ? [DEFAULT_LOCALE] : locales
    l.each do |locale|
      atts_data[locale] = []
      attachments_array.select { |att| att.is_a?(Hash) && !att[:traductionFichiers].blank? }.each do |att|
        atts_data[locale] << {
            id: att[:identifiant],
            name: localized_value(att, :nom, locale),
            url: att[:traductionFichiers][0][:url].gsub('http:', 'https:'),
            type: att[:type],
            link: att[:link],
            description: localized_value(att, :legende, locale)
        }
      end
    end
  end
  {attachments: atts_data}
end

.parse_booking(reservation_hash, visits_hash, *locales) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/apidae/apidae_data_parser.rb', line 321

def self.parse_booking(reservation_hash, visits_hash, *locales)
  booking_hash = {}
  if reservation_hash
    booking_hash[:booking_desc] = node_value(reservation_hash, :complement, *locales)
    booking_hash[:booking_entities] = reservation_hash[:organismes]
  end
  if visits_hash
    booking_hash[:visits_allowed] = visits_hash[:visitable] == true
    booking_hash[:visits_desc] = node_value(visits_hash, :complementVisite, *locales)
    booking_hash[:visits_duration] = visits_hash[:dureeMoyenneVisiteIndividuelle]
    booking_hash[:visits_services] = lists_ids(visits_hash[:prestationsVisitesGroupees]) + lists_ids(visits_hash[:prestationsVisitesIndividuelles])
  end
  booking_hash
end

.parse_contact_data(information_hash, contacts_list) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/apidae/apidae_data_parser.rb', line 173

def self.parse_contact_data(information_hash, contacts_list)
  contact_details = {contacts: contacts_list}
  unless information_hash.blank?
    contact_entries = information_hash[:moyensCommunication] || []
    contact_entries.each do |c|
      case c[:type][:id]
      when PHONE, ALT_PHONE
        contact_details[:telephone] ||= {}
        contact_details[:telephone][c[:identifiant]] = {value: c[:coordonnees][:fr], description: c.dig(:observation, :libelleFr)}
      else
        contacts_refs_by_code = CONTACTS_MAP.invert
        if contacts_refs_by_code.keys.include?(c[:type][:id])
          contact_ref = contacts_refs_by_code[c[:type][:id]].to_sym
          contact_details[contact_ref] ||= {}
          contact_details[contact_ref][c[:identifiant]] = {value: c[:coordonnees][:fr], description: c.dig(:observation, :libelleFr)}
        end
      end
    end
  end
  contact_details
end

.parse_desc_data(data_hash, private_data, presta_data, *locales) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/apidae/apidae_data_parser.rb', line 117

def self.parse_desc_data(data_hash, private_data, presta_data, *locales)
  unless data_hash.blank?
    {
        short_desc: node_value(data_hash, :descriptifCourt, *locales),
        long_desc: node_value(data_hash, :descriptifDetaille, *locales),
        theme_desc: data_hash[:descriptifsThematises].blank? ? {} : Hash[data_hash[:descriptifsThematises].map {|th| [node_id(th, :theme), node_value(th, :description, *locales)]}],
        private_desc: private_data.blank? ? {} : Hash[private_data.map {|d| [d[:nomTechnique], (node_value(d, :descriptif, *locales).blank? ? {LOCALE_FR => d[:libelleFr]} : node_value(d, :descriptif, *locales))]}],
        accessibility_desc: presta_data.blank? ? {} : Hash[presta_data.select {|k, v| k.to_s.start_with?('descriptifHandicap')}.map {|k, v| [k, node_value(presta_data, k, *locales)]}]
    }
  end
end

.parse_entity_fields(information_hash, type_data_hash, sp_hash) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'app/models/apidae/apidae_data_parser.rb', line 345

def self.parse_entity_fields(information_hash, type_data_hash, sp_hash)
  entity_hash = {}
  if information_hash
    if information_hash[:structureGestion]
      entity_hash.merge!({entity_id: information_hash[:structureGestion][:id], entity_name: information_hash[:structureGestion].dig(:nom, :libelleFr),
                          info_entity_id: (information_hash[:structureInformation] || information_hash[:structureGestion])[:id],
                          info_entity_name: (information_hash[:structureInformation] || information_hash[:structureGestion]).dig(:nom, :libelleFr),
                          service_provider_id: node_id(type_data_hash, :prestataireActivites)})
    end

    if information_hash[:informationsLegales]
      entity_hash.merge!({legal: information_hash[:informationsLegales]})
    end
  end
  if sp_hash && sp_hash[:prestataireActivites]
    entity_hash[:is_service_provider] = true
  end
  entity_hash
end

.parse_location_data(location_hash, type_data_hash, territories) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/apidae/apidae_data_parser.rb', line 195

def self.parse_location_data(location_hash, type_data_hash, territories)
  loc_data = {}
  unless location_hash.blank?
    address_hash = location_hash[:adresse] || {}
    computed_address = []
    unless address_hash.blank?
      computed_address << address_hash[:adresse1]
      computed_address << address_hash[:adresse2]
      computed_address << address_hash[:adresse3]
    end
    loc_data.merge!({address: computed_address})
    loc_data.merge!({place: (type_data_hash ? type_data_hash[:nomLieu] : nil) || address_hash[:nomDuLieu]})
    geoloc_details = location_hash[:geolocalisation]
    if geoloc_details && geoloc_details[:valide] && geoloc_details[:geoJson]
      loc_data[:latitude] = geoloc_details[:geoJson][:coordinates][1]
      loc_data[:longitude] = geoloc_details[:geoJson][:coordinates][0]
    end
    loc_data[:map_reference] = geoloc_details[:reperePlan]
    loc_data[:valid] = geoloc_details[:valide] == true
    loc_data[:altitude] = geoloc_details[:altitude] if geoloc_details
    loc_data[:access] = node_value(geoloc_details, :complement) if geoloc_details
    loc_data[:environments] = location_hash[:environnements].map {|e| e[:id]} if location_hash[:environnements]
  end
  loc_data[:territories] = territories.map {|t| t[:id]} unless territories.blank?
  loc_data
end

.parse_openings(openings_hash, opening_days, *locales) ⇒ Object

Note : use internal format for openings storage (ideally Apihours one, to merge data from both sources)



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/models/apidae/apidae_data_parser.rb', line 223

def self.parse_openings(openings_hash, opening_days, *locales)
  if openings_hash && openings_hash[:periodeEnClair]
    {
        openings_desc: node_value(openings_hash, :periodeEnClair, *locales),
        openings_desc_mode: openings_hash[:periodeEnClairGenerationMode] == 'AUTOMATIQUE' ? MODE_AUTO : MODE_MANUAL,
        openings: build_openings(openings_hash, *locales),
        all_year_long: openings_hash[:ouvertTouteLAnnee] == 'OUVERT_TOUTE_L_ANNEE' ? 'true' : 'false',
        time_periods: lists_ids(openings_hash[:indicationsPeriode]),
        openings_extra: lists_ids(openings_hash[:ouverturesComplementaires]),
        opening_days: opening_days
    }
  end
end

.parse_owner_data(data_hash, polls_data) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'app/models/apidae/apidae_data_parser.rb', line 106

def self.parse_owner_data(data_hash, polls_data)
  owner_data = {}
  unless data_hash.blank?
    owner_data.merge!({owner_name: data_hash[:nom], owner_id: data_hash[:id]})
  end
  unless polls_data.blank?
    owner_data.merge!({polls: polls_data[:enquetes] || []})
  end
  owner_data
end

.parse_pictures_data(pictures_array, *locales) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/apidae/apidae_data_parser.rb', line 129

def self.parse_pictures_data(pictures_array, *locales)
  pics_data = {}
  unless pictures_array.blank?
    l = locales.blank? ? [DEFAULT_LOCALE] : locales
    l.each do |locale|
      pics_data[locale] = []
      pictures_array.select { |p| p.is_a?(Hash) && !p[:traductionFichiers].blank? }.each do |pic|
        pic_file = pic[:traductionFichiers][0]
        pics_data[locale] << {
            id: pic[:identifiant],
            name: localized_value(pic, :nom, locale),
            url: pic_file[:url].gsub('http:', 'https:'),
            description: localized_value(pic, :legende, locale),
            credits: localized_value(pic, :copyright, locale),
            expiration_date: pic[:dateLimiteDePublication] || '',
            properties: {size: pic_file[:taille], width: pic_file[:largeur], height: pic_file[:hauteur]}
        }
      end
    end
  end
  {pictures: pics_data}
end

.parse_rates(rates_hash, *locales) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/apidae/apidae_data_parser.rb', line 237

def self.parse_rates(rates_hash, *locales)
  if rates_hash
    desc = rates_hash[:gratuit] ? {DEFAULT_LOCALE => 'gratuit'} : node_value(rates_hash, :tarifsEnClair, *locales)
    values = rates_hash[:periodes].blank? ? [] : rates_hash[:periodes].map {|p| build_rate(p, *locales)}
    methods = rates_hash[:modesPaiement].blank? ? [] : rates_hash[:modesPaiement].map {|p| p[:id]}
    {
        rates_desc: desc, rates: values, rates_category: rates_hash[:indicationTarif], payment_methods: methods,
        tax_included: (rates_hash[:taxeDeSejourIncluse].blank? || rates_hash[:taxeDeSejourIncluse] == 'NON_COMMUNIQUE') ? nil : (rates_hash[:taxeDeSejourIncluse] == 'OUI'),
        rates_desc_mode: rates_hash[:tarifsEnClairGenerationMode] == 'AUTOMATIQUE' ? MODE_AUTO : MODE_MANUAL,
        includes: node_value(rates_hash, :leTarifComprend, *locales),
        excludes: node_value(rates_hash, :leTarifNeComprendPas, *locales),
        rates_extra: node_value(rates_hash, :complement, *locales)
    }
  end
end

.parse_service_data(data_hash, type_data_hash) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'app/models/apidae/apidae_data_parser.rb', line 290

def self.parse_service_data(data_hash, type_data_hash)
  if data_hash
    {
        services: lists_ids(data_hash[:services]),
        equipments: lists_ids(data_hash[:equipements]),
        comfort: lists_ids(data_hash[:conforts]),
        activities: lists_ids(data_hash[:activites], type_data_hash ? type_data_hash[:activites] : [],
                              type_data_hash ? type_data_hash[:activitesSportives] : [],
                              type_data_hash ? type_data_hash[:activitesCulturelles] : []),
        challenged: lists_ids(data_hash[:tourismesAdaptes]),
        languages: lists_ids(data_hash[:languesParlees]),
        documentation_languages: lists_ids(data_hash[:languesDocumentation])
    }
  end
end

.parse_tags_data(pres_data_hash, crit_data_hash, linked_data_hash) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'app/models/apidae/apidae_data_parser.rb', line 306

def self.parse_tags_data(pres_data_hash, crit_data_hash, linked_data_hash)
  tags = {}
  if pres_data_hash
    tags[:promo] = lists_ids(pres_data_hash[:typologiesPromoSitra])
  end
  unless crit_data_hash.blank?
    tags[:internal] = crit_data_hash.map {|c| c[:id]}
  end
  unless linked_data_hash.blank? || linked_data_hash[:liensObjetsTouristiquesTypes].blank?
    tags[:linked] = linked_data_hash[:liensObjetsTouristiquesTypes]
                        .map {|l| {apidae_id: l[:objetTouristique][:id], apidae_type: l[:objetTouristique][:type], category: l[:type]}}
  end
  tags
end

.parse_title(data_hash, *locales) ⇒ Object



102
103
104
# File 'app/models/apidae/apidae_data_parser.rb', line 102

def self.parse_title(data_hash, *locales)
  {title: node_value(data_hash, :nom, *locales)}
end

.parse_town(location_hash) ⇒ Object



336
337
338
339
340
341
342
343
# File 'app/models/apidae/apidae_data_parser.rb', line 336

def self.parse_town(location_hash)
  if location_hash
    address_hash = location_hash[:adresse]
    (!address_hash.blank? && address_hash[:commune]) ? Town.find_by_apidae_id(address_hash[:commune][:id]) : nil
  else
    nil
  end
end

.parse_type_data(apidae_obj, type_hash, presta_hash, business_hash, *locales) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/models/apidae/apidae_data_parser.rb', line 253

def self.parse_type_data(apidae_obj, type_hash, presta_hash, business_hash, *locales)
  data_hash = type_hash || {}
  prestations_hash = presta_hash || {}
  apidae_obj.apidae_subtype = lists_ids(data_hash[:typesManifestation]).first if apidae_obj.apidae_type == Obj::FEM
  apidae_obj.apidae_subtype = node_id(data_hash, :rubrique) if apidae_obj.apidae_type == Obj::EQU
  apidae_obj.apidae_subtype = lists_ids(data_hash[:typesHebergement]).first if apidae_obj.apidae_type == Obj::SPA
  {
      categories: lists_ids(data_hash[:categories], data_hash[:typesDetailles], data_hash[:activiteCategories], data_hash[:typesHabitation]),
      themes: lists_ids(data_hash[:themes]),
      capacity: (data_hash[:capacite] || {})
                    .merge(presta_hash ? {group_min: (presta_hash[:nombrePersonnesMinimum] || presta_hash[:tailleGroupeMin]), group_max: presta_hash[:tailleGroupeMax],
                                          age_min: presta_hash[:ageMin], age_max: presta_hash[:ageMax],
                                          wheelchair_max: presta_hash[:nombrePersonnesEnFauteuilRoulantAccueilliesSimultanement]} : {}),
      classification: nodes_ids(data_hash[:classement], data_hash[:classementPrefectoral], data_hash[:classification]) +
          lists_ids(data_hash[:classementsGuides]) + lists_ids(data_hash[:classements]),
      classification_date: data_hash[:dateClassement],
      classification_ref: data_hash[:numeroClassement],
      labels: lists_ids(data_hash[:labels], data_hash[:labelsChartesQualite], prestations_hash[:labelsTourismeHandicap]) +
          (node_id(data_hash, :typeLabel) ? [node_id(data_hash, :typeLabel)] : []),
      chains: lists_ids(data_hash[:chaines]) + nodes_ids(data_hash[:chaineEtLabel]),
      area: apidae_obj.apidae_type == Obj::DOS ? data_hash.except(:classification) : node_value(data_hash, :lieuDePratique),
      track: apidae_obj.apidae_type == Obj::EQU ? (data_hash[:itineraire] || {}).except(:passagesDelicats) : nil,
      tricky_sections: apidae_obj.apidae_type == Obj::EQU ? node_value(data_hash[:itineraire], :passagesDelicats, *locales) : nil,
      products: lists_ids(data_hash[:typesProduit], data_hash[:aopAocIgps], data_hash[:specialites]),
      audience: lists_ids(prestations_hash[:typesClientele]),
      animals: {allowed: prestations_hash[:animauxAcceptes] == 'ACCEPTES', unknown: prestations_hash[:animauxAcceptes] == 'NON_DISPONIBLE',
                desc: node_value(prestations_hash, :descriptifAnimauxAcceptes, *locales), fee: prestations_hash[:animauxAcceptesSupplement] ==	'AVEC_SUPPLEMENT'},
      extra: apidae_obj.apidae_type == Obj::SPA ? node_value(data_hash, :formuleHebergement, *locales) : node_value(prestations_hash, :complementAccueil, *locales),
      duration: apidae_obj.apidae_type == Obj::SPA ? {days: data_hash[:nombreJours], nights: data_hash[:nombreNuits]} : data_hash[:dureeSeance],
      certifications: data_hash[:agrements].blank? ? [] : data_hash[:agrements].map {|a| {id: a[:type][:id], identifier: a[:numero]}},
      business: (business_hash || {}).except(:sallesEquipeesPour, :sallesEquipement, :sallesRestauration, :sallesReunion, :sallesHebergement),
      business_equipments: lists_ids((business_hash || {})[:sallesEquipeesPour], (business_hash || {})[:sallesEquipement],
                                     (business_hash || {})[:sallesRestauration], (business_hash || {})[:sallesHebergement]),
      business_rooms: (business_hash || {})[:sallesReunion]
  }
end

.parse_versioned_fields(data_hash) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/apidae/apidae_data_parser.rb', line 55

def self.parse_versioned_fields(data_hash)
  version_fields = data_hash[:champsAspect] || []
  matched_fields = []
  version_fields.each do |f|
    case f
    when 'nom'
      matched_fields << 'title'
    when 'presentation.descriptifCourt'
      matched_fields << 'short_desc'
    when 'presentation.descriptifDetaille'
      matched_fields << 'long_desc'
    when 'illustrations'
      matched_fields << 'pictures'
    when 'multimedias'
      matched_fields << 'attachments'
    when 'informations.moyensCommunication'
      matched_fields << 'contact'
    when 'descriptifsThematises'
      matched_fields << 'theme_desc'
    when 'ouverture.periodesOuvertures', 'ouverture.periodeEnClair'
      matched_fields << 'openings_desc'
      matched_fields << 'openings'
    when 'ouverture.periodeEnClairAutomatique'
      matched_fields << 'openings_desc_mode'
    when 'descriptionTarif.tarifsEnClair', 'descriptionTarif.periodes'
      matched_fields << 'rates_desc'
      matched_fields << 'rates'
    when 'descriptionTarif.tarifsEnClairAutomatique'
      matched_fields << 'rates_desc_mode'
    when 'prestations.equipements'
      matched_fields << 'equipments'
    when 'prestations.activites'
      matched_fields << 'activities'
    when 'prestations.services'
      matched_fields << 'services'
    when 'localisation.environnements'
      matched_fields << 'environments'
    when 'prestations.complementAccueil'
      matched_fields << 'extra'
    when 'localisation.geolocalisation.complement'
      matched_fields << 'access'
    else
    end
  end
  matched_fields.uniq
end