From 711b18f6cd8067db1d7d3df132d52db9d762bf8c Mon Sep 17 00:00:00 2001 From: Nicolas Florentin Date: Wed, 20 Apr 2016 18:13:36 +0200 Subject: [PATCH] initial commit --- Gemfile | 2 ++ Gemfile.lock | 6 ++++ app/models/project.rb | 1 + app/models/project/openlab_sync.rb | 54 +++++++++++++++++++++++++++++ app/workers/openlab_worker.rb | 22 ++++++++++++ config/application.yml.default | 2 ++ config/initializers/openlab_ruby.rb | 4 +++ config/secrets.yml | 5 ++- 8 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 app/models/project/openlab_sync.rb create mode 100644 app/workers/openlab_worker.rb create mode 100644 config/initializers/openlab_ruby.rb diff --git a/Gemfile b/Gemfile index 4359d9755..b8aa689be 100644 --- a/Gemfile +++ b/Gemfile @@ -137,3 +137,5 @@ gem 'chroma' gem 'protected_attributes' gem 'message_format' + +gem 'openlab_ruby' diff --git a/Gemfile.lock b/Gemfile.lock index 7e86fbd04..fa7591a7d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -174,6 +174,9 @@ GEM http-cookie (1.0.2) domain_name (~> 0.5) http_parser.rb (0.6.0) + httparty (0.13.7) + json (~> 1.8) + multi_xml (>= 0.5.2) i18n (0.7.0) ice_nine (0.11.1) jbuilder (2.2.12) @@ -242,6 +245,8 @@ GEM omniauth-oauth2 (1.3.1) oauth2 (~> 1.0) omniauth (~> 1.2) + openlab_ruby (0.0.2) + httparty (~> 0.13) orm_adapter (0.5.0) pdf-core (0.5.1) pg (0.18.1) @@ -458,6 +463,7 @@ DEPENDENCIES oj omniauth omniauth-oauth2 + openlab_ruby pg prawn prawn-table diff --git a/app/models/project.rb b/app/models/project.rb index 4a0286bbc..57bbafd7c 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,6 +1,7 @@ class Project < ActiveRecord::Base include AASM include NotifyWith::NotificationAttachedObject + include OpenlabSync # elastic initialisations include Elasticsearch::Model diff --git a/app/models/project/openlab_sync.rb b/app/models/project/openlab_sync.rb new file mode 100644 index 000000000..5e38db976 --- /dev/null +++ b/app/models/project/openlab_sync.rb @@ -0,0 +1,54 @@ +module Project::OpenlabSync + extend ActiveSupport::Concern + + included do + after_create :openlab_create, if: :openlab_sync_active? + after_update :openlab_update, if: :openlab_sync_active? + after_destroy :openlab_destroy, if: :openlab_sync_active? + + def openlab_create + OpenlabSync.perform_async(:create, self.id) if self.published? + end + + def openlab_update + if self.published? + if self.state_was == 'draft' + OpenlabSync.perform_async(:create, self.id) + else + OpenlabSync.perform_async(:update, self.id) + end + end + + end + + def openlab_destroy + OpenlabSync.perform_async(:destroy, self.id) + end + + def openlab_attributes + { + id: id, name: name, description: description, tags: tags, + machines: machines.map(&:name), + components: components.map(&:name), + themes: themes.map(&:name), + author: author.profile.full_name, + collaborators: users.map { |u| u.profile.full_name }, + steps_body: steps_body + } + end + + def steps_body + + end + + def openlab_sync_active? + self.class.openlab_sync_active? + end + end + + class_methods do + def openlab_sync_active? + Rails.application.secrets.openlab_app_secret.present? + end + end +end diff --git a/app/workers/openlab_worker.rb b/app/workers/openlab_worker.rb new file mode 100644 index 000000000..835b8f7ad --- /dev/null +++ b/app/workers/openlab_worker.rb @@ -0,0 +1,22 @@ +class OpenlabWorker + include Sidekiq::Worker + sidekiq_options queue: 'default', retry: true + + Logger = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil + openlab_client = Openlab::Projects.new + + def perform(action, project_id) + logger.debug ["Openlab sync", action, "project ID: #{project_id}"] + + case action.to_s + when /create/ + project = Project.find(project_id) + openlab_client.create(project.openlab_attributes) + when /update/ + project = Project.find(project_id) + openlab_client.update(project_id, project.openlab_attributes) + when /destroy/ + openlab_client.destroy(project_id) + end + end +end diff --git a/config/application.yml.default b/config/application.yml.default index 0807bb263..65d211d40 100644 --- a/config/application.yml.default +++ b/config/application.yml.default @@ -47,3 +47,5 @@ TIME_ZONE: 'Paris' WEEK_STARTING_DAY: 'monday' D3_DATE_FORMAT: '%d/%m/%y' UIB_DATE_FORMAT: 'dd/MM/yyyy' + +OPENLAB_APP_SECRET: 'fSF9jZEWxjHyqjAzzg34jd92' diff --git a/config/initializers/openlab_ruby.rb b/config/initializers/openlab_ruby.rb new file mode 100644 index 000000000..314d55348 --- /dev/null +++ b/config/initializers/openlab_ruby.rb @@ -0,0 +1,4 @@ +Openlab.configure do |config| + config.app_secret = Rails.application.secrets.openfablab_app_secret + config.base_uri = Rails.env.production? ? "https://urltochange.nawak" : "localhost:3300/api/v1" +end diff --git a/config/secrets.yml b/config/secrets.yml index 946b82794..009abce29 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -28,6 +28,7 @@ development: messageformat_locale: <%= ENV["MESSAGEFORMAT_LOCALE"] %> fullcalendar_locale: <%= ENV["FULLCALENDAR_LOCALE"] %> elasticsearch_language_analyzer: <%= ENV["ELASTICSEARCH_LANGUAGE_ANALYZER"] %> + openlab_app_secret: <%= ENV["OPENLAB_APP_SECRET"] %> test: secret_key_base: 83daf5e7b80d990f037407bab78dff9904aaf3c195a50f84fa8695a22287e707dfbd9524b403b1dcf116ae1d8c06844c3d7ed942564e5b46be6ae3ead93a9d30 @@ -47,7 +48,7 @@ test: messageformat_locale: en fullcalendar_locale: en elasticsearch_language_analyzer: french - + openlab_app_secret: staging: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> @@ -74,6 +75,7 @@ staging: messageformat_locale: <%= ENV["MESSAGEFORMAT_LOCALE"] %> fullcalendar_locale: <%= ENV["FULLCALENDAR_LOCALE"] %> elasticsearch_language_analyzer: <%= ENV["ELASTICSEARCH_LANGUAGE_ANALYZER"] %> + openlab_app_secret: <%= ENV["OPENLAB_APP_SECRET"] %> # Do not keep production secrets in the repository, # instead read values from the environment. @@ -102,3 +104,4 @@ production: messageformat_locale: <%= ENV["MESSAGEFORMAT_LOCALE"] %> fullcalendar_locale: <%= ENV["FULLCALENDAR_LOCALE"] %> elasticsearch_language_analyzer: <%= ENV["ELASTICSEARCH_LANGUAGE_ANALYZER"] %> + openlab_app_secret: <%= ENV["OPENLAB_APP_SECRET"] %>