diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/stif/permission_translator.rb | 2 | ||||
| -rw-r--r-- | lib/tasks/ci.rake | 37 | ||||
| -rw-r--r-- | lib/tom_tom.rb | 28 | ||||
| -rw-r--r-- | lib/tom_tom/errors.rb | 4 | ||||
| -rw-r--r-- | lib/tom_tom/errors/matrix_remote_error.rb | 5 | ||||
| -rw-r--r-- | lib/tom_tom/matrix.rb | 21 |
6 files changed, 76 insertions, 21 deletions
diff --git a/lib/stif/permission_translator.rb b/lib/stif/permission_translator.rb index 09a7c610c..aefe50fe1 100644 --- a/lib/stif/permission_translator.rb +++ b/lib/stif/permission_translator.rb @@ -48,7 +48,7 @@ module Stif def translation_table { "boiv:read-offer" => %w{sessions.create}, - "boiv:edit-offer" => all_destructive_permissions + %w{sessions.create}, + "boiv:edit-offer" => all_destructive_permissions + %w{sessions.create workbenches.update}, } end diff --git a/lib/tasks/ci.rake b/lib/tasks/ci.rake index cb9ed77e7..0723e4ccb 100644 --- a/lib/tasks/ci.rake +++ b/lib/tasks/ci.rake @@ -8,12 +8,21 @@ namespace :ci do end end + def parallel_tests? + ENV["PARALLEL_TESTS"] == "true" + end + desc "Prepare CI build" task :setup do cp "config/database.yml", "config/database.yml.orig" cp "config/database/ci.yml", "config/database.yml" puts "Use #{database_name} database" - sh "RAILS_ENV=test rake db:drop db:create db:migrate" + + if parallel_tests? + sh "RAILS_ENV=test rake parallel:drop parallel:create parallel:migrate" + else + sh "RAILS_ENV=test rake db:drop db:create db:migrate" + end end def git_branch @@ -43,11 +52,7 @@ namespace :ci do end task :assets do - sh "RAILS_ENV=test bundle exec rake assets:precompile" - end - - task :i18n_js_export do - sh "RAILS_ENV=test bundle exec rake i18n:js:export" + sh "RAILS_ENV=test bundle exec rake assets:precompile i18n:js:export" end task :jest do @@ -71,12 +76,28 @@ namespace :ci do sh "RAILS_ENV=test bundle exec rake assets:clobber" end - task :build => ["ci:setup", "ci:assets", "ci:i18n_js_export", "spec", "ci:jest", "cucumber", "ci:check_security"] + task :spec do + if parallel_tests? + # parallel tasks invokes this task .. + # but development db isn't available during ci tasks + Rake::Task["db:abort_if_pending_migrations"].clear + + Rake::Task["parallel:spec"].invoke + else + Rake::Task["spec"].invoke + end + end + + task :build => ["ci:setup", "ci:assets", "ci:spec", "ci:jest", "cucumber", "ci:check_security"] namespace :docker do task :clean do puts "Drop #{database_name} database" - sh "RAILS_ENV=test rake db:drop" + if parallel_tests? + sh "RAILS_ENV=test rake parallel:drop" + else + sh "RAILS_ENV=test rake db:drop" + end # Restore projet config/database.yml # cp "config/database.yml.orig", "config/database.yml" if File.exists?("config/database.yml.orig") diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb index fcebcc7ac..91f1a3800 100644 --- a/lib/tom_tom.rb +++ b/lib/tom_tom.rb @@ -1,26 +1,30 @@ module TomTom BASE_URL = 'https://api.tomtom.com' - @api_key = Rails.application.secrets.tomtom_api_key - @connection = Faraday.new( - url: BASE_URL, - params: { - key: @api_key - } - ) do |faraday| - faraday.use FaradayMiddleware::FollowRedirects, limit: 1 - faraday.adapter Faraday.default_adapter + @@api_key = Rails.application.secrets.tomtom_api_key + cattr_accessor :api_key + + def self.connection + @connection ||= Faraday.new( + url: BASE_URL, + params: { + key: api_key + } + ) do |faraday| + faraday.use FaradayMiddleware::FollowRedirects, limit: 1 + faraday.adapter Faraday.default_adapter + end end def self.enabled? - @api_key.present? + api_key.present? && /[a-zA-Z0-9]{32}/ === api_key end def self.batch(way_costs) - TomTom::Batch.new(@connection).batch(way_costs) + TomTom::Batch.new(connection).batch(way_costs) end def self.matrix(way_costs) - TomTom::Matrix.new(@connection).matrix(way_costs) + TomTom::Matrix.new(connection).matrix(way_costs) end end diff --git a/lib/tom_tom/errors.rb b/lib/tom_tom/errors.rb new file mode 100644 index 000000000..da3f2239c --- /dev/null +++ b/lib/tom_tom/errors.rb @@ -0,0 +1,4 @@ +module TomTom + module Errors + end +end diff --git a/lib/tom_tom/errors/matrix_remote_error.rb b/lib/tom_tom/errors/matrix_remote_error.rb new file mode 100644 index 000000000..b13767847 --- /dev/null +++ b/lib/tom_tom/errors/matrix_remote_error.rb @@ -0,0 +1,5 @@ +module TomTom + module Errors + class MatrixRemoteError < RuntimeError; end + end +end diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb index b0c8cc335..e779abbf7 100644 --- a/lib/tom_tom/matrix.rb +++ b/lib/tom_tom/matrix.rb @@ -4,6 +4,10 @@ module TomTom @connection = connection end + # Exceptions: + # + # * This raises a `TomTom::Errors::MatrixRemoteError` when the API responds + # with an error. def matrix(way_costs) points_with_ids = points_from_way_costs(way_costs) points = points_as_params(points_with_ids) @@ -15,11 +19,14 @@ module TomTom req.headers['Content-Type'] = 'application/json' req.params[:routeType] = 'shortest' + req.params[:traffic] = 'false' req.params[:travelMode] = 'bus' req.body = build_request_body(points) end + check_for_error_response(response) + extract_costs_to_way_costs!( way_costs, points_with_ids, @@ -76,6 +83,20 @@ module TomTom }) end + def check_for_error_response(response) + if response.status != 200 + raise TomTom::Errors::MatrixRemoteError, + "status: #{response.status}, body: #{response.body}" + end + + json = JSON.parse(response.body) + + if json.has_key?('error') + raise TomTom::Errors::MatrixRemoteError, + "status: #{response.status}, message: #{json['error']['description']}" + end + end + def extract_costs_to_way_costs!(way_costs, points, matrix_json) way_costs = [] |
