Class: Locomotive::Mounter::EngineApi

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/locomotive/mounter/engine_api.rb

Class Method Summary collapse

Class Method Details

.set_token(*args) ⇒ String

Get a new token from the Engine API and set it for this class. It raises an exception if the operation fails. Example of the base uri: locomotivecms.com, nocoffee.fr.

Parameters:

  • *args (Hash / Array)

    A hash or parameters in that order: uri, email, password or uri, api_key

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/locomotive/mounter/engine_api.rb', line 20

def self.set_token(*args)
  _credentials = self.credentials(args)

  uri = URI _credentials.delete(:uri)
  self.base_uri uri.to_s
  self.basic_auth uri.user, uri.password if uri.userinfo

  response = post('/tokens.json', body: _credentials) #{ email: email, password: password })

  if response.code < 400
    self.default_params auth_token: response['token']
    response['token']
  elsif response.code == 404 # ssl option missing
    raise WrongCredentials.new("#{uri}/tokens.json does not respond. Perhaps, the ssl option is missing in your config/deploy.yml file")
  else
    raise WrongCredentials.new("#{response['message']} (#{response.code})")
  end
end

.teardownObject



39
40
41
42
43
# File 'lib/locomotive/mounter/engine_api.rb', line 39

def self.teardown
  Locomotive::Mounter::EngineApi.default_options[:base_uri] = nil
  Locomotive::Mounter::EngineApi.default_options[:base_auth] = nil
  Locomotive::Mounter::EngineApi.default_options[:default_params] = {}
end