require 'restclient' module RailsConnector # Provides a simple wrapper for the CMS Rest API # using the credentials from {RailsConnector::Configuration.cms_api_options}. # # @example Request the published workspace: # RailsConnector::CmsRestApi.get('workspaces/published') # # @example Create a new Obj: # RailsConnector::CmsRestApi.post('revisions/001384beff9e5845/objs', # {'obj' => {'_path' => '/new_obj', '_obj_class' => 'Publication'}}) # # @example Update an Obj: # RailsConnector::CmsRestApi.put('revisions/001384beff9e5845/objs/9e432077f0412a63', # {'obj' => {'title' => 'new title'}}) # # @example Delete an Obj: # RailsConnector::CmsRestApi.delete('revisions/001384beff9e5845/objs/f4123622ff07b70b') class CmsRestApi def self.get(resource_path, payload = nil) request_cms_api(:get, resource_path, payload) end def self.put(resource_path, payload) request_cms_api(:put, resource_path, payload) end def self.post(resource_path, payload) request_cms_api(:post, resource_path, payload) end def self.delete(resource_path) request_cms_api(:delete, resource_path, nil) end private def self.request_cms_api(action, resource_path, payload) request_params = basic_request_params request_params[:method] = action request_params[:url] = url(resource_path) request_params[:payload] = MultiJson.encode(payload) if payload.present? MultiJson.decode(RestClient::Request.execute(request_params)) end def self.credentials RailsConnector::Configuration.cms_api_options end def self.basic_request_params headers = { :content_type => :json, :accept => :json, } headers[:host] = credentials[:http_host] if credentials[:http_host].present? { :user => credentials[:login], :password => credentials[:api_key], :headers => headers, } end def self.url(resource_path) "#{credentials[:url]}/#{resource_path}" end end end