sitra_client 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21e0cae34c5a4a4f3aa94ef7e0310b93a476ae32
4
- data.tar.gz: b8123dc3b8c0b9f3e99a1fe686190a0b7c490bd2
3
+ metadata.gz: 710f4b61a67ff27a473ac6faf35646473c4a7b89
4
+ data.tar.gz: 711b7862fcf98031caded37dabe9481a3bc77180
5
5
  SHA512:
6
- metadata.gz: beebfd255f34ed72b79657f7a27eed7738fefedb780b93e4c347f1b63593d7689743f3f686b5ae9af8553595dc6ac5ba981284b89a616516cd8fd06768acf674
7
- data.tar.gz: 05297c77a40e5a57d6da6271fe1f07e72fc965d969e84fb5ff21b32feee28cbaaba4c921c854b2d3fcd815b6e13f808327d3ca8e54d4f42d81e0330ae740a93a
6
+ metadata.gz: 2a0f2683430e5a24393768fe6024999efdc911da418959b429c181699d0c50832c4c628c23e18d927c135102365d5062de7ab7aef1565d4c6de8435a4e2f55cf
7
+ data.tar.gz: 610ef251b9d0a64edabe28db915e28d6633fac269d58c548a8f77c28660b0070313c73c7fde647488b90b388015babc91734a3e6c21089c16ed60d4e2731768f
@@ -31,24 +31,24 @@ module SitraClient
31
31
  @config
32
32
  end
33
33
 
34
- def self.query(criteria, all_results = false)
34
+ def self.query(criteria, all_results = false, aspect = nil)
35
35
  query_result = {}
36
36
  if all_results
37
37
  loops = 0
38
38
  criteria[:first] = 0
39
39
  criteria[:count] = MAX_COUNT
40
40
  response = get_response(criteria)
41
- results = response.as_array
41
+ results = response.as_array(aspect)
42
42
  while response.results_count > results.length && loops < MAX_LOOPS
43
43
  loops += 1
44
44
  criteria[:first] += MAX_COUNT
45
- results += get_response(criteria).as_array
45
+ results += get_response(criteria).as_array(aspect)
46
46
  end
47
47
  query_result[:count] = response.results_count
48
48
  query_result[:results] = results
49
49
  else
50
50
  response = get_response(criteria)
51
- results = response.as_array
51
+ results = response.as_array(aspect)
52
52
  query_result[:count] = response.results_count
53
53
  query_result[:results] = results
54
54
  end
@@ -1,11 +1,11 @@
1
- module AttributeHelper
2
- def attributes=(hash)
3
- hash.each do |key, value|
4
- instance_variable_set("@#{key}", value)
5
- end
6
- end
7
-
8
- def attributes
9
- Hash[instance_variables.map { |name| [name.to_s[1..-1], instance_variable_get(name)] }]
10
- end
1
+ module AttributeHelper
2
+ def attributes=(hash)
3
+ hash.each do |key, value|
4
+ instance_variable_set("@#{key}", value)
5
+ end
6
+ end
7
+
8
+ def attributes
9
+ Hash[instance_variables.map { |name| [name.to_s[1..-1], instance_variable_get(name)] }]
10
+ end
11
11
  end
@@ -1,18 +1,18 @@
1
- require 'sitra_client/attribute_helper'
2
- require 'json'
3
-
4
- class SitraQuery
5
-
6
- include AttributeHelper
7
-
8
- def initialize(api_key, site_identifier, criteria = {})
9
- @apiKey = api_key
10
- @projetId = site_identifier
11
- self.attributes = criteria
12
- end
13
-
14
- def to_params
15
- JSON.generate attributes
16
- end
17
-
18
- end
1
+ require 'sitra_client/attribute_helper'
2
+ require 'json'
3
+
4
+ class SitraQuery
5
+
6
+ include AttributeHelper
7
+
8
+ def initialize(api_key, site_identifier, criteria = {})
9
+ @apiKey = api_key
10
+ @projetId = site_identifier
11
+ self.attributes = criteria
12
+ end
13
+
14
+ def to_params
15
+ JSON.generate attributes
16
+ end
17
+
18
+ end
@@ -1,44 +1,44 @@
1
- require 'sitra_client/attribute_helper'
2
- require 'sitra_client/touristic_object'
3
- require 'json'
4
-
5
- class SitraResponse
6
-
7
- def initialize
8
- @json_response = ''
9
- @response_hash = {}
10
- end
11
-
12
- def append_line(line)
13
- @json_response += line unless line.nil?
14
- end
15
-
16
- def returned_count
17
- [as_hash[:query][:count], results_count - as_hash[:query][:first]].min
18
- end
19
-
20
- def results_count
21
- as_hash[:numFound]
22
- end
23
-
24
- def as_hash
25
- if @response_hash.empty?
26
- @response_hash = JSON.parse @json_response, :symbolize_names => true
27
- end
28
- @response_hash
29
- end
30
-
31
- def as_raw_json
32
- @json_response
33
- end
34
-
35
- def as_array
36
- if as_hash[:objetsTouristiques].nil?
37
- results = []
38
- else
39
- results = as_hash[:objetsTouristiques].collect {|obj_hash| TouristicObject.new(obj_hash)}
40
- end
41
- results
42
- end
43
-
1
+ require 'sitra_client/attribute_helper'
2
+ require 'sitra_client/touristic_object'
3
+ require 'json'
4
+
5
+ class SitraResponse
6
+
7
+ def initialize
8
+ @json_response = ''
9
+ @response_hash = {}
10
+ end
11
+
12
+ def append_line(line)
13
+ @json_response += line unless line.nil?
14
+ end
15
+
16
+ def returned_count
17
+ [as_hash[:query][:count], results_count - as_hash[:query][:first]].min
18
+ end
19
+
20
+ def results_count
21
+ as_hash[:numFound]
22
+ end
23
+
24
+ def as_hash
25
+ if @response_hash.empty?
26
+ @response_hash = JSON.parse @json_response, :symbolize_names => true
27
+ end
28
+ @response_hash
29
+ end
30
+
31
+ def as_raw_json
32
+ @json_response
33
+ end
34
+
35
+ def as_array(aspect = nil)
36
+ if as_hash[:objetsTouristiques].nil?
37
+ results = []
38
+ else
39
+ results = as_hash[:objetsTouristiques].collect {|obj_hash| TouristicObject.new(obj_hash, aspect)}
40
+ end
41
+ results
42
+ end
43
+
44
44
  end
@@ -1,296 +1,324 @@
1
- # encoding: UTF-8
2
- require 'active_support/duration'
3
- require 'active_support/core_ext/numeric/time'
4
- require 'active_support/core_ext/integer/time'
5
- require 'sitra_client/attribute_helper'
6
-
7
- class TouristicObject
8
-
9
- include AttributeHelper
10
-
11
- SPECIFIC_INFOS = {
12
- 'ACTIVITE' => '@informationsActivite',
13
- 'COMMERCE_ET_SERVICE' => '@informationsCommerceEtService',
14
- 'DEGUSTATION' => '@informationsDegustation',
15
- 'DOMAINE_SKIABLE' => '@informationsDomaineSkiable',
16
- 'EQUIPEMENT' => '@informationsEquipement',
17
- 'FETE_ET_MANIFESTATION' => '@informationsFeteEtManifestation',
18
- 'HEBERGEMENT_COLLECTIF' => '@informationsHebergementCollectif',
19
- 'HEBERGEMENT_LOCATIF' => '@informationsHebergementLocatif',
20
- 'HOTELLERIE' => '@informationsHotellerie',
21
- 'HOTELLERIE_PLEIN_AIR' => '@informationsHotelleriePleinAir',
22
- 'PATRIMOINE_CULTUREL' => '@informationsPatrimoineCulturel',
23
- 'PATRIMOINE_NATUREL' => '@informationsPatrimoineNaturel',
24
- 'RESTAURATION' => '@informationsRestauration',
25
- 'SEJOUR_PACKAGE' => '@informationsSejourPackage',
26
- 'STRUCTURE' => '@informationsStructure',
27
- 'TERRITOIRE' => '@informationsTerritoire'
28
- }
29
-
30
- DEFAULT_LIBELLE = :libelleFr
31
-
32
- PHONE = 201
33
- EMAIL = 204
34
- WEBSITE = 205
35
-
36
- WEEKDAYS_FR = ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi']
37
-
38
- def initialize(hash)
39
- self.attributes = hash
40
- @libelle = DEFAULT_LIBELLE
41
- end
42
-
43
- def set_locale(locale)
44
- unless locale.nil?
45
- @libelle = "libelle#{locale.capitalize}".to_sym
46
- end
47
- end
48
-
49
- def id
50
- @id.to_s
51
- end
52
-
53
- def type
54
- @type
55
- end
56
-
57
- def title
58
- @nom[@libelle] || @nom[DEFAULT_LIBELLE]
59
- end
60
-
61
- def description
62
- if @presentation[:descriptifCourt]
63
- @presentation[:descriptifCourt][@libelle] || @presentation[:descriptifCourt][DEFAULT_LIBELLE]
64
- end
65
- end
66
-
67
- def details
68
- if @presentation[:descriptifDetaille]
69
- @presentation[:descriptifDetaille][@libelle] || @presentation[:descriptifDetaille][DEFAULT_LIBELLE]
70
- end
71
- end
72
-
73
- def contact(types_ids = [])
74
- contact_details = {}
75
- contact_entries = @informations[:moyensCommunication].nil? ? [] : @informations[:moyensCommunication]
76
- contact_entries.each do |c|
77
- if types_ids.include?(c[:type][:id])
78
- label = c[:type][@libelle]
79
- contact_details[label] = c[:coordonnees][:fr]
80
- end
81
- end
82
- contact_details
83
- end
84
-
85
- def information
86
- specific_information = {}
87
- unless @type.nil?
88
- specific_information = instance_variable_get(SPECIFIC_INFOS[@type])
89
- end
90
- @informations.merge(specific_information)
91
- end
92
-
93
- def pictures
94
- (@illustrations.nil? || @illustrations.empty?) ? [{}] :
95
- @illustrations.collect {|i| i[:traductionFichiers][0].keep_if {|k, v| k.to_s.start_with?('url')}}
96
- end
97
-
98
- def service_provider
99
- if @informationsActivite && @informationsActivite[:prestataireActivites]
100
- @informationsActivite[:prestataireActivites][:nom][@libelle]
101
- elsif @informationsFeteEtManifestation
102
- @informationsFeteEtManifestation[:nomLieu]
103
- else
104
- nil
105
- end
106
- end
107
-
108
- def address_details
109
- if @informationsActivite && @informationsActivite[:prestataireActivites]
110
- @informationsActivite[:prestataireActivites][:adresse]
111
- else
112
- @localisation[:adresse]
113
- end
114
- end
115
-
116
- def address
117
- computed_address = ''
118
- computed_address += "#{address_details[:adresse1]}, " unless address_details[:adresse1].nil?
119
- computed_address + address_details[:commune][:nom]
120
- end
121
-
122
- def latitude
123
- geoloc_details = parse_geoloc_details
124
- geoloc_details[:valide] ? geoloc_details[:geoJson][:coordinates][1] : nil
125
- end
126
-
127
- def longitude
128
- geoloc_details = parse_geoloc_details
129
- geoloc_details[:valide] ? geoloc_details[:geoJson][:coordinates][0] : nil
130
- end
131
-
132
- def horaires
133
- if @ouverture && @ouverture[:periodeEnClair]
134
- @ouverture[:periodeEnClair][@libelle]
135
- end
136
- end
137
-
138
- def open_on?(start_date, end_date)
139
- opening_periods = @ouverture[:periodesOuvertures]
140
- is_open = false
141
- unless opening_periods.nil? || opening_periods.empty?
142
- i = 0
143
- while !is_open && i < opening_periods.length
144
- period_start = Date.parse(opening_periods[i][:dateDebut])
145
- period_end = Date.parse(opening_periods[i][:dateFin])
146
- start_day = Date.parse(start_date)
147
- end_day = [Date.parse(end_date), start_day.next_month].min
148
- if ranges_intersect(period_start, period_end, start_day, end_day, opening_periods[i][:tousLesAns])
149
- is_open = (start_day..end_day).to_a.any? {|d| open_day_predicate(opening_periods[i]).call(d)}
150
- end
151
- i += 1
152
- end
153
- end
154
- is_open
155
- end
156
-
157
- def tarif
158
- if @descriptionTarif
159
- if @descriptionTarif[:gratuit]
160
- return 'gratuit'
161
- elsif @descriptionTarif[:tarifsEnClair]
162
- @descriptionTarif[:tarifsEnClair][@libelle]
163
- end
164
- end
165
- end
166
-
167
- def population
168
- eligible_populations = []
169
- if @prestations && @prestations[:typesClientele]
170
- eligible_populations += @prestations[:typesClientele].collect {|t| t[@libelle]}
171
- end
172
- eligible_populations.uniq
173
- end
174
-
175
- def prestations(prestation_type)
176
- presta = {}
177
- unless @prestations.nil? || @prestations[prestation_type].nil?
178
- @prestations[prestation_type].each do |item|
179
- value = item[@libelle]
180
- if item[:familleCritere].nil?
181
- key = 'Autre'
182
- else
183
- key = item[:familleCritere][:libelleFr]
184
- end
185
- presta[key] ||= []
186
- presta[key] << value
187
- end
188
- end
189
- presta
190
- end
191
-
192
- def environments
193
- @localisation && @localisation[:environnements] && @localisation[:environnements].collect {|e| e[@libelle]}
194
- end
195
-
196
- def additional_criteria
197
- @presentation && @presentation[:typologiesPromoSitra] && @presentation[:typologiesPromoSitra].collect {|t| t[@libelle]}
198
- end
199
-
200
- def resa
201
- if @reservation
202
- @reservation[:organismes]
203
- end
204
- end
205
-
206
- def multimedias
207
- @multimedias
208
- end
209
-
210
- def reservation
211
- if @reservation[:complement]
212
- @reservation[:complement][:libelleFr] || @reservation[:complement][DEFAULT_LIBELLE]
213
- end
214
- end
215
-
216
- def bonplan
217
- if @presentation[:bonsPlans]
218
- @presentation[:bonsPlans][@libelle] || @presentation[:bonsPlans][DEFAULT_LIBELLE]
219
- end
220
- end
221
-
222
- def cpltaccueil
223
- @prestations && @prestations[:complementAccueil] && @prestations[:complementAccueil][:libelleFr]
224
- end
225
-
226
- def accessibilite
227
- @prestations && @prestations[:tourismesAdaptes] && @prestations[:tourismesAdaptes].collect {|t| t[@libelle]}
228
- end
229
-
230
- def ranges_intersect(ref_start_day, ref_end_day, other_start_day, other_end_day, ignore_year = false)
231
- has_intersection = other_start_day.between?(ref_start_day, ref_end_day) || other_end_day.between?(ref_start_day, ref_end_day) ||
232
- ref_start_day.between?(other_start_day, other_end_day) || ref_end_day.between?(other_start_day, other_end_day)
233
- if !has_intersection && ignore_year && ref_start_day <= other_end_day
234
- ref_start_day = ref_start_day.next_year
235
- ref_end_day = ref_end_day.next_year
236
- has_intersection = ranges_intersect(ref_start_day, ref_end_day, other_start_day, other_end_day, true)
237
- end
238
- has_intersection
239
- end
240
-
241
- def open_day_predicate(opening_period)
242
- is_eligible = lambda {|date| return true}
243
- case(opening_period[:type])
244
- when 'OUVERTURE_SAUF'
245
- excluded_days = opening_period[:ouverturesJournalieres].collect {|o| WEEKDAYS_FR.index(o[:jour].downcase)}
246
- is_eligible = lambda {|date| return !excluded_days.include?(date.wday)}
247
- when 'OUVERTURE_TOUS_LES_JOURS'
248
- when 'OUVERTURE_SEMAINE'
249
- unless opening_period[:ouverturesJournalieres][0][:jour] == 'TOUS'
250
- included_days = opening_period[:ouverturesJournalieres].collect {|o| WEEKDAYS_FR.index(o[:jour].downcase)}
251
- is_eligible = lambda {|date| return included_days.include?(date.wday)}
252
- end
253
- when 'OUVERTURE_MOIS'
254
- opening_days = {}
255
- unless opening_period[:ouverturesJourDuMois].nil?
256
- opening_period[:ouverturesJourDuMois].each {|o| opening_days[o[:jour].downcase] = o[:jourDuMois]}
257
- end
258
- if opening_days.keys.include?('tous')
259
- is_eligible = lambda {|date| return occurrence_in_month(date) == opening_days['tous']}
260
- else
261
- included_weekdays = opening_days.keys.collect {|d| WEEKDAYS_FR.index(d)}
262
- is_eligible = lambda {|date| return included_weekdays.include?(date.wday) && occurrence_in_month(date) == opening_days[WEEKDAYS_FR[date.wday]]}
263
- end
264
- else
265
- # Unsupported
266
- end
267
- is_eligible
268
- end
269
-
270
- private
271
-
272
- def occurrence_in_month(date)
273
- case date.mday
274
- when 1..7
275
- 'D_1ER'
276
- when 8..14
277
- 'D_2EME'
278
- when 15..21
279
- 'D_3EME'
280
- when 22..28
281
- 'D_4EME'
282
- else
283
- 'D_DERNIER'
284
- end
285
- end
286
-
287
- def parse_geoloc_details
288
- if @informationsActivite.nil? || @informationsActivite[:prestataireActivites].nil?
289
- geoloc_details = @localisation[:geolocalisation]
290
- else
291
- geoloc_details = @informationsActivite[:prestataireActivites][:geolocalisation]
292
- end
293
- geoloc_details
294
- end
295
-
1
+ # encoding: UTF-8
2
+ require 'active_support/duration'
3
+ require 'active_support/core_ext/object/deep_dup'
4
+ require 'active_support/core_ext/hash/deep_merge'
5
+ require 'active_support/core_ext/numeric/time'
6
+ require 'active_support/core_ext/integer/time'
7
+ require 'sitra_client/attribute_helper'
8
+
9
+ class TouristicObject
10
+
11
+ include AttributeHelper
12
+
13
+ SPECIFIC_INFOS = {
14
+ 'ACTIVITE' => '@informationsActivite',
15
+ 'COMMERCE_ET_SERVICE' => '@informationsCommerceEtService',
16
+ 'DEGUSTATION' => '@informationsDegustation',
17
+ 'DOMAINE_SKIABLE' => '@informationsDomaineSkiable',
18
+ 'EQUIPEMENT' => '@informationsEquipement',
19
+ 'FETE_ET_MANIFESTATION' => '@informationsFeteEtManifestation',
20
+ 'HEBERGEMENT_COLLECTIF' => '@informationsHebergementCollectif',
21
+ 'HEBERGEMENT_LOCATIF' => '@informationsHebergementLocatif',
22
+ 'HOTELLERIE' => '@informationsHotellerie',
23
+ 'HOTELLERIE_PLEIN_AIR' => '@informationsHotelleriePleinAir',
24
+ 'PATRIMOINE_CULTUREL' => '@informationsPatrimoineCulturel',
25
+ 'PATRIMOINE_NATUREL' => '@informationsPatrimoineNaturel',
26
+ 'RESTAURATION' => '@informationsRestauration',
27
+ 'SEJOUR_PACKAGE' => '@informationsSejourPackage',
28
+ 'STRUCTURE' => '@informationsStructure',
29
+ 'TERRITOIRE' => '@informationsTerritoire'
30
+ }
31
+
32
+ DEFAULT_LIBELLE = :libelleFr
33
+
34
+ ASPECT_WINTER = 'HIVER'
35
+ ASPECT_SUMMER = 'ETE'
36
+ ASPECT_CHALLENGED = 'HANDICAP'
37
+ ASPECT_BUSINESS = 'TOURISME_AFFAIRES'
38
+ ASPECT_GROUPS = 'GROUPES'
39
+ ASPECT_ACTIVITIES = 'PRESTATAIRE_ACTIVITES'
40
+
41
+ PHONE = 201
42
+ EMAIL = 204
43
+ WEBSITE = 205
44
+
45
+ WEEKDAYS_FR = ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi']
46
+
47
+ def initialize(hash, aspect = nil)
48
+ if aspect && hash[:aspects]
49
+ aspect_data = hash[:aspects].select {|a| a[:aspect] == aspect && !a[:champsAspect].blank?}.first
50
+ self.attributes = aspect_data ? aspect_hash(hash, aspect_data) : hash
51
+ else
52
+ self.attributes = hash
53
+ end
54
+ @libelle = DEFAULT_LIBELLE
55
+ end
56
+
57
+ def aspect_hash(all_fields, aspect_fields)
58
+ all_data = all_fields.except(:aspects)
59
+ aspect_data = aspect_fields.except(:aspect, :champsAspect)
60
+ overrides = aspect_fields[:champsAspect]
61
+ overrides.each do |o|
62
+ path = o.split('.').map(&:to_sym)
63
+ if all_data.dig(path.first, *path[1..-1]) && aspect_fields.dig(path.first, *path[1..-1]).nil?
64
+ *keys, last_key = path
65
+ keys.inject(all_data, :fetch)[last_key] = nil
66
+ end
67
+ end
68
+ all_data.deep_merge(aspect_data)
69
+ end
70
+
71
+ def set_locale(locale)
72
+ unless locale.nil?
73
+ @libelle = "libelle#{locale.capitalize}".to_sym
74
+ end
75
+ end
76
+
77
+ def id
78
+ @id.to_s
79
+ end
80
+
81
+ def type
82
+ @type
83
+ end
84
+
85
+ def title
86
+ @nom[@libelle] || @nom[DEFAULT_LIBELLE]
87
+ end
88
+
89
+ def description
90
+ if @presentation[:descriptifCourt]
91
+ @presentation[:descriptifCourt][@libelle] || @presentation[:descriptifCourt][DEFAULT_LIBELLE]
92
+ end
93
+ end
94
+
95
+ def details
96
+ if @presentation[:descriptifDetaille]
97
+ @presentation[:descriptifDetaille][@libelle] || @presentation[:descriptifDetaille][DEFAULT_LIBELLE]
98
+ end
99
+ end
100
+
101
+ def contact(types_ids = [])
102
+ contact_details = {}
103
+ contact_entries = @informations[:moyensCommunication].nil? ? [] : @informations[:moyensCommunication]
104
+ contact_entries.each do |c|
105
+ if types_ids.include?(c[:type][:id])
106
+ label = c[:type][@libelle]
107
+ contact_details[label] = c[:coordonnees][:fr]
108
+ end
109
+ end
110
+ contact_details
111
+ end
112
+
113
+ def information
114
+ specific_information = {}
115
+ unless @type.nil?
116
+ specific_information = instance_variable_get(SPECIFIC_INFOS[@type])
117
+ end
118
+ @informations.merge(specific_information)
119
+ end
120
+
121
+ def pictures
122
+ (@illustrations.nil? || @illustrations.empty?) ? [{}] :
123
+ @illustrations.collect {|i| i[:traductionFichiers][0].keep_if {|k, v| k.to_s.start_with?('url')}}
124
+ end
125
+
126
+ def service_provider
127
+ if @informationsActivite && @informationsActivite[:prestataireActivites]
128
+ @informationsActivite[:prestataireActivites][:nom][@libelle]
129
+ elsif @informationsFeteEtManifestation
130
+ @informationsFeteEtManifestation[:nomLieu]
131
+ else
132
+ nil
133
+ end
134
+ end
135
+
136
+ def address_details
137
+ if @informationsActivite && @informationsActivite[:prestataireActivites]
138
+ @informationsActivite[:prestataireActivites][:adresse]
139
+ else
140
+ @localisation[:adresse]
141
+ end
142
+ end
143
+
144
+ def address
145
+ computed_address = ''
146
+ computed_address += "#{address_details[:adresse1]}, " unless address_details[:adresse1].nil?
147
+ computed_address + address_details[:commune][:nom]
148
+ end
149
+
150
+ def latitude
151
+ geoloc_details = parse_geoloc_details
152
+ geoloc_details[:valide] ? geoloc_details[:geoJson][:coordinates][1] : nil
153
+ end
154
+
155
+ def longitude
156
+ geoloc_details = parse_geoloc_details
157
+ geoloc_details[:valide] ? geoloc_details[:geoJson][:coordinates][0] : nil
158
+ end
159
+
160
+ def horaires
161
+ if @ouverture && @ouverture[:periodeEnClair]
162
+ @ouverture[:periodeEnClair][@libelle]
163
+ end
164
+ end
165
+
166
+ def open_on?(start_date, end_date)
167
+ opening_periods = @ouverture[:periodesOuvertures]
168
+ is_open = false
169
+ unless opening_periods.nil? || opening_periods.empty?
170
+ i = 0
171
+ while !is_open && i < opening_periods.length
172
+ period_start = Date.parse(opening_periods[i][:dateDebut])
173
+ period_end = Date.parse(opening_periods[i][:dateFin])
174
+ start_day = Date.parse(start_date)
175
+ end_day = [Date.parse(end_date), start_day.next_month].min
176
+ if ranges_intersect(period_start, period_end, start_day, end_day, opening_periods[i][:tousLesAns])
177
+ is_open = (start_day..end_day).to_a.any? {|d| open_day_predicate(opening_periods[i]).call(d)}
178
+ end
179
+ i += 1
180
+ end
181
+ end
182
+ is_open
183
+ end
184
+
185
+ def tarif
186
+ if @descriptionTarif
187
+ if @descriptionTarif[:gratuit]
188
+ return 'gratuit'
189
+ elsif @descriptionTarif[:tarifsEnClair]
190
+ @descriptionTarif[:tarifsEnClair][@libelle]
191
+ end
192
+ end
193
+ end
194
+
195
+ def population
196
+ eligible_populations = []
197
+ if @prestations && @prestations[:typesClientele]
198
+ eligible_populations += @prestations[:typesClientele].collect {|t| t[@libelle]}
199
+ end
200
+ eligible_populations.uniq
201
+ end
202
+
203
+ def prestations(prestation_type)
204
+ presta = {}
205
+ unless @prestations.nil? || @prestations[prestation_type].nil?
206
+ @prestations[prestation_type].each do |item|
207
+ value = item[@libelle]
208
+ if item[:familleCritere].nil?
209
+ key = 'Autre'
210
+ else
211
+ key = item[:familleCritere][:libelleFr]
212
+ end
213
+ presta[key] ||= []
214
+ presta[key] << value
215
+ end
216
+ end
217
+ presta
218
+ end
219
+
220
+ def environments
221
+ @localisation && @localisation[:environnements] && @localisation[:environnements].collect {|e| e[@libelle]}
222
+ end
223
+
224
+ def additional_criteria
225
+ @presentation && @presentation[:typologiesPromoSitra] && @presentation[:typologiesPromoSitra].collect {|t| t[@libelle]}
226
+ end
227
+
228
+ def resa
229
+ if @reservation
230
+ @reservation[:organismes]
231
+ end
232
+ end
233
+
234
+ def multimedias
235
+ @multimedias
236
+ end
237
+
238
+ def reservation
239
+ if @reservation[:complement]
240
+ @reservation[:complement][:libelleFr] || @reservation[:complement][DEFAULT_LIBELLE]
241
+ end
242
+ end
243
+
244
+ def bonplan
245
+ if @presentation[:bonsPlans]
246
+ @presentation[:bonsPlans][@libelle] || @presentation[:bonsPlans][DEFAULT_LIBELLE]
247
+ end
248
+ end
249
+
250
+ def cpltaccueil
251
+ @prestations && @prestations[:complementAccueil] && @prestations[:complementAccueil][:libelleFr]
252
+ end
253
+
254
+ def accessibilite
255
+ @prestations && @prestations[:tourismesAdaptes] && @prestations[:tourismesAdaptes].collect {|t| t[@libelle]}
256
+ end
257
+
258
+ def ranges_intersect(ref_start_day, ref_end_day, other_start_day, other_end_day, ignore_year = false)
259
+ has_intersection = other_start_day.between?(ref_start_day, ref_end_day) || other_end_day.between?(ref_start_day, ref_end_day) ||
260
+ ref_start_day.between?(other_start_day, other_end_day) || ref_end_day.between?(other_start_day, other_end_day)
261
+ if !has_intersection && ignore_year && ref_start_day <= other_end_day
262
+ ref_start_day = ref_start_day.next_year
263
+ ref_end_day = ref_end_day.next_year
264
+ has_intersection = ranges_intersect(ref_start_day, ref_end_day, other_start_day, other_end_day, true)
265
+ end
266
+ has_intersection
267
+ end
268
+
269
+ def open_day_predicate(opening_period)
270
+ is_eligible = lambda {|date| return true}
271
+ case(opening_period[:type])
272
+ when 'OUVERTURE_SAUF'
273
+ excluded_days = opening_period[:ouverturesJournalieres].collect {|o| WEEKDAYS_FR.index(o[:jour].downcase)}
274
+ is_eligible = lambda {|date| return !excluded_days.include?(date.wday)}
275
+ when 'OUVERTURE_TOUS_LES_JOURS'
276
+ when 'OUVERTURE_SEMAINE'
277
+ unless opening_period[:ouverturesJournalieres][0][:jour] == 'TOUS'
278
+ included_days = opening_period[:ouverturesJournalieres].collect {|o| WEEKDAYS_FR.index(o[:jour].downcase)}
279
+ is_eligible = lambda {|date| return included_days.include?(date.wday)}
280
+ end
281
+ when 'OUVERTURE_MOIS'
282
+ opening_days = {}
283
+ unless opening_period[:ouverturesJourDuMois].nil?
284
+ opening_period[:ouverturesJourDuMois].each {|o| opening_days[o[:jour].downcase] = o[:jourDuMois]}
285
+ end
286
+ if opening_days.keys.include?('tous')
287
+ is_eligible = lambda {|date| return occurrence_in_month(date) == opening_days['tous']}
288
+ else
289
+ included_weekdays = opening_days.keys.collect {|d| WEEKDAYS_FR.index(d)}
290
+ is_eligible = lambda {|date| return included_weekdays.include?(date.wday) && occurrence_in_month(date) == opening_days[WEEKDAYS_FR[date.wday]]}
291
+ end
292
+ else
293
+ # Unsupported
294
+ end
295
+ is_eligible
296
+ end
297
+
298
+ private
299
+
300
+ def occurrence_in_month(date)
301
+ case date.mday
302
+ when 1..7
303
+ 'D_1ER'
304
+ when 8..14
305
+ 'D_2EME'
306
+ when 15..21
307
+ 'D_3EME'
308
+ when 22..28
309
+ 'D_4EME'
310
+ else
311
+ 'D_DERNIER'
312
+ end
313
+ end
314
+
315
+ def parse_geoloc_details
316
+ if @informationsActivite.nil? || @informationsActivite[:prestataireActivites].nil?
317
+ geoloc_details = @localisation[:geolocalisation]
318
+ else
319
+ geoloc_details = @informationsActivite[:prestataireActivites][:geolocalisation]
320
+ end
321
+ geoloc_details
322
+ end
323
+
296
324
  end