Sha256: c52189dc35c8ddaa6bb883615b3ad4970fb077a0081e84c4cee429823a99571e

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

module Stylish
  module Developer
    class ModelDelegator
      attr_reader :path, :action, :request, :library, :prefix, :parts, :path_args

      attr_accessor :code

      def initialize(path, action, prefix, options)
        @path       = path
        @action     = action
        @request    = options.fetch(:request, nil)
        @path_args  = options.fetch(:path_args, [])
        @prefix     = prefix
        @library    = library || Stylish::Developer.config.library
        @code       = 200
        @parts      = path.split("/")
      end

      def response_body
        case
        when prefix == "packages" && action == "browse"
          library.packages.map(&:to_hash)
        when prefix == "packages" && action == "show"
          slug = parts.first
          package = library.find_package(slug)
          package.to_hash(true)
        when %w(layouts components pages).include?(prefix)
          slug = parts.first
          package = library.find_package(slug)
          resp = package.handle_request(prefix, action, parts, request: request, params: request.params.to_mash)

          resp
        else
          self.code = 400

          {
            error: "invalid request",
            path: path,
            action: action,
            prefix: prefix,
            request: request.path
          }
        end
      end

      def to_rack_response
        [code, {"Content-Type"=>"application/json"}, [response_body.to_json] ]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stylish-0.3.1 lib/stylish/developer/model_delegator.rb
stylish-0.3.0 lib/stylish/developer/model_delegator.rb
stylish-0.0.2 lib/stylish/developer/model_delegator.rb