Sha256: 95c6984844eb2d43ba2d58803bafae752f06de4f0ee3a7a255c3cd64a41e1d49

Contents?: true

Size: 1.36 KB

Versions: 217

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Motor
  module Configs
    module SyncWithRemote
      UnableToSync = Class.new(StandardError)
      ApiNotFound = Class.new(StandardError)

      module_function

      def call(remote_url, api_key)
        url = remote_url.delete_suffix('/') + Motor::Configs::SYNC_API_PATH

        sync_from_remote!(url, api_key)
        sync_to_remote!(url, api_key)
      end

      def sync_from_remote!(remote_url, api_key)
        response = Motor::NetHttpUtils.get(remote_url, {}, { 'X-Authorization' => api_key })

        raise ApiNotFound if response.is_a?(Net::HTTPNotFound)
        raise UnableToSync, [response.message, response.body].join(': ') unless response.is_a?(Net::HTTPSuccess)

        configs_hash = JSON.parse(response.body)

        Motor::Configs::SyncFromHash.call(configs_hash)
      end

      def sync_to_remote!(remote_url, api_key)
        configs_hash = Motor::Configs::BuildConfigsHash.call

        response = Motor::NetHttpUtils.post(
          remote_url,
          {},
          {
            'X-Authorization' => api_key,
            'Content-Type' => 'application/json'
          },
          configs_hash.to_json
        )

        raise ApiNotFound if response.is_a?(Net::HTTPNotFound)
        raise UnableToSync, [response.message, response.body].join(': ') unless response.is_a?(Net::HTTPSuccess)
      end
    end
  end
end

Version data entries

217 entries across 217 versions & 7 rubygems

Version Path
motor-admin-0.4.36 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.35 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.34 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.33 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.32 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.31 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.30 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.29 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.28 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.35 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.34 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.33 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.32 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.31 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.30 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.29 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.28 lib/motor/configs/sync_with_remote.rb
motor-admin-cstham8-0.4.27 lib/motor/configs/sync_with_remote.rb
motor-admin-0.4.27 lib/motor/configs/sync_with_remote.rb
motor-admin-unosquare-0.4.28 lib/motor/configs/sync_with_remote.rb