apidae 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/apidae/file_import.rb +116 -0
- data/app/models/apidae/object.rb +57 -102
- data/app/models/apidae/selection.rb +15 -19
- data/db/migrate/20170730102424_create_apidae_file_imports.rb +13 -0
- data/lib/apidae/version.rb +1 -1
- data/test/models/apidae/file_import_test.rb +9 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c68638a33f855dbe14caf23d4bb0fa842e8a0263
|
4
|
+
data.tar.gz: 05978a1df57ca66bd96f42d66b8141e8c5c096f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 071dc1136e326c7a3370055203ed04eae6954394a65d0f83194a3c9118199c51e4c17650acf0d09931ee43234175365af1cd58c10bec596a19e7b8c7acc19813
|
7
|
+
data.tar.gz: 6c82209df78847b4d22b5d42119cc7c664184ced8a067d7a24f6356cebbe7ee29a3b8213b9ecae14620d6e7e00784231a42d98607cf52a6a8e862c9060df936f
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module Apidae
|
2
|
+
class FileImport < ActiveRecord::Base
|
3
|
+
|
4
|
+
STATUS_PENDING = 'pending'
|
5
|
+
STATUS_COMPLETE = 'complete'
|
6
|
+
STATUS_CANCELLED = 'cancelled'
|
7
|
+
|
8
|
+
MODIFIED_DIR = 'objets_modifies'
|
9
|
+
DELETED_FILE = 'objets_supprimes.json'
|
10
|
+
SELECTIONS_FILE = 'selections.json'
|
11
|
+
|
12
|
+
def self.import(dir)
|
13
|
+
puts 'Update results : '
|
14
|
+
puts import_updates(File.join(dir, MODIFIED_DIR))
|
15
|
+
puts '------'
|
16
|
+
puts 'Deletion results : '
|
17
|
+
puts import_deletions(File.join(dir, DELETED_FILE))
|
18
|
+
puts '------'
|
19
|
+
puts 'Selection results : '
|
20
|
+
puts import_selections(File.join(dir,SELECTIONS_FILE))
|
21
|
+
puts '------'
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.import_updates(json_dir)
|
25
|
+
result = {success: false, created: 0, updated: 0}
|
26
|
+
if Dir.exist?(json_dir)
|
27
|
+
Dir.foreach(json_dir) do |f|
|
28
|
+
if f.end_with?('.json')
|
29
|
+
json_file = File.join(json_dir, f)
|
30
|
+
objects_json = File.read(json_file)
|
31
|
+
objects_hashes = JSON.parse(objects_json, symbolize_names: true)
|
32
|
+
objects_hashes.each do |object_data|
|
33
|
+
existing = Apidae::Object.find_by_apidae_id(object_data[:id])
|
34
|
+
if existing
|
35
|
+
Apidae::Object.update_object(existing, object_data)
|
36
|
+
result[:updated] += 1
|
37
|
+
else
|
38
|
+
Apidae::Object.add_object(object_data)
|
39
|
+
result[:created] += 1
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
result[:success] = true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
result
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def self.import_deletions(json_file)
|
51
|
+
result = {success: false, deleted: 0}
|
52
|
+
if File.exist?(json_file)
|
53
|
+
deleted_json = File.read(json_file)
|
54
|
+
deleted_ids = JSON.parse(deleted_json)
|
55
|
+
deleted_ids.each do |id|
|
56
|
+
obj = Apidae::Object.find_by_apidae_id(id)
|
57
|
+
if obj
|
58
|
+
obj.destroy!
|
59
|
+
result[:deleted] += 1
|
60
|
+
else
|
61
|
+
puts "skipping object deletion : #{id}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
result[:success] = true
|
65
|
+
end
|
66
|
+
result
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.update_fields(json_dir)
|
70
|
+
result = false
|
71
|
+
if Dir.exist?(json_dir)
|
72
|
+
Dir.foreach(json_dir) do |f|
|
73
|
+
if f.end_with?('.json')
|
74
|
+
json_file = File.join(json_dir, f)
|
75
|
+
objects_json = File.read(json_file)
|
76
|
+
objects_hashes = JSON.parse(objects_json, symbolize_names: true)
|
77
|
+
objects_hashes.each do |object_data|
|
78
|
+
obj = Apidae::Object.find_by_apidae_id(object_data[:id])
|
79
|
+
if obj
|
80
|
+
yield(obj, object_data)
|
81
|
+
obj.save!
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
result = true
|
86
|
+
end
|
87
|
+
result
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.load_pictures
|
92
|
+
Apidae::Object.all.each do |obj|
|
93
|
+
if obj.apidae_attached_files.blank? && obj.pictures.any?
|
94
|
+
obj.pictures.each do |pic|
|
95
|
+
begin
|
96
|
+
attached = Apidae::AttachedFile.new(apidae_object_id: id, name: pic[:name], picture: URI.parse(pic[:url]),
|
97
|
+
description: pic[:description], credits: pic[:credits])
|
98
|
+
attached.save
|
99
|
+
rescue OpenURI::HTTPError => e
|
100
|
+
puts "Could not retrieve attached picture for object #{title} - Error is #{e.message}"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.import_selections(json_file)
|
108
|
+
selections_json = File.read(json_file)
|
109
|
+
selections_hashes = JSON.parse(selections_json, symbolize_names: true)
|
110
|
+
selections_hashes.each do |selection_data|
|
111
|
+
Apidae::Selection.add_or_update(selection_data)
|
112
|
+
end
|
113
|
+
Apidae::Selection.all.collect {|sel| {id: sel.id, apidae_id: sel.apidae_id, reference: sel.reference, objects: sel.objects.count}}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/app/models/apidae/object.rb
CHANGED
@@ -12,115 +12,70 @@ module Apidae
|
|
12
12
|
store :address, accessors: [:address_fields], coder: JSON
|
13
13
|
store :openings, accessors: [:description, :opening_periods], coder: JSON
|
14
14
|
|
15
|
+
ACT = 'ACTIVITE'
|
16
|
+
COS = 'COMMERCE_ET_SERVICE'
|
17
|
+
DEG = 'DEGUSTATION'
|
18
|
+
DOS = 'DOMAINE_SKIABLE'
|
19
|
+
EQU = 'EQUIPEMENT'
|
20
|
+
FEM = 'FETE_ET_MANIFESTATION'
|
21
|
+
HCO = 'HEBERGEMENT_COLLECTIF'
|
22
|
+
HLO = 'HEBERGEMENT_LOCATIF'
|
23
|
+
HOT = 'HOTELLERIE'
|
24
|
+
HPA = 'HOTELLERIE_PLEIN_AIR'
|
25
|
+
PCU = 'PATRIMOINE_CULTUREL'
|
26
|
+
PNA = 'PATRIMOINE_NATUREL'
|
27
|
+
RES = 'RESTAURATION'
|
28
|
+
SPA = 'SEJOUR_PACKAGE'
|
29
|
+
STR = 'STRUCTURE'
|
30
|
+
TER = 'TERRITOIRE'
|
31
|
+
|
15
32
|
TYPES_DATA = {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
ACT => {node: :informationsActivite, subtype: :activiteType},
|
34
|
+
COS => {node: :informationsCommerceEtService, subtype: :commerceEtServiceType},
|
35
|
+
DEG => {node: :informationsDegustation, subtype: :degustationType},
|
36
|
+
DOS => {node: :informationsDomaineSkiable, subtype: :domaineSkiableType},
|
37
|
+
EQU => {node: :informationsEquipement, subtype: :equipementType},
|
38
|
+
FEM => {node: :informationsFeteEtManifestation, subtype: :feteEtManifestationType},
|
39
|
+
HCO => {node: :informationsHebergementCollectif, subtype: :hebergementCollectifType},
|
40
|
+
HLO => {node: :informationsHebergementLocatif, subtype: :hebergementLocatifType},
|
41
|
+
HOT => {node: :informationsHotellerie, subtype: :hotellerieType},
|
42
|
+
HPA => {node: :informationsHotelleriePleinAir, subtype: :hotelleriePleinAirType},
|
43
|
+
PCU => {node: :informationsPatrimoineCulturel, subtype: :patrimoineCulturelType},
|
44
|
+
PNA => {node: :informationsPatrimoineNaturel, subtype: :patrimoineNaturelType},
|
45
|
+
RES => {node: :informationsRestauration, subtype: :restaurationType},
|
46
|
+
SPA => {node: :informationsSejourPackage, subtype: :sejourPackageType},
|
47
|
+
STR => {node: :informationsStructure, subtype: :structureType},
|
48
|
+
TER => {node: :informationsTerritoire, subtype: :territoireType}
|
32
49
|
}
|
33
50
|
|
34
51
|
PHONE = 201
|
35
52
|
EMAIL = 204
|
36
53
|
WEBSITE = 205
|
37
54
|
|
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
|
-
apidae_obj.reservation = reservation(object_data[:reservation])
|
63
|
-
apidae_obj.type_data = object_data[type_fields[:node]]
|
64
|
-
apidae_obj.pictures_data = pictures_urls(object_data[:illustrations])
|
65
|
-
apidae_obj.entity_data = entity_fields(object_data[:informations])
|
66
|
-
apidae_obj.save!
|
67
|
-
end
|
68
|
-
end
|
69
|
-
result = true
|
70
|
-
end
|
71
|
-
result
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def self.import_deletions(json_file)
|
76
|
-
result = false
|
77
|
-
if File.exist?(json_file)
|
78
|
-
deleted_json = File.read(json_file)
|
79
|
-
deleted_ids = JSON.parse(deleted_json)
|
80
|
-
deleted_ids.each do |id|
|
81
|
-
Apidae::Object.find_by_apidae_id(id).destroy
|
82
|
-
end
|
83
|
-
result = true
|
84
|
-
end
|
85
|
-
result
|
86
|
-
end
|
87
|
-
|
88
|
-
def self.update_fields(json_dir)
|
89
|
-
result = false
|
90
|
-
if Dir.exist?(json_dir)
|
91
|
-
Dir.foreach(json_dir) do |f|
|
92
|
-
if f.end_with?('.json')
|
93
|
-
json_file = File.join(json_dir, f)
|
94
|
-
objects_json = File.read(json_file)
|
95
|
-
objects_hashes = JSON.parse(objects_json, symbolize_names: true)
|
96
|
-
objects_hashes.each do |object_data|
|
97
|
-
obj = Apidae::Object.find_by_apidae_id(object_data[:id])
|
98
|
-
if obj
|
99
|
-
yield(obj, object_data)
|
100
|
-
obj.save!
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
result = true
|
105
|
-
end
|
106
|
-
result
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
def self.load_pictures
|
111
|
-
Object.all.each do |obj|
|
112
|
-
if obj.apidae_attached_files.blank? && obj.pictures.any?
|
113
|
-
obj.pictures.each do |pic|
|
114
|
-
begin
|
115
|
-
attached = AttachedFile.new(apidae_object_id: id, name: pic[:name], picture: URI.parse(pic[:url]),
|
116
|
-
description: pic[:description], credits: pic[:credits])
|
117
|
-
attached.save
|
118
|
-
rescue OpenURI::HTTPError => e
|
119
|
-
puts "Could not retrieve attached picture for object #{title} - Error is #{e.message}"
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
55
|
+
def self.add_object(object_data)
|
56
|
+
apidae_obj = Apidae::Object.new(apidae_id: object_data[:id])
|
57
|
+
update_object(apidae_obj, object_data)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.update_object(apidae_obj, object_data)
|
61
|
+
type_fields = TYPES_DATA[object_data[:type]]
|
62
|
+
apidae_obj.apidae_type = object_data[:type]
|
63
|
+
apidae_obj.apidae_subtype = node_value(object_data[type_fields[:node]], object_data[type_fields[:subtype]])
|
64
|
+
apidae_obj.title = node_value(object_data, :nom)
|
65
|
+
apidae_obj.short_desc = node_value(object_data[:presentation], :descriptifCourt)
|
66
|
+
apidae_obj.long_desc = node_value(object_data[:presentation], :descriptifDetaille)
|
67
|
+
apidae_obj.contact = contact(object_data[:informations])
|
68
|
+
apidae_obj.address = address(object_data[:localisation][:adresse])
|
69
|
+
apidae_obj.town = town(object_data[:localisation][:adresse])
|
70
|
+
apidae_obj.latitude = latitude(object_data[:localisation])
|
71
|
+
apidae_obj.longitude = longitude(object_data[:localisation])
|
72
|
+
apidae_obj.openings = openings(object_data[:ouverture])
|
73
|
+
apidae_obj.rates = rates(object_data[:descriptionTarif])
|
74
|
+
apidae_obj.reservation = reservation(object_data[:reservation])
|
75
|
+
apidae_obj.type_data = object_data[type_fields[:node]]
|
76
|
+
apidae_obj.pictures_data = pictures_urls(object_data[:illustrations])
|
77
|
+
apidae_obj.entity_data = entity_fields(object_data[:informations])
|
78
|
+
apidae_obj.save!
|
124
79
|
end
|
125
80
|
|
126
81
|
def self.pictures_urls(pictures_array)
|
@@ -5,28 +5,24 @@ module Apidae
|
|
5
5
|
MAX_COUNT = 100
|
6
6
|
MAX_LOOPS = 10
|
7
7
|
|
8
|
-
def self.
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
apidae_sel = Apidae::Selection.first_or_initialize(apidae_id: selection_data[:id])
|
13
|
-
apidae_sel.label = selection_data[:nom]
|
14
|
-
apidae_sel.save!
|
8
|
+
def self.add_or_update(selection_data)
|
9
|
+
apidae_sel = Apidae::Selection.where(apidae_id: selection_data[:id]).first_or_initialize
|
10
|
+
apidae_sel.label = selection_data[:nom]
|
11
|
+
apidae_sel.save!
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
current_objs = apidae_sel.objects.collect {|obj| obj.apidae_id}
|
14
|
+
imported_objs = selection_data[:objetsTouristiques].blank? ? [] : selection_data[:objetsTouristiques].collect {|obj| obj[:id]}
|
15
|
+
added = imported_objs - current_objs
|
16
|
+
removed = current_objs - imported_objs
|
20
17
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
added.each do |o|
|
19
|
+
apidae_object = Apidae::Object.find_by_apidae_id(o)
|
20
|
+
apidae_sel.objects << apidae_object if apidae_object
|
21
|
+
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
23
|
+
removed.each do |o|
|
24
|
+
apidae_object = Apidae::Object.find_by_apidae_id(o)
|
25
|
+
apidae_sel.objects.delete(apidae_object) if apidae_object
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CreateApidaeFileImports < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :apidae_file_imports do |t|
|
4
|
+
t.string :status
|
5
|
+
t.string :remote_file
|
6
|
+
t.integer :created
|
7
|
+
t.integer :updated
|
8
|
+
t.integer :deleted
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/apidae/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apidae
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean-Baptiste Vilain
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- app/helpers/apidae/objects_helper.rb
|
72
72
|
- app/helpers/apidae/selections_helper.rb
|
73
73
|
- app/models/apidae/attached_file.rb
|
74
|
+
- app/models/apidae/file_import.rb
|
74
75
|
- app/models/apidae/object.rb
|
75
76
|
- app/models/apidae/selection.rb
|
76
77
|
- app/models/apidae/town.rb
|
@@ -95,6 +96,7 @@ files:
|
|
95
96
|
- db/migrate/20170513121215_create_apidae_attached_files.rb
|
96
97
|
- db/migrate/20170513205932_rename_objects_selections_table.rb
|
97
98
|
- db/migrate/20170720161134_add_entity_data_to_objects.rb
|
99
|
+
- db/migrate/20170730102424_create_apidae_file_imports.rb
|
98
100
|
- lib/apidae.rb
|
99
101
|
- lib/apidae/engine.rb
|
100
102
|
- lib/apidae/version.rb
|
@@ -146,6 +148,7 @@ files:
|
|
146
148
|
- test/fixtures/apidae/towns.yml
|
147
149
|
- test/integration/navigation_test.rb
|
148
150
|
- test/models/apidae/attached_file_test.rb
|
151
|
+
- test/models/apidae/file_import_test.rb
|
149
152
|
- test/models/apidae/object_test.rb
|
150
153
|
- test/models/apidae/selection_test.rb
|
151
154
|
- test/models/apidae/town_test.rb
|
@@ -222,6 +225,7 @@ test_files:
|
|
222
225
|
- test/fixtures/apidae/towns.yml
|
223
226
|
- test/integration/navigation_test.rb
|
224
227
|
- test/models/apidae/attached_file_test.rb
|
228
|
+
- test/models/apidae/file_import_test.rb
|
225
229
|
- test/models/apidae/object_test.rb
|
226
230
|
- test/models/apidae/selection_test.rb
|
227
231
|
- test/models/apidae/town_test.rb
|