Class: TouristicObject

Inherits:
Object
  • Object
show all
Includes:
AttributeHelper
Defined in:
lib/sitra_client/touristic_object.rb

Constant Summary collapse

SPECIFIC_INFOS =
{
  'ACTIVITE' => '@informationsActivite',
  'COMMERCE_ET_SERVICE' => '@informationsCommerceEtService',
  'DEGUSTATION' => '@informationsDegustation',
  'DOMAINE_SKIABLE' => '@informationsDomaineSkiable',
  'EQUIPEMENT' => '@informationsEquipement',
  'FETE_ET_MANIFESTATION' => '@informationsFeteEtManifestation',
  'HEBERGEMENT_COLLECTIF' => '@informationsHebergementCollectif',
  'HEBERGEMENT_LOCATIF' => '@informationsHebergementLocatif',
  'HOTELLERIE' => '@informationsHotellerie',
  'HOTELLERIE_PLEIN_AIR' => '@informationsHotelleriePleinAir',
  'PATRIMOINE_CULTUREL' => '@informationsPatrimoineCulturel',
  'PATRIMOINE_NATUREL' => '@informationsPatrimoineNaturel',
  'RESTAURATION' => '@informationsRestauration',
  'SEJOUR_PACKAGE' => '@informationsSejourPackage',
  'STRUCTURE' => '@informationsStructure',
  'TERRITOIRE' => '@informationsTerritoire'
}
DEFAULT_LIBELLE =
:libelleFr
ASPECT_WINTER =
'HIVER'
ASPECT_SUMMER =
'ETE'
ASPECT_CHALLENGED =
'HANDICAP'
ASPECT_BUSINESS =
'TOURISME_AFFAIRES'
ASPECT_GROUPS =
'GROUPES'
ASPECT_ACTIVITIES =
'PRESTATAIRE_ACTIVITES'
PHONE =
201
EMAIL =
204
WEBSITE =
205
WEEKDAYS_FR =
['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi']

Instance Method Summary collapse

Methods included from AttributeHelper

#attributes, #attributes=

Constructor Details

#initialize(hash, aspect = nil) ⇒ TouristicObject

Returns a new instance of TouristicObject.



47
48
49
50
51
52
53
54
55
# File 'lib/sitra_client/touristic_object.rb', line 47

def initialize(hash, aspect = nil)
  if aspect && hash[:aspects]
    aspect_data = hash[:aspects].select {|a| a[:aspect] == aspect && !a[:champsAspect].blank?}.first
    self.attributes = aspect_data ? aspect_hash(hash, aspect_data) : hash
  else
    self.attributes = hash
  end
  @libelle = DEFAULT_LIBELLE
end

Instance Method Details

#accessibiliteObject



254
255
256
# File 'lib/sitra_client/touristic_object.rb', line 254

def accessibilite
  @prestations && @prestations[:tourismesAdaptes] && @prestations[:tourismesAdaptes].collect {|t| t[@libelle]}
end

#additional_criteriaObject



224
225
226
# File 'lib/sitra_client/touristic_object.rb', line 224

def additional_criteria
  @presentation && @presentation[:typologiesPromoSitra] && @presentation[:typologiesPromoSitra].collect {|t| t[@libelle]}
end

#addressObject



144
145
146
147
148
# File 'lib/sitra_client/touristic_object.rb', line 144

def address
  computed_address = ''
  computed_address += "#{address_details[:adresse1]}, " unless address_details[:adresse1].nil?
  computed_address + address_details[:commune][:nom]
end

#address_detailsObject



136
137
138
139
140
141
142
# File 'lib/sitra_client/touristic_object.rb', line 136

def address_details
  if @informationsActivite && @informationsActivite[:prestataireActivites]
    @informationsActivite[:prestataireActivites][:adresse]
  else
    @localisation[:adresse]
  end
end

#aspect_hash(all_fields, aspect_fields) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sitra_client/touristic_object.rb', line 57

def aspect_hash(all_fields, aspect_fields)
  all_data = all_fields.except(:aspects)
  aspect_data = aspect_fields.except(:aspect, :champsAspect)
  overrides = aspect_fields[:champsAspect]
  overrides.each do |o|
    path = o.split('.').map(&:to_sym)
    if all_data.dig(path.first, *path[1..-1]) && aspect_fields.dig(path.first, *path[1..-1]).nil?
      *keys, last_key = path
      keys.inject(all_data, :fetch)[last_key] = nil
    end
  end
  all_data.deep_merge(aspect_data)
end

#bonplanObject



244
245
246
247
248
# File 'lib/sitra_client/touristic_object.rb', line 244

def bonplan
  if @presentation[:bonsPlans]
    @presentation[:bonsPlans][@libelle] || @presentation[:bonsPlans][DEFAULT_LIBELLE]
  end
end

#contact(types_ids = []) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sitra_client/touristic_object.rb', line 101

def contact(types_ids = [])
  contact_details = {}
  contact_entries = @informations[:moyensCommunication].nil? ? [] : @informations[:moyensCommunication]
  contact_entries.each do |c|
    if types_ids.include?(c[:type][:id])
      label = c[:type][@libelle]
      contact_details[label] = c[:coordonnees][:fr]
    end
  end
  contact_details
end

#cpltaccueilObject



250
251
252
# File 'lib/sitra_client/touristic_object.rb', line 250

def cpltaccueil
  @prestations && @prestations[:complementAccueil] && @prestations[:complementAccueil][:libelleFr]
end

#descriptionObject



89
90
91
92
93
# File 'lib/sitra_client/touristic_object.rb', line 89

def description
  if @presentation[:descriptifCourt]
    @presentation[:descriptifCourt][@libelle] || @presentation[:descriptifCourt][DEFAULT_LIBELLE]
  end
end

#detailsObject



95
96
97
98
99
# File 'lib/sitra_client/touristic_object.rb', line 95

def details
  if @presentation[:descriptifDetaille]
    @presentation[:descriptifDetaille][@libelle] || @presentation[:descriptifDetaille][DEFAULT_LIBELLE]
  end
end

#environmentsObject



220
221
222
# File 'lib/sitra_client/touristic_object.rb', line 220

def environments
  @localisation && @localisation[:environnements] && @localisation[:environnements].collect {|e| e[@libelle]}
end

#horairesObject



160
161
162
163
164
# File 'lib/sitra_client/touristic_object.rb', line 160

def horaires
  if @ouverture && @ouverture[:periodeEnClair]
    @ouverture[:periodeEnClair][@libelle]
  end
end

#idObject



77
78
79
# File 'lib/sitra_client/touristic_object.rb', line 77

def id
  @id.to_s
end

#informationObject



113
114
115
116
117
118
119
# File 'lib/sitra_client/touristic_object.rb', line 113

def information
  specific_information = {}
  unless @type.nil?
    specific_information = instance_variable_get(SPECIFIC_INFOS[@type])
  end
  @informations.merge(specific_information)
end

#latitudeObject



150
151
152
153
# File 'lib/sitra_client/touristic_object.rb', line 150

def latitude
  geoloc_details = parse_geoloc_details
  geoloc_details[:valide] ? geoloc_details[:geoJson][:coordinates][1] : nil
end

#longitudeObject



155
156
157
158
# File 'lib/sitra_client/touristic_object.rb', line 155

def longitude
  geoloc_details = parse_geoloc_details
  geoloc_details[:valide] ? geoloc_details[:geoJson][:coordinates][0] : nil
end

#multimediasObject



234
235
236
# File 'lib/sitra_client/touristic_object.rb', line 234

def multimedias
  @multimedias
end

#open_day_predicate(opening_period) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/sitra_client/touristic_object.rb', line 269

def open_day_predicate(opening_period)
  is_eligible = lambda {|date| return true}
  case(opening_period[:type])
    when 'OUVERTURE_SAUF'
      excluded_days = opening_period[:ouverturesJournalieres].collect {|o| WEEKDAYS_FR.index(o[:jour].downcase)}
      is_eligible = lambda {|date| return !excluded_days.include?(date.wday)}
    when 'OUVERTURE_TOUS_LES_JOURS'
    when 'OUVERTURE_SEMAINE'
      unless opening_period[:ouverturesJournalieres][0][:jour] == 'TOUS'
        included_days = opening_period[:ouverturesJournalieres].collect {|o| WEEKDAYS_FR.index(o[:jour].downcase)}
        is_eligible = lambda {|date| return included_days.include?(date.wday)}
      end
    when 'OUVERTURE_MOIS'
      opening_days = {}
      unless opening_period[:ouverturesJourDuMois].nil?
        opening_period[:ouverturesJourDuMois].each {|o| opening_days[o[:jour].downcase] = o[:jourDuMois]}
      end
      if opening_days.keys.include?('tous')
        is_eligible = lambda {|date| return occurrence_in_month(date) == opening_days['tous']}
      else
        included_weekdays = opening_days.keys.collect {|d| WEEKDAYS_FR.index(d)}
        is_eligible = lambda {|date| return included_weekdays.include?(date.wday) && occurrence_in_month(date) == opening_days[WEEKDAYS_FR[date.wday]]}
      end
    else
    # Unsupported
  end
  is_eligible
end

#open_on?(start_date, end_date) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/sitra_client/touristic_object.rb', line 166

def open_on?(start_date, end_date)
  opening_periods = @ouverture[:periodesOuvertures]
  is_open = false
  unless opening_periods.nil? || opening_periods.empty?
    i = 0
    while !is_open && i < opening_periods.length
      period_start = Date.parse(opening_periods[i][:dateDebut])
      period_end = Date.parse(opening_periods[i][:dateFin])
      start_day = Date.parse(start_date)
      end_day = [Date.parse(end_date), start_day.next_month].min
      if ranges_intersect(period_start, period_end, start_day, end_day, opening_periods[i][:tousLesAns])
        is_open = (start_day..end_day).to_a.any? {|d| open_day_predicate(opening_periods[i]).call(d)}
      end
      i += 1
    end
  end
  is_open
end

#picturesObject



121
122
123
124
# File 'lib/sitra_client/touristic_object.rb', line 121

def pictures
  (@illustrations.nil? || @illustrations.empty?) ? [{}] :
      @illustrations.collect {|i| i[:traductionFichiers][0].keep_if {|k, v| k.to_s.start_with?('url')}}
end

#populationObject



195
196
197
198
199
200
201
# File 'lib/sitra_client/touristic_object.rb', line 195

def population
  eligible_populations = []
  if @prestations && @prestations[:typesClientele]
    eligible_populations += @prestations[:typesClientele].collect {|t| t[@libelle]}
  end
  eligible_populations.uniq
end

#prestations(prestation_type) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/sitra_client/touristic_object.rb', line 203

def prestations(prestation_type)
  presta = {}
  unless @prestations.nil? || @prestations[prestation_type].nil?
    @prestations[prestation_type].each do |item|
      value = item[@libelle]
      if item[:familleCritere].nil?
        key = 'Autre'
      else
        key = item[:familleCritere][:libelleFr]
      end
      presta[key] ||= []
      presta[key] << value
    end
  end
  presta
end

#ranges_intersect(ref_start_day, ref_end_day, other_start_day, other_end_day, ignore_year = false) ⇒ Object



258
259
260
261
262
263
264
265
266
267
# File 'lib/sitra_client/touristic_object.rb', line 258

def ranges_intersect(ref_start_day, ref_end_day, other_start_day, other_end_day, ignore_year = false)
  has_intersection = other_start_day.between?(ref_start_day, ref_end_day) || other_end_day.between?(ref_start_day, ref_end_day) ||
      ref_start_day.between?(other_start_day, other_end_day) || ref_end_day.between?(other_start_day, other_end_day)
  if !has_intersection && ignore_year && ref_start_day <= other_end_day
    ref_start_day = ref_start_day.next_year
    ref_end_day = ref_end_day.next_year
    has_intersection = ranges_intersect(ref_start_day, ref_end_day, other_start_day, other_end_day, true)
  end
  has_intersection
end

#resaObject



228
229
230
231
232
# File 'lib/sitra_client/touristic_object.rb', line 228

def resa
  if @reservation
    @reservation[:organismes]
  end
end

#reservationObject



238
239
240
241
242
# File 'lib/sitra_client/touristic_object.rb', line 238

def reservation
  if @reservation[:complement]
    @reservation[:complement][:libelleFr] || @reservation[:complement][DEFAULT_LIBELLE]
  end
end

#service_providerObject



126
127
128
129
130
131
132
133
134
# File 'lib/sitra_client/touristic_object.rb', line 126

def service_provider
  if @informationsActivite && @informationsActivite[:prestataireActivites]
    @informationsActivite[:prestataireActivites][:nom][@libelle]
  elsif @informationsFeteEtManifestation
    @informationsFeteEtManifestation[:nomLieu]
  else
    nil
  end
end

#set_locale(locale) ⇒ Object



71
72
73
74
75
# File 'lib/sitra_client/touristic_object.rb', line 71

def set_locale(locale)
  unless locale.nil?
    @libelle = "libelle#{locale.capitalize}".to_sym
  end
end

#tarifObject



185
186
187
188
189
190
191
192
193
# File 'lib/sitra_client/touristic_object.rb', line 185

def tarif
  if @descriptionTarif
    if @descriptionTarif[:gratuit]
      return 'gratuit'
    elsif @descriptionTarif[:tarifsEnClair]
      @descriptionTarif[:tarifsEnClair][@libelle]
    end
  end
end

#titleObject



85
86
87
# File 'lib/sitra_client/touristic_object.rb', line 85

def title
  @nom[@libelle] || @nom[DEFAULT_LIBELLE]
end

#typeObject



81
82
83
# File 'lib/sitra_client/touristic_object.rb', line 81

def type
  @type
end