Module: Motor::Configs::SyncWithRemote

Defined in:
lib/motor/configs/sync_with_remote.rb

Constant Summary collapse

UnableToSync =
Class.new(StandardError)
ApiNotFound =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.call(remote_url, api_key) ⇒ Object



11
12
13
14
15
16
# File 'lib/motor/configs/sync_with_remote.rb', line 11

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

.sync_from_remote!(remote_url, api_key) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
# File 'lib/motor/configs/sync_with_remote.rb', line 18

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

.sync_to_remote!(remote_url, api_key) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/motor/configs/sync_with_remote.rb', line 29

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