Class: Apidae::Object

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

Constant Summary collapse

TYPES_DATA =
{
    'ACTIVITE' => {node: :informationsActivite, subtype: :activiteType},
    'COMMERCE_ET_SERVICE' => {node: :informationsCommerceEtService, subtype: :commerceEtServiceType},
    'DEGUSTATION' => {node: :informationsDegustation, subtype: :degustationType},
    'DOMAINE_SKIABLE' => {node: :informationsDomaineSkiable, subtype: :domaineSkiableType},
    'EQUIPEMENT' => {node: :informationsEquipement, subtype: :equipementType},
    'FETE_ET_MANIFESTATION' => {node: :informationsFeteEtManifestation, subtype: :feteEtManifestationType},
    'HEBERGEMENT_COLLECTIF' => {node: :informationsHebergementCollectif, subtype: :hebergementCollectifType},
    'HEBERGEMENT_LOCATIF' => {node: :informationsHebergementLocatif, subtype: :hebergementLocatifType},
    'HOTELLERIE' => {node: :informationsHotellerie, subtype: :hotellerieType},
    'HOTELLERIE_PLEIN_AIR' => {node: :informationsHotelleriePleinAir, subtype: :hotelleriePleinAirType},
    'PATRIMOINE_CULTUREL' => {node: :informationsPatrimoineCulturel, subtype: :patrimoineCulturelType},
    'PATRIMOINE_NATUREL' => {node: :informationsPatrimoineNaturel, subtype: :patrimoineNaturelType},
    'RESTAURATION' => {node: :informationsRestauration, subtype: :restaurationType},
    'SEJOUR_PACKAGE' => {node: :informationsSejourPackage, subtype: :sejourPackageType},
    'STRUCTURE' => {node: :informationsStructure, subtype: :structureType},
    'TERRITOIRE' => {node: :informationsTerritoire, subtype: :territoireType}
}
PHONE =
201
EMAIL =
204
WEBSITE =
205

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.address(address_hash) ⇒ Object



147
148
149
150
151
152
153
# File 'app/models/apidae/object.rb', line 147

def self.address(address_hash)
  computed_address = []
  computed_address << address_hash[:adresse1] unless address_hash[:adresse1].blank?
  computed_address << address_hash[:adresse2] unless address_hash[:adresse2].blank?
  computed_address << address_hash[:adresse3] unless address_hash[:adresse3].blank?
  {address_fields: computed_address}
end

.contact(information_hash) ⇒ Object



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

def self.contact(information_hash)
  contact_details = {}
  contact_entries = information_hash[:moyensCommunication].nil? ? [] : information_hash[:moyensCommunication]
  contact_entries.each do |c|
    case c[:type][:id]
      when PHONE
        contact_details[:telephone] = c[:coordonnees][:fr]
      when EMAIL
        contact_details[:email] = c[:coordonnees][:fr]
      when WEBSITE
        contact_details[:website] = c[:coordonnees][:fr]
      else
    end
  end
  contact_details
end

.import(json_dir) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/apidae/object.rb', line 38

def self.import(json_dir)
  result = false
  if Dir.exist?(json_dir)
    Dir.foreach(json_dir) do |f|
      if f.end_with?('.json')
        json_file = File.join(json_dir, f)
        objects_json = File.read(json_file)
        objects_hashes = JSON.parse(objects_json, symbolize_names: true)
        objects_hashes.each do |object_data|
          type_fields = TYPES_DATA[object_data[:type]]
          existing_obj = Apidae::Object.find_by_apidae_id(object_data[:id])
          unless existing_obj
            Apidae::Object.create!(
                apidae_id: object_data[:id],
                apidae_type: object_data[:type],
                apidae_subtype: node_value(object_data[type_fields[:node]], object_data[type_fields[:subtype]]),
                title: node_value(object_data, :nom),
                short_desc: node_value(object_data[:presentation], :descriptifCourt),
                long_desc: node_value(object_data[:presentation], :descriptifDetaille),
                contact: contact(object_data[:informations]),
                address: address(object_data[:localisation][:adresse]),
                town: town(object_data[:localisation][:adresse]),
                latitude: latitude(object_data[:localisation]),
                longitude: longitude(object_data[:localisation]),
                openings: openings(object_data[:ouverture]),
                rates: rates(object_data[:descriptionTarif]),
                reservation: reservation(object_data[:reservation]),
                type_data: object_data[type_fields[:node]],
                pictures_data: pictures_urls(object_data[:illustrations])
            )
          end
        end
      end
      result = true
    end
    result
  end
end

.latitude(location_hash) ⇒ Object



159
160
161
162
# File 'app/models/apidae/object.rb', line 159

def self.latitude(location_hash)
  geoloc_details = location_hash[:geolocalisation]
  (geoloc_details && geoloc_details[:valide] && geoloc_details[:geoJson]) ? geoloc_details[:geoJson][:coordinates][1] : nil
end

.load_picturesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/apidae/object.rb', line 99

def self.load_pictures
  Object.all.each do |obj|
    if obj.apidae_attached_files.blank? && obj.pictures.any?
      obj.pictures.each do |pic|
        begin
          attached = AttachedFile.new(apidae_object_id: id, name: pic[:name], picture: URI.parse(pic[:url]),
                                      description: pic[:description], credits: pic[:credits])
          attached.save
        rescue OpenURI::HTTPError => e
          puts "Could not retrieve attached picture for object #{title} - Error is #{e.message}"
        end
      end
    end
  end
end

.longitude(location_hash) ⇒ Object



164
165
166
167
# File 'app/models/apidae/object.rb', line 164

def self.longitude(location_hash)
  geoloc_details = location_hash[:geolocalisation]
  (geoloc_details && geoloc_details[:valide] && geoloc_details[:geoJson]) ? geoloc_details[:geoJson][:coordinates][0] : nil
end

.openings(openings_hash) ⇒ Object



169
170
171
172
173
# File 'app/models/apidae/object.rb', line 169

def self.openings(openings_hash)
  if openings_hash && openings_hash[:periodeEnClair]
    {description: openings_hash[:periodeEnClair][:libelleFr], opening_periods: openings_hash[:periodesOuvertures]}
  end
end

.pictures_urls(pictures_array) ⇒ Object



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

def self.pictures_urls(pictures_array)
  pics_data = []
  unless pictures_array.blank?
    pictures_array.select { |p| p.is_a?(Hash) && !p[:traductionFichiers].blank? }.each do |pic|
      pics_data << {
          name: node_value(pic, :nom),
          url: pic[:traductionFichiers][0][:url],
          description: node_value(pic, :legende),
          credits: node_value(pic, :copyright)
      }
    end
  end
  {pictures: pics_data}
end

.rates(rates_hash) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'app/models/apidae/object.rb', line 175

def self.rates(rates_hash)
  if rates_hash
    if rates_hash[:gratuit]
      return 'gratuit'
    elsif rates_hash[:tarifsEnClair]
      rates_hash[:tarifsEnClair][:libelleFr]
    end
  end
end

.reservation(reservation_hash) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'app/models/apidae/object.rb', line 185

def self.reservation(reservation_hash)
  if reservation_hash
    if reservation_hash[:complement]
      reservation_hash[:complement][:libelleFr]
    else
      reservation_hash[:organismes]
    end
  end
end

.town(address_hash) ⇒ Object



155
156
157
# File 'app/models/apidae/object.rb', line 155

def self.town(address_hash)
  address_hash[:commune] ? Town.find_by_apidae_id(address_hash[:commune][:id]) : nil
end

.update_fields(json_dir) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/apidae/object.rb', line 77

def self.update_fields(json_dir)
  result = false
  if Dir.exist?(json_dir)
    Dir.foreach(json_dir) do |f|
      if f.end_with?('.json')
        json_file = File.join(json_dir, f)
        objects_json = File.read(json_file)
        objects_hashes = JSON.parse(objects_json, symbolize_names: true)
        objects_hashes.each do |object_data|
          obj = Apidae::Object.find_by_apidae_id(object_data[:id])
          if obj
            yield(obj, object_data)
            obj.save!
          end
        end
      end
      result = true
    end
    result
  end
end

Instance Method Details

#contact_textObject



195
196
197
198
199
200
201
# File 'app/models/apidae/object.rb', line 195

def contact_text
  entries = []
  JSON.parse(contact).each_pair do |k, v|
    entries << "#{k}: #{v}"
  end
  entries.join("\n")
end

#main_pictureObject



203
204
205
# File 'app/models/apidae/object.rb', line 203

def main_picture
  pictures.any? ? pictures[0][:url] : "/#{Rails.application.config.apidae_pictures_path}/default/logo.png"
end