Class: Locomotive::Mounter::EngineApi
- Inherits:
-
Object
- Object
- Locomotive::Mounter::EngineApi
- Includes:
- HTTMultiParty
- Defined in:
- lib/locomotive/mounter/engine_api.rb
Class Method Summary collapse
-
.create(resource_name, attributes, locale = nil, attribute_names = nil) ⇒ Object
Create a resource from the API.
-
.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.
-
.set_token(*args) ⇒ String
Get a new token from the Engine API and set it for this class.
- .teardown ⇒ Object
-
.update(resource_name, id, attributes, locale = nil, attribute_names = nil) ⇒ Object
Update a resource from the API.
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.
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.
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.
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 |
.teardown ⇒ Object
117 118 119 120 121 |
# File 'lib/locomotive/mounter/engine_api.rb', line 117 def self.teardown Locomotive::Mounter::EngineApi.[:base_uri] = nil Locomotive::Mounter::EngineApi.[:base_auth] = nil Locomotive::Mounter::EngineApi.[: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.
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 |