Sha256: c7b5d13fa29e0e608e98adf523f8b7614da0893685d036be9194fc26781e214e

Contents?: true

Size: 1.76 KB

Versions: 47

Compression:

Stored size: 1.76 KB

Contents

module Picky

  module Adapters
    # This is an adapter that is plugged into a Rack outlet.
    #
    # It looks at what is given to it and generate an appropriate
    # adapter for it.
    #
    # For example, if you give it a query, it will extract the query param etc.
    # and call search on it if it is called by Rack.
    #
    module Rack

      class Search < Base

        @@defaults = {
          query_key:    'query'.freeze,
          ids_key:      'ids'.freeze,
          offset_key:   'offset'.freeze,
          content_type: 'application/json'.freeze
        }

        def initialize query
          @query    = query
          @defaults = @@defaults.dup
        end

        def to_app options = {}
          # For capturing in the lambda.
          #
          query        = @query
          query_key    = options[:query_key]    || @defaults[:query_key]
          content_type = options[:content_type] || @defaults[:content_type]

          lambda do |env|
            params  = ::Rack::Request.new(env).params

            results = query.search *extracted(params)

            Picky.logger && Picky.logger.info(results)

            respond_with results.to_json, content_type
          end
        end

        # Helper method to extract the params
        #
        # Defaults are 20 ids, offset 0.
        #
        UTF8_STRING = 'UTF-8'.freeze
        def extracted params
          [
            # query is encoded in ASCII
            #
            params[@defaults[:query_key]]  && params[@defaults[:query_key]].force_encoding(UTF8_STRING),
            params[@defaults[:ids_key]]    && params[@defaults[:ids_key]].to_i || 20,
            params[@defaults[:offset_key]] && params[@defaults[:offset_key]].to_i || 0
          ]
        end

      end

    end

  end

end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
picky-4.0.0pre1 lib/picky/adapters/rack/search.rb
picky-3.6.16 lib/picky/adapters/rack/search.rb
picky-3.6.15 lib/picky/adapters/rack/search.rb
picky-3.6.14 lib/picky/adapters/rack/search.rb
picky-3.6.13 lib/picky/adapters/rack/search.rb
picky-3.6.12 lib/picky/adapters/rack/search.rb
picky-3.6.11 lib/picky/adapters/rack/search.rb
picky-3.6.10 lib/picky/adapters/rack/search.rb
picky-3.6.9 lib/picky/adapters/rack/search.rb
picky-3.6.8 lib/picky/adapters/rack/search.rb
picky-3.6.7 lib/picky/adapters/rack/search.rb
picky-3.6.6 lib/picky/adapters/rack/search.rb
picky-3.6.4 lib/picky/adapters/rack/search.rb
picky-3.6.3 lib/picky/adapters/rack/search.rb
picky-3.6.2 lib/picky/adapters/rack/search.rb
picky-3.6.1 lib/picky/adapters/rack/search.rb
picky-3.6.0 lib/picky/adapters/rack/search.rb
picky-3.5.4 lib/picky/adapters/rack/search.rb
picky-3.5.3 lib/picky/adapters/rack/search.rb
picky-3.5.2 lib/picky/adapters/rack/search.rb