Sha256: da0439b2f569b04c2d3eb90a60c7fba3195ccb93b09e5784cda712a75877040a

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

##
class Cartage::Rack
  class << self
    # Creates a new instance of the Cartage::Rack::Simple application to the
    # specified +root_path+, or +Dir.pwd+. This method has been deprecated.
    def mount(root_path = nil)
      warn <<-warning
Cartage::Rack.mount(path) is deprecated; use Cartage::Rack::Simple(path) instead.
      warning
      Simple(root_path)
    end

    # Generate and mount the simple metadata reporter, Cartage::Rack::Simple,
    # for the application at +root_path+ or +Dir.pwd+.
    def Simple(root_path = nil)
      Cartage::Rack::Simple.new(root_path)
    end
  end

  # Cartage::Rack::Simple is a simple application that reads an application's
  # static release metadata and returns it as a +text/plain+ string, or as
  # +application/json+ if it is called with +.json+.
  #
  # If static release metadata does not exist and the application is in
  # +development+ or +test+ modes, Cartage::Rack::Simple will read data from
  # the operating environment.
  #
  # See Cartage::Rack::Metadata for more information.
  class Simple < Cartage::Rack
    private

    def resolve_content(env)
      full_content = @metadata.resolve

      content = {}

      content[:env] = dig(full_content, 'env', 'name')
      content[:release_hashref] = dig(full_content, 'package', 'hashref')
      content[:timestamp] = dig(full_content, 'package', 'timestamp')

      case env['PATH_INFO']
      when /\.json\z/
        type = 'application/json'
        body = content.to_json
      else
        type = 'text/plain'
        body = "#{content[:env]}: #{content[:release_hashref]}"
        body += " (#{content[:timestamp]})" if content[:timestamp]
      end

      [ type, body ]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cartage-rack-2.2 lib/cartage/rack/simple.rb
cartage-rack-2.1 lib/cartage/rack/simple.rb
cartage-rack-2.0 lib/cartage/rack/simple.rb
cartage-rack-2.0.rc1 lib/cartage/rack/simple.rb