Sha256: f01a222b11fa75a712e516d168ee651c950af22b5a54e099fc7c6e74123477b2

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'cgi'

module Alephant
  module Broker
    class Request
      attr_reader :type, :component_id, :options, :extension, :content_type

      DEFAULT_EXTENSION = :html

      @@extension_mapping = {
        :html => 'text/html',
        :json => 'application/json'
      }

      def initialize(path, querystring)
        request = request_components(path, querystring)
        case request[:type]
        when "component"
          @type = :asset

          @component_id = request[:component_id]
          raise Errors::InvalidAssetId.new("No Asset ID specified") if @component_id.nil?

          @options = request[:options]
          @extension = request[:extension] || DEFAULT_EXTENSION
          @content_type = @@extension_mapping[@extension.to_sym] || @@extension_mapping[DEFAULT_EXTENSION]
        when "status"
          @type = :status
        else
          @type = :notfound
        end
      end

      private

      def request_components(path, query_string)
        request_parts = path.split('/')
        {
          :type         => request_parts[1],
          :component_id => (request_parts[2] || '').split('.')[0],
          :extension    => path.split('.')[1],
          :options      => options_from(query_string)
        }
      end

      def options_from(query_string)
        query_string.split('&').reduce({}) do |object, key_pair|
          key, value = key_pair.split('=')
          object.tap { |o| o.store(key.to_sym, value) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-broker-0.0.2 lib/alephant/broker/models/request.rb