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

.create(resource_name, attributes, locale = nil, attribute_names = nil) ⇒ Object

Create a resource from the API. Raise an exception if something went wrong.

Parameters:

  • resource_name (String)

    The path to the resource (usually, the resource name)

  • attributes (Hash)

    The attributes of the resource

  • locale (String) (defaults to: nil)

    The locale for the request

  • attribute_names (Array) (defaults to: nil)

    The attributes we want to keep in the response

Returns:

  • (Object)

    The response of the API



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/locomotive/mounter/engine_api.rb', line 82

def self.create(resource_name, attributes, locale = nil, attribute_names = nil)
  params    = self.build_create_or_params(resource_name, attributes, locale)
  response  = self.post("/#{resource_name}.json", params)
  data      = response.parsed_response

  if response.success?
    self.keep_attributes(data, attribute_names)
  else
    raise ApiWriteException.new(data)
  end
end

.fetch(resource_name, query = {}, locale = nil, attribute_names = nil) ⇒ Object

Read a resource or a list of resources from the API Raise an exception if something went wrong.

Parameters:

  • resource_name (String)

    The path to the resource (usually, the resource name)

  • query (Hash) (defaults to: {})

    If we want to filter the results

  • locale (String) (defaults to: nil)

    The locale for the request

  • attribute_names (Array) (defaults to: nil)

    The attributes we want to keep in the response



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/locomotive/mounter/engine_api.rb', line 47

def self.fetch(resource_name, query = {}, locale = nil, attribute_names = nil)
  params = { query: query || {} }
  params[:query][:locale] = locale if locale

  url       = "/#{resource_name}.json"
  response  = self.get(url, params)
  data      = response.parsed_response

  if response.success?
    object_or_list = self.keep_attributes(data, attribute_names)

    if response.headers['x-total-pages']
      PaginatedCollection.new(url, params, attribute_names).tap do |collection|
        collection.list           = object_or_list
        collection.total_pages    = response.headers['x-total-pages'].to_i
        collection.total_entries  = response.headers['x-total-entries'].to_i
      end
    else
      object_or_list
    end
  else
    raise ApiReadException.new(data['error'])
  end
end

.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



117
118
119
120
121
# File 'lib/locomotive/mounter/engine_api.rb', line 117

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

.update(resource_name, id, attributes, locale = nil, attribute_names = nil) ⇒ Object

Update a resource from the API. Raise an exception if something went wrong.

Parameters:

  • resource_name (String)

    The path to the resource (usually, the resource name)

  • id (String)

    The unique identifier of the resource

  • attributes (Hash)

    The attributes of the resource

  • locale (String) (defaults to: nil)

    The locale for the request

  • attribute_names (Array) (defaults to: nil)

    The attributes we want to keep in the response

Returns:

  • (Object)

    The response of the API



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/locomotive/mounter/engine_api.rb', line 105

def self.update(resource_name, id, attributes, locale = nil, attribute_names = nil)
  params    = self.build_create_or_params(resource_name, attributes, locale)
  response  = self.put("/#{resource_name}/#{id}.json", params)
  data      = response.parsed_response

  if response.success?
    self.keep_attributes(data, attribute_names)
  else
    raise ApiWriteException.new(data)
  end
end