Class: Stickler::Middleware::Index

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Logable, Helpers::Compression, Helpers::Specs
Defined in:
lib/stickler/middleware/index.rb

Overview

Index is a Rack middleware that passes all requests through except for those matching these two urls:

/specs.#Gem.marhsal_version.gz

The [ name, version, platform ] index of <b>all<b> the gems in the entire repository

/latest_specs.#Gem.marshal_version.gz

The [ name, version, # platform ] index of the most recent version of each gem in the repository.

For these 2 urls, it respond reponds with the summation of all the specs that are in env['stickler.specs']. If there are no specs in that environment variable, then it returns with an empty index.

Options

This class is also the base class for all the other GemServer type middlewares, so there is an optional behavior to NOT respond to the index url requests and just append the spec, or latest_specs to env instead of serving the values out of there.

:serve_indexes

true or false it defaults to true. This option is used when Index is used in a stack with other Index derived middlewares. In this case, all of the Index derived middlewares should set :serve_indexes => false except for the bottom one. It should set :serve_indexes => true. This allows all the Index derived middlewares to cooperatively respond to the /specs and </b>/latests_specs</b> urls.

Usage

use Stickler::Middleware::Index, :serve_indexes => true use Stickler::Middleware::Index, :serve_indexes => false

Direct Known Subclasses

Local, Mirror

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logable

#logger

Methods included from Helpers::Specs

#append_latest_specs, #append_spec, #append_specs, #specs, #specs_by_first_upcase_char, #specs_by_name, #specs_by_repo, #specs_grouped_by_name

Methods included from Helpers::Compression

#compression, #compression=

Constructor Details

#initialize(app, opts = {}) ⇒ Index

Returns a new instance of Index.



62
63
64
65
66
67
# File 'lib/stickler/middleware/index.rb', line 62

def initialize( app, opts = {} )
  @app           = app
  @repo          = ::Stickler::Repository::Null.new
  @serve_indexes = opts.has_key?( :serve_indexes ) ? opts[:serve_indexes] : true
  super( app )
end

Instance Attribute Details

#repoObject (readonly)

The respository of the Index is a Repository::Null



54
55
56
# File 'lib/stickler/middleware/index.rb', line 54

def repo
  @repo
end

Instance Method Details

#marshal(data) ⇒ Object



151
152
153
154
# File 'lib/stickler/middleware/index.rb', line 151

def marshal( data )
  content_type 'application/octet-stream'
  ::Marshal.dump( data )
end

#marshalled_specs(spec_a) ⇒ Object

Convert to the array format used by gem servers everywhere



147
148
149
# File 'lib/stickler/middleware/index.rb', line 147

def marshalled_specs( spec_a )
  marshal( spec_a.collect { |s| s.to_rubygems_a } )
end

#serve_indexes(with_compression = :none) ⇒ Object

Serve the indexes up as the response if @serve_indexes is true. Otherwise return false



103
104
105
106
107
108
109
110
# File 'lib/stickler/middleware/index.rb', line 103

def serve_indexes( with_compression = :none )
  if @serve_indexes then
    self.compression = to_compression_flag( with_compression )
    return marshalled_specs( specs )
  else
    pass
  end
end

#to_compression_flag(with_compression) ⇒ Object



156
157
158
159
160
161
# File 'lib/stickler/middleware/index.rb', line 156

def to_compression_flag( with_compression )
  return with_compression if [ :gzip, :deflate, :none ].include?( with_compression )
  return :gzip            if with_compression =~ /\.gz\Z/i
  return :deflate         if with_compression =~ /\.(Z|rz)\Z/i
  return :none
end