From 3e8de2239ef0bf034b1a62d6a526238bc93f3c0b Mon Sep 17 00:00:00 2001 From: Brice TEXIER Date: Sun, 12 Mar 2017 16:35:10 +0100 Subject: [PATCH 1/4] Updates gem dependencies --- Gemfile.lock | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 130d6a8816..1cae108523 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -74,7 +74,7 @@ GEM sidekiq (>= 2.11) arel (6.0.4) ast (2.3.0) - autoprefixer-rails (6.7.4) + autoprefixer-rails (6.7.7) execjs awesome_nested_set (3.0.3) activerecord (>= 4.0.0, < 5) @@ -97,7 +97,7 @@ GEM bootstrap-sass (3.3.7) autoprefixer-rails (>= 5.2.1) sass (>= 3.3.4) - bootstrap3-datetimepicker-rails (4.17.43) + bootstrap3-datetimepicker-rails (4.17.47) momentjs-rails (>= 2.8.1) browser (2.3.0) builder (3.2.3) @@ -114,7 +114,7 @@ GEM capybara (>= 2.3.0, < 2.13.0) json charlock_holmes (0.7.3) - childprocess (0.6.1) + childprocess (0.6.2) ffi (~> 1.0, >= 1.0.11) climate_control (0.1.0) cocaine (0.5.8) @@ -134,7 +134,7 @@ GEM execjs coffee-script-source (1.12.2) colored (1.2) - concurrent-ruby (1.0.4) + concurrent-ruby (1.0.5) connection_pool (2.2.1) coveralls (0.8.19) json (>= 1.8, < 3) @@ -159,29 +159,29 @@ GEM actionmailer (>= 4.1.0) devise (>= 4.0.0) docile (1.1.5) - domain_name (0.5.20161129) + domain_name (0.5.20170223) unf (>= 0.0.5, < 1.0.0) enumerize (2.0.1) activesupport (>= 3.2) erubis (2.7.0) - eventmachine (1.2.2) + eventmachine (1.2.3) exception_notification (4.2.1) actionmailer (>= 4.0, < 6) activesupport (>= 4.0, < 6) execjs (2.7.0) factory_girl (4.8.0) activesupport (>= 3.0.0) - faraday (0.10.1) + faraday (0.11.0) multipart-post (>= 1.2, < 3) faraday_middleware (0.11.0.1) faraday (>= 0.7.4, < 1.0) - feedjira (2.1.0) + feedjira (2.1.2) faraday (>= 0.9) faraday_middleware (>= 0.9) loofah (>= 2.0) sax-machine (>= 1.0) ffaker (2.5.0) - ffi (1.9.17) + ffi (1.9.18) figaro (1.1.1) thor (~> 0.14) foreman (0.83.0) @@ -196,7 +196,7 @@ GEM builder (>= 2.1.2) haml (4.0.7) tilt - hashie (3.5.3) + hashie (3.5.5) hpricot (0.8.6) http-cookie (1.0.3) domain_name (~> 0.5) @@ -205,14 +205,14 @@ GEM rack socksify humanize (1.3.0) - i18n (0.8.0) + i18n (0.8.1) i18n-complements (1.1.0) i18n (>= 0.6) i18n-js (3.0.0.rc15) i18n (~> 0.6, >= 0.6.6) iban-tools (1.1.0) - jbuilder (2.6.1) - activesupport (>= 3.0.0, < 5.1) + jbuilder (2.6.3) + activesupport (>= 3.0.0, < 5.2) multi_json (~> 1.2) jc-validates_timeliness (3.1.1) timeliness (~> 0.3.7) @@ -246,7 +246,7 @@ GEM addressable (~> 2.3) letter_opener (1.4.1) launchy (~> 2.2) - libv8 (3.16.14.17) + libv8 (3.16.14.19) loofah (2.0.3) nokogiri (>= 1.5.9) luhn (1.0.2) @@ -285,8 +285,8 @@ GEM mini_portile2 (~> 2.1.0) nori (2.6.0) ntlm-http (0.1.1) - oauth2 (1.3.0) - faraday (>= 0.8, < 0.11) + oauth2 (1.3.1) + faraday (>= 0.8, < 0.12) jwt (~> 1.0) multi_json (~> 1.3) multi_xml (~> 0.5) @@ -309,7 +309,7 @@ GEM paperclip (>= 3.1, < 5.2) parser (2.4.0.0) ast (~> 2.2) - pg (0.19.0) + pg (0.20.0) polyglot (0.3.5) possibly (1.0.1) powerpack (0.1.1) @@ -352,7 +352,7 @@ GEM rails-deprecated_sanitizer (>= 1.0.1) rails-html-sanitizer (1.0.3) loofah (~> 2.0) - rails_best_practices (1.17.0) + rails_best_practices (1.18.0) activesupport code_analyzer (>= 0.4.3) erubis @@ -381,7 +381,7 @@ GEM http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 4.0) netrc (~> 0.8) - rgeo (0.5.3) + rgeo (0.6.0) rgeo-activerecord (4.0.5) activerecord (~> 4.2) rgeo (~> 0.3) @@ -431,7 +431,7 @@ GEM sdoc (0.4.2) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) - selenium-webdriver (3.1.0) + selenium-webdriver (3.3.0) childprocess (~> 0.5) rubyzip (~> 1.0) websocket (~> 1.0) @@ -453,7 +453,7 @@ GEM sidekiq-unique-jobs (4.0.18) sidekiq (>= 2.6) thor - simple_calendar (2.2.3) + simple_calendar (2.2.5) rails (>= 3.0) simple_form (3.2.1) actionpack (> 4, < 5.1) @@ -464,7 +464,7 @@ GEM simplecov-html (~> 0.10.0) simplecov-html (0.10.0) slop (3.6.0) - socksify (1.7.0) + socksify (1.7.1) spring (2.0.1) activesupport (>= 4.2) sprockets (3.7.1) @@ -489,7 +489,7 @@ GEM eventmachine (~> 1.0, >= 1.0.4) rack (>= 1, < 3) thor (0.19.4) - thread_safe (0.3.5) + thread_safe (0.3.6) tilt (2.0.6) timecop (0.8.1) timeliness (0.3.8) @@ -504,7 +504,7 @@ GEM railties (>= 3.1) tzinfo (1.2.2) thread_safe (~> 0.1) - uglifier (3.0.4) + uglifier (3.1.7) execjs (>= 0.3.0, < 3) unf (0.1.4) unf_ext From f2134cc1347332d1f920625707e01cb074fcf371 Mon Sep 17 00:00:00 2001 From: Brice TEXIER Date: Sun, 12 Mar 2017 19:58:21 +0100 Subject: [PATCH 2/4] Begins to fix OAuth integration process --- .../backend/integrations_controller.rb | 20 +- app/exchangers/ekylibre/sales_exchanger.rb | 8 +- app/models/call_message.rb | 6 +- app/models/call_request.rb | 2 +- app/models/call_response.rb | 2 +- app/models/integration.rb | 30 ++- .../tekyla/stylesheets/integrations.scss | 1 + .../backend/integrations/_form.html.haml | 2 +- config/locales/eng/action.yml | 1 + config/navigation.xml | 1 + config/rights.yml | 1 + config/routes.rb | 3 +- ...183557_change_call_message_headers_type.rb | 9 + db/structure.sql | 4 +- db/tables.yml | 2 +- lib/action_integration/base.rb | 191 ++++++++++-------- lib/action_integration/parameters.rb | 2 + lib/action_integration/protocols/json.rb | 20 +- lib/action_integration/protocols/rest_base.rb | 30 +-- test/fixtures/call_messages.yml | 2 +- test/models/call_message_test.rb | 2 +- test/models/call_request_test.rb | 2 +- test/models/call_response_test.rb | 2 +- 23 files changed, 206 insertions(+), 137 deletions(-) create mode 100644 db/migrate/20170312183557_change_call_message_headers_type.rb diff --git a/app/controllers/backend/integrations_controller.rb b/app/controllers/backend/integrations_controller.rb index 17dfdc2077..6fb514f355 100644 --- a/app/controllers/backend/integrations_controller.rb +++ b/app/controllers/backend/integrations_controller.rb @@ -21,13 +21,13 @@ def new return end @integration = Integration.new(nature: params[:nature], parameters: (params[:parameters] || {})) - t3e(@integration.attributes.merge(name: @integration.nature.camelize)) + t3e(@integration.attributes.merge(name: @integration.name)) render(locals: { cancel_url: :back }) end def edit return unless @integration = find_and_check(:integration) - t3e(@integration.attributes.merge(name: @integration.nature.camelize)) + t3e(@integration.attributes.merge(name: @integration.name)) render(locals: { cancel_url: :back }) end @@ -38,17 +38,29 @@ def destroy def create @integration = resource_model.new(permitted_params) - t3e(@integration.attributes.merge(name: @integration.nature.camelize)) + t3e(@integration.attributes.merge(name: @integration.name)) return if save_and_redirect(@integration, url: :backend_integrations) render(locals: { cancel_url: :backend_integrations }) end def update return unless @integration = find_and_check(:integration) - t3e(@integration.attributes.merge(name: @integration.nature.camelize)) + t3e(@integration.attributes.merge(name: @integration.name)) @integration.attributes = permitted_params return if save_and_redirect(@integration, url: :backend_integrations) render(locals: { cancel_url: :back }) end + + def check + unless params[:nature] + head :unprocessable_entity + return + end + @integration = Integration.find_or_initialize_by(nature: params[:nature]) + @integration.parameters[:access_token] = params[:access_token] + @integration.parameters[:token_type] = params[:token_type] if params[:token_type] + @integration.save! + redirect_to action: :index + end end end diff --git a/app/exchangers/ekylibre/sales_exchanger.rb b/app/exchangers/ekylibre/sales_exchanger.rb index 7c6098ec37..e34b333dcf 100644 --- a/app/exchangers/ekylibre/sales_exchanger.rb +++ b/app/exchangers/ekylibre/sales_exchanger.rb @@ -66,10 +66,10 @@ def import # find or create a purchase line if sale && variant && r.unit_pretax_amount && r.quantity && sale_item_tax unless sale_item = SaleItem.where( - sale_id: sale.id, - pretax_amount: r.pretax_amount, - variant_id: variant.id - ).first + sale_id: sale.id, + pretax_amount: r.pretax_amount, + variant_id: variant.id + ).first sale.items.create!( quantity: r.quantity, tax: sale_item_tax, diff --git a/app/models/call_message.rb b/app/models/call_message.rb index 0f821f500a..1b7c39a283 100644 --- a/app/models/call_message.rb +++ b/app/models/call_message.rb @@ -27,7 +27,7 @@ # created_at :datetime not null # creator_id :integer # format :string -# headers :string +# headers :text # id :integer not null, primary key # ip_address :string # lock_version :integer default(0), not null @@ -48,8 +48,8 @@ class CallMessage < Ekylibre::Record::Base enumerize :nature, in: [:incoming, :outgoing], predicates: true # [VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`. - validates :body, length: { maximum: 500_000 }, allow_blank: true - validates :format, :headers, :ip_address, :ssl, :status, :url, :verb, length: { maximum: 500 }, allow_blank: true + validates :body, :headers, length: { maximum: 500_000 }, allow_blank: true + validates :format, :ip_address, :ssl, :status, :url, :verb, length: { maximum: 500 }, allow_blank: true validates :nature, presence: true # ]VALIDATORS] end diff --git a/app/models/call_request.rb b/app/models/call_request.rb index ddf56cf14e..f6ff75b56f 100644 --- a/app/models/call_request.rb +++ b/app/models/call_request.rb @@ -27,7 +27,7 @@ # created_at :datetime not null # creator_id :integer # format :string -# headers :string +# headers :text # id :integer not null, primary key # ip_address :string # lock_version :integer default(0), not null diff --git a/app/models/call_response.rb b/app/models/call_response.rb index 489522b150..d857c37454 100644 --- a/app/models/call_response.rb +++ b/app/models/call_response.rb @@ -27,7 +27,7 @@ # created_at :datetime not null # creator_id :integer # format :string -# headers :string +# headers :text # id :integer not null, primary key # ip_address :string # lock_version :integer default(0), not null diff --git a/app/models/integration.rb b/app/models/integration.rb index 7e946dab74..184de44340 100644 --- a/app/models/integration.rb +++ b/app/models/integration.rb @@ -32,23 +32,35 @@ # updated_at :datetime not null # updater_id :integer # + +# Integration model is here to save connection parameters in (encrypted) store +# to keep them reusable when necessary. class Integration < Ekylibre::Record::Base # [VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`. validates :nature, presence: true, uniqueness: true, length: { maximum: 500 } # ]VALIDATORS] - delegate :auth_type, :check_connection, :integration_name, to: :integration_type + delegate :authentication_mode, :check_connection, :integration_name, to: :integration_type composed_of :parameters, class_name: 'ActionIntegration::Parameters', mapping: [%w(ciphered_parameters ciphered), %w(initialization_vectors ivs)], converter: proc { |parameters| ActionIntegration::Parameters.cipher(parameters) } validate do - check_connection attributes do |c| - c.redirect do - errors.add(:parameters, :check_redirected) - end - c.error do - errors.add(:parameters, :check_errored) + if integration_type + if authentication_mode == :check + check_connection attributes do |c| + c.redirect do + errors.add(:parameters, :check_redirected) + end + c.error do + errors.add(:parameters, :check_errored) + end + end + elsif authentication_mode == :check + list = parameters.keys.map(&:to_s) + unless parameters && !integration_type.parameters.detect { |p| list.include?(p.to_s) } + errors.add(:parameters, :check_errored) + end end end end @@ -73,4 +85,8 @@ def integration_type def parameter_keys integration_type.parameters end + + def name + nature.to_s.camelize + end end diff --git a/app/themes/tekyla/stylesheets/integrations.scss b/app/themes/tekyla/stylesheets/integrations.scss index 1c2e74aaa3..d27cc9add1 100644 --- a/app/themes/tekyla/stylesheets/integrations.scss +++ b/app/themes/tekyla/stylesheets/integrations.scss @@ -22,6 +22,7 @@ position: absolute; right: 1em; top: 1em; + padding: 0.3em 0.4em; } .integration-card { diff --git a/app/views/backend/integrations/_form.html.haml b/app/views/backend/integrations/_form.html.haml index e3e346507e..29c7f24eeb 100644 --- a/app/views/backend/integrations/_form.html.haml +++ b/app/views/backend/integrations/_form.html.haml @@ -11,4 +11,4 @@ %p = :website.tl = link_to(url_name, url) - = render resource.auth_type.to_s, f: f + = render resource.authentication_mode.to_s, f: f diff --git a/config/locales/eng/action.yml b/config/locales/eng/action.yml index 821f3e575a..5e7895fe1e 100644 --- a/config/locales/eng/action.yml +++ b/config/locales/eng/action.yml @@ -353,6 +353,7 @@ eng: new: "New product grading" show: "Product grading\_: %{number}" backend/integrations: + # check: "Check integration: %{name}" edit: "Edit integration: %{name}" index: "Integrations" new: "Activate integration: %{name}" diff --git a/config/navigation.xml b/config/navigation.xml index 28fe2cfd27..03e491068b 100644 --- a/config/navigation.xml +++ b/config/navigation.xml @@ -1107,6 +1107,7 @@ + diff --git a/config/rights.yml b/config/rights.yml index 0dea68e219..59c1c0070f 100644 --- a/config/rights.yml +++ b/config/rights.yml @@ -1120,6 +1120,7 @@ integrations: - "backend/identifiers#edit" - "backend/identifiers#new" - "backend/identifiers#update" + - "backend/integrations#check" - "backend/integrations#create" - "backend/integrations#destroy" - "backend/integrations#edit" diff --git a/config/routes.rb b/config/routes.rb index 8bd6cc40b3..2e1c3c7e07 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -550,8 +550,9 @@ resources :integrations, except: [:show, :destroy] do collection do + get ':nature/check', action: :check get ':nature', action: :new - delete :destroy + delete ':nature', action: :destroy end end diff --git a/db/migrate/20170312183557_change_call_message_headers_type.rb b/db/migrate/20170312183557_change_call_message_headers_type.rb new file mode 100644 index 0000000000..ad62146f89 --- /dev/null +++ b/db/migrate/20170312183557_change_call_message_headers_type.rb @@ -0,0 +1,9 @@ +class ChangeCallMessageHeadersType < ActiveRecord::Migration + def up + change_column :call_messages, :headers, :text + end + + def down + change_column :call_messages, :headers, :string + end +end diff --git a/db/structure.sql b/db/structure.sql index f552e61afa..39e1e7eea1 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1087,7 +1087,7 @@ ALTER SEQUENCE bank_statements_id_seq OWNED BY bank_statements.id; CREATE TABLE call_messages ( id integer NOT NULL, status character varying, - headers character varying, + headers text, body text, type character varying, nature character varying NOT NULL, @@ -16942,3 +16942,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170307103213'); INSERT INTO schema_migrations (version) VALUES ('20170307171442'); +INSERT INTO schema_migrations (version) VALUES ('20170312183557'); + diff --git a/db/tables.yml b/db/tables.yml index e339b5e77b..ebd17202d1 100644 --- a/db/tables.yml +++ b/db/tables.yml @@ -988,7 +988,7 @@ call_messages: format: type: string headers: - type: string + type: text id: type: integer required: true diff --git a/lib/action_integration/base.rb b/lib/action_integration/base.rb index efd2ac9376..6673982bf8 100644 --- a/lib/action_integration/base.rb +++ b/lib/action_integration/base.rb @@ -15,118 +15,122 @@ def initialize(call) @call = call end - def self.calls(*called_methods) - # Each method in the "calls" parameters corresponds to a Call to an API - # therefore for each of them we define a class method that will initialize - # a Call object which will actually call the method. - called_methods.each do |method| - singleton_class.instance_exec(method) do - define_method(method) do |*args| - ::Call.new( - integration_name: self, - name: method, - arguments: args - ) + class << self + def calls(*called_methods) + # Each method in the "calls" parameters corresponds to a Call to an API + # therefore for each of them we define a class method that will initialize + # a Call object which will actually call the method. + called_methods.each do |method| + singleton_class.instance_exec(method) do + define_method(method) do |*args| + ::Call.new( + integration_name: self, + name: method, + arguments: args + ) + end end end end - end - def self.on_logout(trigger: false, &block) - Ekylibre::Hook.subscribe("#{integration_name.underscore}_logout", block) if block_given? - Ekylibre::Hook.publish("#{integration_name.underscore}_logout") if trigger - end + def on_logout(trigger: false, &block) + Ekylibre::Hook.subscribe("#{integration_name.underscore}_logout", block) if block_given? + Ekylibre::Hook.publish("#{integration_name.underscore}_logout") if trigger + end - def self.run(method_name = nil, every: nil, &block) - raise ArgumentError, 'Please use either a method or a block at a time.' if block_given? && method_name.present? - raise ArgumentError, 'Please specify either a method or a block.' unless method_name.present? || block_given? - raise ArgumentError, 'Please specify a frequency.' unless every.present? - raise ArgumentError, 'Invalid frequency.' unless [:day, :hour].include? every - proc = lambda do - begin - to_execute = block || send(method_name).method(:execute) - to_execute.call - rescue ServiceNotIntegrated - Rails.logger.info "Integration not present on tenant #{Ekylibre::Tenant.current}".yellow + def run(method_name = nil, every: nil, &block) + if block_given? && method_name.present? + raise ArgumentError, 'Please use either a method or a block at a time.' end + unless method_name.present? || block_given? + raise ArgumentError, 'Please specify either a method or a block.' + end + raise ArgumentError, 'Please specify a frequency.' unless every.present? + raise ArgumentError, 'Invalid frequency.' unless [:day, :hour].include? every + proc = lambda do + begin + to_execute = block || send(method_name).method(:execute) + to_execute.call + rescue ServiceNotIntegrated + Rails.logger.info "Integration not present on tenant #{Ekylibre::Tenant.current}".yellow + end + end + Ekylibre::Hook.subscribe("every_#{every}", proc) end - Ekylibre::Hook.subscribe("every_#{every}", proc) - end - # Check ########## + # Check ########## - def self.check_connection(account = nil, &block) - calls :check - check(account).execute(&block) - end + def check_connection(account = nil, &block) + calls :check + check(account).execute(&block) + end - def self.on_check_success(&block) - Ekylibre::Hook.subscribe("#{integration_name.underscore}_check_successful", &block) - end + def on_check_success(&block) + Ekylibre::Hook.subscribe("#{integration_name.underscore}_check_successful", &block) + end - def self.on_check_error(&block) - Ekylibre::Hook.subscribe("#{integration_name.underscore}_check_errored", &block) - end + def on_check_error(&block) + Ekylibre::Hook.subscribe("#{integration_name.underscore}_check_errored", &block) + end - def self.check - raise NotImplementedError - end + def check + raise NotImplementedError + end - ################## + ################## - # Small helper methods # + # Small helper methods # - # find_integration("bonjour_hello") => BonjourHelloIntegration / nil - def self.find_integration(type) - descendants.select { |caller| caller.name.demodulize == type.to_s.camelize + 'Integration' }.first - end + # find_integration("bonjour_hello") => BonjourHelloIntegration / nil + def find_integration(type) + descendants.select { |caller| caller.name.demodulize == type.to_s.camelize + 'Integration' }.first + end - # BonjourHelloIntegration => BonjourHello - def self.integration_name - name.underscore - .split('/') - .last - .split('_')[0...-1] - .join('_') - .camelize - end + # Toto::BonjourHelloIntegration => BonjourHello + def integration_name + name.underscore + .split('/') + .last + .split('_')[0...-1] + .join('_') + .camelize + end - ################# + # Toto::BonjourHelloIntegration => toto/bonjour_hello + def integration_path + name.underscore.gsub(/\_integration\z/, '') + end - def self.auth_type - @auth_type || :check - end + # Toto::BonjourHelloIntegration => toto/bonjour_hello + def integration_path + name.underscore.gsub(/\_integration\z/, '') + end - def self.auth(type) - @auth_type = type - yield if block_given? - end + ################# - def self.parameters - @parameters || [] - end + def authentication_mode + @authentication_mode || :check + end - def self.parameter(name) - @parameters ||= [] - @parameters << name - end + def authenticate_with(mode) + @authentication_mode = mode + yield if block_given? + end - # Finds the corresponding Integration record - # or instantiate an integration from the params - def fetch(integration_params = nil) - # Needed for #new Integrations - integration = integration_params && ::Integration.new(integration_params) - integration ||= ::Integration.find_by(nature: self.class.integration_name.underscore) + def auth(type, &block) + ActiveSupport::Deprecation.warn 'ActionIntegration::Base.auth is deprecated. Please use ActionIntegration::Base.authenticate_with instead.' + authenticate_with(type, &block) + end - raise ServiceNotIntegrated unless integration - self.class.parameters.each do |p| - raise IntegrationParameterEmpty, p if integration.parameters[p.to_s].blank? + def parameters + @parameters || [] end - integration - end + def parameter(name) + @parameters ||= [] + @parameters << name + end - class << self # TODO: fetch shouldn't raise exceptions, fetch! does def fetch(local_name = nil) integration ||= ::Integration.find_by(nature: (local_name || integration_name).underscore) @@ -141,5 +145,20 @@ def fetch(local_name = nil) alias fetch! fetch end + + # Finds the corresponding Integration record + # or instantiate an integration from the params + def fetch(integration_params = nil) + # Needed for #new Integrations + integration = integration_params && ::Integration.new(integration_params) + integration ||= ::Integration.find_by(nature: self.class.integration_name.underscore) + + raise ServiceNotIntegrated unless integration + self.class.parameters.each do |p| + raise IntegrationParameterEmpty, p if integration.parameters[p.to_s].blank? + end + + integration + end end end diff --git a/lib/action_integration/parameters.rb b/lib/action_integration/parameters.rb index 31338a11cf..45fa8f33a8 100644 --- a/lib/action_integration/parameters.rb +++ b/lib/action_integration/parameters.rb @@ -2,6 +2,8 @@ module ActionIntegration class Parameters attr_accessor :ivs, :ciphered + delegate :keys, :values, to: :values + def initialize(ciphered, ivs) @ciphered = ciphered || {} @ivs = ivs || {} diff --git a/lib/action_integration/protocols/json.rb b/lib/action_integration/protocols/json.rb index 5e5817b784..f9e29b0558 100644 --- a/lib/action_integration/protocols/json.rb +++ b/lib/action_integration/protocols/json.rb @@ -4,24 +4,24 @@ module Protocols module JSON include Protocols::RestBase - def get(path, &block) - get_base(path, &block) + def get(path, headers = {}, &block) + get_base(path, { 'content-type' => 'application/json' }.merge(headers), &block) end - def post(path, data, &block) - post_base(path, data, { 'content-type' => 'application/json' }, &block) + def post(path, data, headers = {}, &block) + post_base(path, data, { 'content-type' => 'application/json' }.merge(headers), &block) end - def put(path, data, &block) - put_base(path, data, { 'content-type' => 'application/json' }, &block) + def put(path, data, headers = {}, &block) + put_base(path, data, { 'content-type' => 'application/json' }.merge(headers), &block) end - def patch(path, data, &block) - patch_base(path, data, { 'content-type' => 'application/json' }, &block) + def patch(path, data, headers = {}, &block) + patch_base(path, data, { 'content-type' => 'application/json' }.merge(headers), &block) end - def delete(path, &block) - delete_base(path, &block) + def delete(path, headers = {}, &block) + delete_base(path, { 'content-type' => 'application/json' }.merge(headers), &block) end end end diff --git a/lib/action_integration/protocols/rest_base.rb b/lib/action_integration/protocols/rest_base.rb index 8a22701c5f..28a332925d 100644 --- a/lib/action_integration/protocols/rest_base.rb +++ b/lib/action_integration/protocols/rest_base.rb @@ -1,38 +1,42 @@ +require 'net/https' + module ActionIntegration module Protocols # Methods used by every other protocol module RestBase private - def get_base(path, &block) - action_base(path, nil, :get, &block) + def get_base(path, headers = {}, &block) + action_base(path, nil, Net::HTTP::Get, headers, &block) end - def post_base(path, data, options = {}, &block) - action_base(path, data, :post, options, &block) + def post_base(path, data, headers = {}, &block) + action_base(path, data, Net::HTTP::Post, headers, &block) end - def put_base(path, data, options = {}, &block) - action_base(path, data, :put, options, &block) + def put_base(path, data, headers = {}, &block) + action_base(path, data, Net::HTTP::Put, headers, &block) end - def patch_base(path, data, options = {}, &block) - action_base(path, data, :patch, options, &block) + def patch_base(path, data, headers = {}, &block) + action_base(path, data, Net::HTTP::Patch, headers, &block) end def delete_base(path, &block) - action_base(path, nil, :delete, &block) + action_base(path, nil, Net::HTTP::Delete, &block) end - def action_base(path, data, action, options = {}, &block) + def action_base(path, data, action_class, headers = {}, &block) url = URI.parse(path) http = Net::HTTP.new(url.host, url.port) - - action_class = "Net::HTTP::#{action.to_s.camelize}".constantize + http.use_ssl = true if url.scheme == 'https' request = action_class.new(url) request.body = data.to_json if data - request.content_type = options['content-type'] if options['content-type'] + # request.content_type = headers['content-type'] if headers['content-type'] + headers.each do |key, value| + request[key] = value + end handle_request(http, request, &block) end diff --git a/test/fixtures/call_messages.yml b/test/fixtures/call_messages.yml index c36d319178..9d10170dc0 100644 --- a/test/fixtures/call_messages.yml +++ b/test/fixtures/call_messages.yml @@ -27,7 +27,7 @@ # created_at :datetime not null # creator_id :integer # format :string -# headers :string +# headers :text # id :integer not null, primary key # ip_address :string # lock_version :integer default(0), not null diff --git a/test/models/call_message_test.rb b/test/models/call_message_test.rb index 9ab5202ca8..ad9e81ae6d 100644 --- a/test/models/call_message_test.rb +++ b/test/models/call_message_test.rb @@ -27,7 +27,7 @@ # created_at :datetime not null # creator_id :integer # format :string -# headers :string +# headers :text # id :integer not null, primary key # ip_address :string # lock_version :integer default(0), not null diff --git a/test/models/call_request_test.rb b/test/models/call_request_test.rb index caf83d6ae5..1c262b7ef8 100644 --- a/test/models/call_request_test.rb +++ b/test/models/call_request_test.rb @@ -27,7 +27,7 @@ # created_at :datetime not null # creator_id :integer # format :string -# headers :string +# headers :text # id :integer not null, primary key # ip_address :string # lock_version :integer default(0), not null diff --git a/test/models/call_response_test.rb b/test/models/call_response_test.rb index 398a241e9e..35fb80407d 100644 --- a/test/models/call_response_test.rb +++ b/test/models/call_response_test.rb @@ -27,7 +27,7 @@ # created_at :datetime not null # creator_id :integer # format :string -# headers :string +# headers :text # id :integer not null, primary key # ip_address :string # lock_version :integer default(0), not null From fa8edc213b3109640d5b22d95cd739cb6dddd734 Mon Sep 17 00:00:00 2001 From: Nicolas PROCUREUR Date: Wed, 15 Mar 2017 11:10:29 +0100 Subject: [PATCH 3/4] Fixes bad typo. Fixes #1736. --- app/models/deposit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/deposit.rb b/app/models/deposit.rb index 9c42efe939..cc7e9c0d9b 100644 --- a/app/models/deposit.rb +++ b/app/models/deposit.rb @@ -74,7 +74,7 @@ class Deposit < Ekylibre::Record::Base validate do if self.cash - error.add(:cash_id, :must_be_a_bank_account) unless self.cash.bank_account? + errors.add(:cash_id, :must_be_a_bank_account) unless self.cash.bank_account? end end From 64453a381cb13accf801035d526b4b6a6d6f9d93 Mon Sep 17 00:00:00 2001 From: Brice TEXIER Date: Wed, 15 Mar 2017 14:06:20 +0100 Subject: [PATCH 4/4] Change content-type case for ActionIntegration --- app/models/call_response.rb | 2 +- lib/action_integration/protocols/json.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/call_response.rb b/app/models/call_response.rb index d857c37454..d78f5da1cd 100644 --- a/app/models/call_response.rb +++ b/app/models/call_response.rb @@ -100,7 +100,7 @@ def self.create_from_savon_httpi_response!(response, request) status: response.code, headers: response.headers, body: response.raw_body, - format: response.headers['content-type'].split(';').first, + format: response.headers['Content-Type'].split(';').first, request: request ) r.save! diff --git a/lib/action_integration/protocols/json.rb b/lib/action_integration/protocols/json.rb index f9e29b0558..786cd0a975 100644 --- a/lib/action_integration/protocols/json.rb +++ b/lib/action_integration/protocols/json.rb @@ -5,23 +5,23 @@ module JSON include Protocols::RestBase def get(path, headers = {}, &block) - get_base(path, { 'content-type' => 'application/json' }.merge(headers), &block) + get_base(path, { 'Content-Type' => 'application/json' }.merge(headers), &block) end def post(path, data, headers = {}, &block) - post_base(path, data, { 'content-type' => 'application/json' }.merge(headers), &block) + post_base(path, data, { 'Content-Type' => 'application/json' }.merge(headers), &block) end def put(path, data, headers = {}, &block) - put_base(path, data, { 'content-type' => 'application/json' }.merge(headers), &block) + put_base(path, data, { 'Content-Type' => 'application/json' }.merge(headers), &block) end def patch(path, data, headers = {}, &block) - patch_base(path, data, { 'content-type' => 'application/json' }.merge(headers), &block) + patch_base(path, data, { 'Content-Type' => 'application/json' }.merge(headers), &block) end def delete(path, headers = {}, &block) - delete_base(path, { 'content-type' => 'application/json' }.merge(headers), &block) + delete_base(path, { 'Content-Type' => 'application/json' }.merge(headers), &block) end end end