module ActiveCrew::Responders::FayeResponder

ActiveCrew Faye responder.

Constants

MAX_RETRIES

Public Instance Methods

channel(invoker) click to toggle source

@return Invoker channel name

# File lib/active_crew/responders/faye_responder.rb, line 42
def channel(invoker)
  "/#{invoker.class.to_s.underscore}/#{invoker.id}"
end
config() click to toggle source
# File lib/active_crew/responders/faye_responder.rb, line 97
def config
  return @config if defined? @config

  @config = YAML.load(ERB.new(File.new(config_path).read).result)[Rails.env]
  @config = (@config || {}).symbolize_keys
end
config_path() click to toggle source
# File lib/active_crew/responders/faye_responder.rb, line 104
def config_path
  File.join(Rails.root, 'config', 'faye.yml')
end
header() click to toggle source

@return Faye request header

# File lib/active_crew/responders/faye_responder.rb, line 77
def header
  {
    'Content-Type' => 'application/json',
    'Pragma' => 'no-cache',
    'X-Requested-With' => 'XMLHttpRequest'
  }
end
init(context, request) click to toggle source
# File lib/active_crew/responders/faye_responder.rb, line 15
def init(context, request)
  context[:session] = request.headers['x-session-token']
end
payload(name, invoker, context, model) click to toggle source

@return Faye request payload

# File lib/active_crew/responders/faye_responder.rb, line 47
def payload(name, invoker, context, model)
  {
    invoker: serialize_invoker(invoker),
    session: context[:session],
    command: name,
    status: status(model),
    response: serialize(model)
  }
end
request(message) click to toggle source
# File lib/active_crew/responders/faye_responder.rb, line 26
def request(message)
  retries = 0

  begin
    validate RestClient.post url, message.to_json, header
  rescue FayeResponderError
    retries += 1
    raise FayeConnectionError $ERROR_INFO unless retries < MAX_RETRIES

    retry
  rescue FayeConnectionError
    Rails.logger.fatal $ERROR_INFO.message
  end
end
respond(name, invoker, context, model) click to toggle source

Respond with faye

# File lib/active_crew/responders/faye_responder.rb, line 20
def respond(name, invoker, context, model)
  request channel: channel(invoker),
          data: payload(name, invoker, context, model),
          ext: { publish_key: config[:publish_key] }
end
serialize(model, options = {}) click to toggle source

Serialize payload

# File lib/active_crew/responders/faye_responder.rb, line 62
def serialize(model, options = {})
  return { base: model.message } if model.is_a? CommandError

  if model.is_a?(Array) || model.errors.empty?
    ActiveModelSerializers::SerializableResource.new(model, options).serializable_hash
  else
    ActiveModelSerializers::SerializableResource.new(model.errors, options.merge(root: 'errors')).serializable_hash[:errors]
  end
end
serialize_invoker(invoker) click to toggle source
# File lib/active_crew/responders/faye_responder.rb, line 57
def serialize_invoker(invoker)
  serialize invoker, serializer: "#{invoker.class}::InvokerSerializer".constantize
end
status(model) click to toggle source
# File lib/active_crew/responders/faye_responder.rb, line 72
def status(model)
  model.is_a?(Array) || !model.is_a?(CommandError) && model.valid? ? :success : :failure
end
url() click to toggle source
# File lib/active_crew/responders/faye_responder.rb, line 93
def url
  "http://#{config[:host]}/faye"
end
validate(response) click to toggle source

Validate faye response

# File lib/active_crew/responders/faye_responder.rb, line 86
def validate(response)
  fail FayeResponderError, response.code unless response.code == 200

  response = JSON.parse(response)[0].symbolize_keys
  fail FayeResponderError, response[:error] unless response[:successful]
end