Sha256: e3c54ef138a2a253e5b7285e1d107f792ac4c43287d933c2b030cae00f3e9f6e

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module BeanstalkApi

  class ApplicationController <  ActionController::Base
    before_filter :restrict_access

    def authenticated_entity
      @api_key.try(:authenticateable)
    end

    private

    #
    # This is different from the standard API authentication because we need to
    # restrict access to only the API key that is associated with the providigm account.
    #
    def restrict_access
      authorization_header = request.headers['Authorization'] || ''
      
      if authorization_header =~ /^Basic/
        authenticate_or_request_with_http_basic do | username, password |
          @api_key = ApiKey.where(access_token: password).first
          @api_key.present? && @api_key.authenticateable.id.to_s == username && username == '1'
        end
      else
        authenticate_or_request_with_http_token do |token, options|
          @api_key = ApiKey.where(access_token: token).first
          @api_key.present? && @api_key.authenticateable.id.to_s == '1'
        end
      end
    end

    private

    def format_job(job)
      hash = {}
      if job.is_a?(Hash)
        job.each_pair do | host, job |
          hash[job.id] = job.body
        end
      else
        hash[job.id] = job.body
      end
      hash
    end

    def beanstalk_client
      Ayl::Engine.get_active_engine
    end

    def pool
      beanstalk_client.pool
    end

    def connection
      pool.open_connections.first
    end

    def beanstalk_running
      render json: { error: "Beanstalk not running" } unless beanstalk_client.asynchronous?
    end

    def default_tube
      defined?(ASYNC_TUBE) ? ASYNC_TUBE : 'default'
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pvdgm_beanstalk_api-0.1.0 app/controllers/beanstalk_api/application_controller.rb