apidae 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/apidae/import_controller.rb +2 -40
- data/app/controllers/apidae/objects_controller.rb +9 -10
- data/app/helpers/apidae/application_helper.rb +1 -1
- data/app/models/apidae/apidae_data_parser.rb +5 -4
- data/app/models/apidae/export.rb +41 -0
- data/app/models/apidae/obj.rb +1 -1
- data/app/models/apidae/selection.rb +12 -7
- data/lib/apidae/version.rb +1 -1
- data/test/dummy/log/development.log +2 -0
- metadata +12 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11f5031e46f8bfa23ce228c242e023c4ebe10e0e
|
4
|
+
data.tar.gz: '0083f65ff4cfbd3f53c5a13126ca940c4be696ae'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a6544dae89ca3cee7525b37433fc6941b5ae76d720f2adce045d3357fd65efca3150885b0cb9da8234a5324218448e57545897865a77896e1bdfe7a462ab31b
|
7
|
+
data.tar.gz: 9c51ebdb6c1d9d671499fd60f086a8c43feb94a9c77d06108aa030b73d5bce00ad4fd5b58e0f5467d21a509bcc1594c2c59f6d19e4be2f61a78d76fc04037b3b
|
@@ -37,7 +37,7 @@ module Apidae
|
|
37
37
|
def run
|
38
38
|
success = true
|
39
39
|
Export.pending.each do |e|
|
40
|
-
success &&= import_data
|
40
|
+
success &&= e.import_data
|
41
41
|
end
|
42
42
|
success ? head(:ok) : head(:internal_server_error)
|
43
43
|
end
|
@@ -48,7 +48,7 @@ module Apidae
|
|
48
48
|
|
49
49
|
def create
|
50
50
|
@export = Export.new(export_params)
|
51
|
-
if @export.save &&
|
51
|
+
if @export.save && @export.import_data
|
52
52
|
redirect_to apidae.root_url, notice: 'Le fichier a bien été importé.'
|
53
53
|
else
|
54
54
|
flash.now[:alert] = "Une erreur s'est produite lors de l'import du fichier."
|
@@ -61,43 +61,5 @@ module Apidae
|
|
61
61
|
def export_params
|
62
62
|
params.require(:export).permit(:project_id, :file_url, :status)
|
63
63
|
end
|
64
|
-
|
65
|
-
def import_data(e)
|
66
|
-
success = true
|
67
|
-
begin
|
68
|
-
open(e.file_url) do |f|
|
69
|
-
begin
|
70
|
-
FileImport.import(f, e.project_id)
|
71
|
-
unless e.confirm_url.blank?
|
72
|
-
uri = URI(e.confirm_url)
|
73
|
-
req = Net::HTTP::Post.new(uri)
|
74
|
-
Net::HTTP.start(uri.hostname, uri.port) do |http|
|
75
|
-
http.request(req)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
e.update(status: Export::COMPLETE)
|
79
|
-
if Rails.application.config.respond_to?(:apidae_import_callback)
|
80
|
-
Rails.application.config.apidae_import_callback.call(e)
|
81
|
-
end
|
82
|
-
rescue Exception => ex
|
83
|
-
logger.error("Failed to import export file : #{e.file_url}")
|
84
|
-
logger.error("Error is : #{ex} \n#{ex.backtrace.join("\n") unless ex.backtrace.blank?}")
|
85
|
-
success = false
|
86
|
-
e.update(status: Export::CANCELLED)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
rescue OpenURI::HTTPError => err
|
90
|
-
logger.error("Failed to download export file : #{e.file_url}")
|
91
|
-
logger.error("Error is : #{err}")
|
92
|
-
success = false
|
93
|
-
e.update(status: Export::CANCELLED)
|
94
|
-
rescue Exception => e
|
95
|
-
logger.error "Failed to import file : #{e.file_url}"
|
96
|
-
logger.error("Error is : #{err}")
|
97
|
-
success = false
|
98
|
-
e.update(status: Export::CANCELLED)
|
99
|
-
end
|
100
|
-
success
|
101
|
-
end
|
102
64
|
end
|
103
65
|
end
|
@@ -34,17 +34,15 @@ module Apidae
|
|
34
34
|
def create
|
35
35
|
@obj = Obj.new(object_params)
|
36
36
|
if Obj.find_by_apidae_id(@obj.apidae_id)
|
37
|
-
redirect_to objects_url, alert: "Cet objet est déjà importé."
|
38
|
-
|
39
|
-
selection =
|
40
|
-
if @obj.save && selection.add_or_refresh_obj(@obj.apidae_id)
|
41
|
-
redirect_to objects_url, notice: "L'objet a bien été importé"
|
42
|
-
else
|
43
|
-
flash[:alert] = "Une erreur s'est produite lors de l'import de l'objet."
|
44
|
-
render :new
|
37
|
+
redirect_to objects_url, alert: "Cet objet est déjà importé." and return
|
38
|
+
elsif !params[:selection_apidae_id].blank?
|
39
|
+
selection = Selection.find_by_apidae_id(params[:selection_apidae_id])
|
40
|
+
if selection && selection.valid_api? && @obj.save && selection.add_or_refresh_obj(@obj.apidae_id)
|
41
|
+
redirect_to objects_url, notice: "L'objet a bien été importé" and return
|
45
42
|
end
|
46
43
|
end
|
47
|
-
|
44
|
+
flash[:alert] = "Une erreur s'est produite lors de l'import de l'objet."
|
45
|
+
render :new
|
48
46
|
end
|
49
47
|
|
50
48
|
def update
|
@@ -63,7 +61,8 @@ module Apidae
|
|
63
61
|
def refresh
|
64
62
|
referrer = (session.delete(:referrer) || objects_url)
|
65
63
|
begin
|
66
|
-
|
64
|
+
sel = @obj.selections.find {|s| s.valid_api?}
|
65
|
+
if sel && sel.add_or_refresh_obj(@obj.apidae_id)
|
67
66
|
redirect_to referrer, notice: "L'objet touristique a bien été mis à jour."
|
68
67
|
else
|
69
68
|
redirect_to referrer, alert: "Une erreur s'est produite lors de la mise à jour de l'objet."
|
@@ -245,13 +245,14 @@ module Apidae
|
|
245
245
|
def self.parse_rates(rates_hash, *locales)
|
246
246
|
if rates_hash
|
247
247
|
desc = rates_hash[:gratuit] ? {DEFAULT_LOCALE => 'gratuit'} : node_value(rates_hash, :tarifsEnClair, *locales)
|
248
|
-
values = rates_hash[:periodes].blank? ? [] : rates_hash[:periodes].map {|p| build_rate(p)}
|
248
|
+
values = rates_hash[:periodes].blank? ? [] : rates_hash[:periodes].map {|p| build_rate(p, *locales)}
|
249
249
|
methods = rates_hash[:modesPaiement].blank? ? [] : rates_hash[:modesPaiement].map {|p| p[:id]}
|
250
250
|
{
|
251
251
|
rates_desc: desc, rates: values, payment_methods: methods,
|
252
252
|
rates_desc_mode: rates_hash[:tarifsEnClairGenerationMode] == 'AUTOMATIQUE' ? MODE_AUTO : MODE_MANUAL,
|
253
253
|
includes: node_value(rates_hash, :leTarifComprend, *locales),
|
254
|
-
excludes: node_value(rates_hash, :leTarifNeComprendPas, *locales)
|
254
|
+
excludes: node_value(rates_hash, :leTarifNeComprendPas, *locales),
|
255
|
+
rates_extra: node_value(rates_hash, :complement, *locales)
|
255
256
|
}
|
256
257
|
end
|
257
258
|
end
|
@@ -346,10 +347,10 @@ module Apidae
|
|
346
347
|
|
347
348
|
private
|
348
349
|
|
349
|
-
def self.build_rate(rate_period)
|
350
|
+
def self.build_rate(rate_period, *locales)
|
350
351
|
{
|
351
352
|
id: rate_period[:identifiant], start_date: rate_period[:dateDebut], end_date: rate_period[:dateFin],
|
352
|
-
values: rate_period[:tarifs].blank? ? [] : rate_period[:tarifs].map {|t| {min: t[:minimum], max: t[:maximum], type: t[:type][:id], details: node_value(t, :precisionTarif)}}
|
353
|
+
values: rate_period[:tarifs].blank? ? [] : rate_period[:tarifs].map {|t| {min: t[:minimum], max: t[:maximum], type: t[:type][:id], details: node_value(t, :precisionTarif, *locales)}}
|
353
354
|
}
|
354
355
|
end
|
355
356
|
|
data/app/models/apidae/export.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
|
1
4
|
module Apidae
|
2
5
|
class Export < ActiveRecord::Base
|
3
6
|
|
@@ -20,5 +23,43 @@ module Apidae
|
|
20
23
|
def self.pending
|
21
24
|
where(remote_status: 'SUCCESS', status: PENDING).order(:id)
|
22
25
|
end
|
26
|
+
|
27
|
+
def import_data
|
28
|
+
success = true
|
29
|
+
begin
|
30
|
+
open(file_url) do |f|
|
31
|
+
begin
|
32
|
+
FileImport.import(f, project_id)
|
33
|
+
unless confirm_url.blank?
|
34
|
+
uri = URI(confirm_url)
|
35
|
+
req = Net::HTTP::Post.new(uri)
|
36
|
+
Net::HTTP.start(uri.hostname, uri.port) do |http|
|
37
|
+
http.request(req)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
update(status: Export::COMPLETE)
|
41
|
+
if Rails.application.config.respond_to?(:apidae_import_callback)
|
42
|
+
Rails.application.config.apidae_import_callback.call(self)
|
43
|
+
end
|
44
|
+
rescue Exception => ex
|
45
|
+
logger.error("Failed to import export file : #{file_url}")
|
46
|
+
logger.error("Error is : #{ex} \n#{ex.backtrace.join("\n") unless ex.backtrace.blank?}")
|
47
|
+
success = false
|
48
|
+
update(status: Export::CANCELLED)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
rescue OpenURI::HTTPError => err
|
52
|
+
logger.error("Failed to download export file : #{file_url}")
|
53
|
+
logger.error("Error is : #{err}")
|
54
|
+
success = false
|
55
|
+
update(status: Export::CANCELLED)
|
56
|
+
rescue Exception => e
|
57
|
+
logger.error "Failed to import file : #{e.file_url}"
|
58
|
+
logger.error("Error is : #{err}")
|
59
|
+
success = false
|
60
|
+
e.update(status: Export::CANCELLED)
|
61
|
+
end
|
62
|
+
success
|
63
|
+
end
|
23
64
|
end
|
24
65
|
end
|
data/app/models/apidae/obj.rb
CHANGED
@@ -20,7 +20,7 @@ module Apidae
|
|
20
20
|
:mobile_website, :shorty_url, :contacts
|
21
21
|
store_accessor :location_data, :address, :place, :latitude, :longitude, :access, :territories, :environments
|
22
22
|
store_accessor :openings_data, :openings_desc, :openings_desc_mode, :openings, :time_periods, :openings_extra
|
23
|
-
store_accessor :rates_data, :rates_desc, :rates_desc_mode, :rates, :payment_methods, :includes, :excludes
|
23
|
+
store_accessor :rates_data, :rates_desc, :rates_desc_mode, :rates, :payment_methods, :includes, :excludes, :rates_extra
|
24
24
|
store_accessor :service_data, :services, :equipments, :comfort, :activities, :challenged, :languages
|
25
25
|
store_accessor :booking_data, :booking_desc, :booking_entities
|
26
26
|
store_accessor :tags_data, :promo, :internal, :linked
|
@@ -50,6 +50,10 @@ module Apidae
|
|
50
50
|
objects.where(where_clause).count
|
51
51
|
end
|
52
52
|
|
53
|
+
def valid_api?
|
54
|
+
apidae_project && !apidae_project.api_key.blank? && !apidae_project.apidae_id.blank?
|
55
|
+
end
|
56
|
+
|
53
57
|
def api_results(opts = {})
|
54
58
|
key = cache_key(:results)
|
55
59
|
res = $apidae_cache.read(key)
|
@@ -83,14 +87,14 @@ module Apidae
|
|
83
87
|
res
|
84
88
|
end
|
85
89
|
|
86
|
-
def api_object(apidae_obj_id)
|
87
|
-
query_args = build_args(OBJECTS_ENDPOINT, {obj_ids: [apidae_obj_id], fields:
|
90
|
+
def api_object(apidae_obj_id, fields = ["@all"])
|
91
|
+
query_args = build_args(OBJECTS_ENDPOINT, {obj_ids: [apidae_obj_id], fields: fields})
|
88
92
|
query_api(query_args, true, false)
|
89
93
|
end
|
90
94
|
|
91
|
-
def add_or_refresh_obj(apidae_obj_id)
|
92
|
-
if
|
93
|
-
res = api_object(apidae_obj_id)
|
95
|
+
def add_or_refresh_obj(apidae_obj_id, fields = ["@all"])
|
96
|
+
if valid_api?
|
97
|
+
res = api_object(apidae_obj_id, fields)
|
94
98
|
if res[:results] && res[:results].length == 1
|
95
99
|
obj_data = res[:results].first.deep_symbolize_keys
|
96
100
|
add_or_refresh(obj_data)
|
@@ -99,7 +103,7 @@ module Apidae
|
|
99
103
|
end
|
100
104
|
|
101
105
|
def add_or_refresh_objs(fields = ["@all"])
|
102
|
-
if
|
106
|
+
if valid_api?
|
103
107
|
res = api_objects({fields: fields})
|
104
108
|
if res[:results] && res[:results].length > 0
|
105
109
|
res[:results].each do |result|
|
@@ -116,8 +120,9 @@ module Apidae
|
|
116
120
|
obj = Obj.update_object(obj, obj_data, apidae_project.locales, apidae_project.versions)
|
117
121
|
else
|
118
122
|
obj = Obj.add_object(obj_data, apidae_project.locales, apidae_project.versions)
|
119
|
-
SelectionObject.create(apidae_selection_id: id, apidae_object_id: added_obj.id)
|
120
123
|
end
|
124
|
+
SelectionObject.create(apidae_selection_id: id, apidae_object_id: obj.id) unless objects.include?(obj)
|
125
|
+
|
121
126
|
if Rails.application.config.respond_to?(:apidae_obj_refresh_callback)
|
122
127
|
Rails.application.config.apidae_obj_refresh_callback.call(obj.apidae_id)
|
123
128
|
end
|
data/lib/apidae/version.rb
CHANGED
@@ -63,3 +63,5 @@ Migrating to AddLocalesDataToApidaeProjects (20190123214635)
|
|
63
63
|
[1m[35m (0.2ms)[0m [1m[35mCOMMIT[0m
|
64
64
|
[1m[35m (0.2ms)[0m [1m[34mSELECT pg_advisory_unlock(6140174353533887940)[0m
|
65
65
|
[1m[35m (0.2ms)[0m [1m[34mSELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC[0m
|
66
|
+
[1m[36mApidae::Obj Load (26.8ms)[0m [1m[34mSELECT apidae_id, COUNT(id) FROM "apidae_objs" WHERE "apidae_objs"."root_obj_id" IS NULL GROUP BY "apidae_objs"."apidae_id" HAVING (COUNT(id) > 1)[0m
|
67
|
+
[1m[36mApidae::Obj Load (2.4ms)[0m [1m[34mSELECT apidae_id, COUNT(id) FROM "apidae_objs" WHERE "apidae_objs"."root_obj_id" IS NULL GROUP BY "apidae_objs"."apidae_id" HAVING (COUNT(id) > 1)[0m
|
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: 1.
|
4
|
+
version: 1.3.0
|
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: 2020-
|
11
|
+
date: 2020-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: pg
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -31,6 +31,9 @@ dependencies:
|
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.1'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '2.0'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -38,20 +41,23 @@ dependencies:
|
|
38
41
|
- - "~>"
|
39
42
|
- !ruby/object:Gem::Version
|
40
43
|
version: '1.1'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.0'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: rubyzip
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
51
|
- - "~>"
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
53
|
+
version: '2.0'
|
48
54
|
type: :runtime
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
51
57
|
requirements:
|
52
58
|
- - "~>"
|
53
59
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
60
|
+
version: '2.0'
|
55
61
|
- !ruby/object:Gem::Dependency
|
56
62
|
name: jbuilder
|
57
63
|
requirement: !ruby/object:Gem::Requirement
|