class FfcrmEndpoint::Endpoint

Attributes

params[RW]
request[RW]

Public Class Methods

new(request) click to toggle source
# File lib/ffcrm_endpoint/endpoint.rb, line 14
def initialize(request)
  @request = request
  @params = request.params
  raise EndpointNoActionError, "No action defined." unless params[:klass_name].present?
end

Private Class Methods

inherited(subclass) click to toggle source

Endpoints must be registered (by subclassing) before they can be called. This ensures actions such as “/endpoints/object” don’t result in dangerous calls to the Object class

# File lib/ffcrm_endpoint/endpoint.rb, line 65
def self.inherited(subclass)
  # handle name duplicates
  if (duplicates = klasses.select{ |klass| klass.to_s.demodulize == subclass.to_s.demodulize }).any?
    raise EndpointDuplicateError, "You cannot have endpoints with the same name: #{duplicates.join(', ')}"
  else
    klasses << subclass
  end
end

Public Instance Methods

authenticate() click to toggle source

The authentication method is called by the endpoint controller before ‘process’ is run. ‘process’ will only be called if authenticate returns true

# File lib/ffcrm_endpoint/endpoint.rb, line 30
def authenticate
  klass.new(request).authenticate == true
end
process() click to toggle source

The main function to process the incoming data. It delegates to the process function in your custom endpoint class.

# File lib/ffcrm_endpoint/endpoint.rb, line 23
def process
  klass.new(request).process
end

Protected Instance Methods

config() click to toggle source

Configuration is assumed to be in Settings namespaced under the name of the action If class is MyCustomEndpoint, then configuration should be in Setting You can override this behaviour in the endpoint subclass if desired

# File lib/ffcrm_endpoint/endpoint.rb, line 40
def config
  Setting[klass_name]
end
klass() click to toggle source

klasses are stored as MyPluginName::MyCustomEndpoint Return first match on “MyCustomEndpoint”

# File lib/ffcrm_endpoint/endpoint.rb, line 47
def klass
  @klass ||= klasses.select{|k| k.to_s.demodulize == klass_name}.compact.first
  raise EndpointNotDefinedError, "To call /endpoints/#{params[:klass_name]} you must define 'class #{params[:klass_name].classify} < FfcrmEndpoint::Endpoint'" if !@klass.present?
  @klass
end
klass_name() click to toggle source

We derive the name of the endpoint subclass from the action /endpoints/my_custom_endpoint corresponds to MyCustomEndpoint

# File lib/ffcrm_endpoint/endpoint.rb, line 56
def klass_name
  "#{params[:klass_name].classify}"
end