module Repo

Repo is a Wire::App for accessing versioned content @author Bryan T. Meyers

Public Class Methods

read_configs() click to toggle source

Read all of the configs in './configs/repos' @return [void]

# File lib/app/repo.rb, line 35
def self.read_configs
  Wire::Config.read_config_dir('config/repos', nil)
end

Public Instance Methods

configure(conf) click to toggle source

Configure repo with listing template @param [Hash] conf the raw configuration @return [Hash] post-processed configuration

# File lib/app/repo.rb, line 28
def configure(conf)
  conf['listing'] = Tilt.new(conf['listing'], 1)
  conf
end
do_create(context) click to toggle source

Create a new file @param [Hash] context the context for this request @return [Response] status code

# File lib/app/repo.rb, line 42
def do_create(context)
  conf    = context.closet.repos[context.config['repo']]
  repo    = context.resource
  content = context.json
  id      = context.id
  if content[:file]
    file = content[:file][:content].match(/base64,(.*)/)[1]
    file = Base64.decode64(file)
    if context.query[:type]
      mime = context.query[:type]
    else
      mime = content[:file][:mime]
    end
    do_create_file(conf, repo, id, file, content[:message], mime, context.user)
  else
    do_create_file(conf, repo, id, URI.unescape(content[:updated]), content[:message], context.query[:type], context.user)
  end
end
do_read(context) click to toggle source

Get the a single file or directory listing @param [Hash] context the context for this request @return [Response] the file, listing, or status code

# File lib/app/repo.rb, line 86
def do_read(context)
  conf     = context.closet.repos[context.config['repo']]
  referrer = context.referer
  repo     = context.resource
  id       = context.id
  rev      = context.query[:rev]
  info     = do_read_info(conf, repo, id, rev)
  if info == 404
    return 404
  end
  type = info[:@kind]
  if type.eql? 'dir'
    mime     = 'text/html'
    list     = do_read_listing(conf, repo, id)
    template = context.config['listing']
    body     = template.render(self,
                               list:     list,
                               resource: repo,
                               id:       id,
                               referrer: referrer)
  else
    body = do_read_file(conf, repo, id, rev)
    if body == 500
      return body
    end
    mime = do_read_mime(conf, repo, id, rev)
  end
  headers = { 'Content-Type'  => mime,
              'Cache-Control' => 'public',
              'Expires'       => "#{(Time.now + 1000).utc}" }
  [200, headers, [body]]
end
do_read_all(context) click to toggle source

Get the a root directory listing @param [Hash] context the context for this request @return [Response] the listing, or status code

# File lib/app/repo.rb, line 64
def do_read_all(context)
  conf = context.closet.repos[context.config['repo']]
  mime = 'text/html'
  list = do_read_listing(conf, context.resource)
  if list == 404
    return 404
  end
  template = context.config['listing']
  list     = template.render(self,
                             list:     list,
                             resource: context.resource,
                             id:       '',
                             referrer: context.referer)
  headers  = { 'Content-Type'  => mime,
               'Cache-Control' => 'public',
               'Expires'       => "#{(Time.now + 1000).utc}" }
  [200, headers, [list]]
end
do_update(context) click to toggle source

Update the a single file @param [Hash] context the context for this request @return [Response] status code

# File lib/app/repo.rb, line 122
def do_update(context)
  conf    = context.closet.repos[context.config['repo']]
  repo    = context.resource
  content = context.json
  id      = context.id
  if content[:file]
    file = content[:file][:content].match(/base64,(.*)/)[1]
    file = Base64.decode64(file)
    if context.query[:type]
      mime = context.query[:type]
    else
      mime = content[:file][:mime]
    end
    do_update_file(conf, repo, id, file, content[:message], mime, context.user)
  else
    do_update_file(conf, repo, id, URI.unescape(content[:updated]), content[:message], context.query[:type], context.user)
  end
end
invoke(actions, context) click to toggle source

Proxy method used when routing @param [Array] actions the allowed actions for this URI @param [Hash] context the context for this request @return [Response] a Rack Response triplet, or status code

# File lib/app/repo.rb, line 145
def invoke(actions, context)
  return 404 unless context.resource
  case context.action
    when :create
      do_create(context)
    when :read
      if context.id
        do_read(context)
      else
        do_read_all(context)
      end
    when :update
      do_update(context)
    else
      403
  end
end