Class: Smeagol::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/smeagol/app.rb

Overview

Sinatra based app for serving the the site directly from the Gollum wiki repo.

Instance Method Summary collapse

Instance Method Details

#get_mime_type(file) ⇒ String (private)

Retrieves the mime type for a filename based on its extension.

Parameters:

  • file

    The filename. [String]

Returns:

  • (String)

    Returns the mime type for a file.



150
151
152
153
154
155
156
157
# File 'lib/smeagol/app.rb', line 150

def get_mime_type(file)
  unless file.nil?
    extension = ::File.extname(file)
    return Rack::Mime::MIME_TYPES[extension] || 'text/plain'
  end
  
  return 'text/plain'
end

#mount_pathString (private)

Determines the mounted path to prefix to internal links.

Returns:

  • (String)

    Returns the mount path.



182
183
184
185
186
# File 'lib/smeagol/app.rb', line 182

def mount_path
  path = settings.mount_path
  path += '/' unless path.end_with?('/')
  path
end

#parse_params(params) ⇒ String (private)

If the path starts with a version identifier, use it.

Parameters:

  • params

    The request parameters. [Hash]

Returns:

  • (String)

    Returns the version number.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/smeagol/app.rb', line 121

def parse_params(params)
  name     = params[:splat].first
  version  = 'master'
  tag_name = nil

  if name.index(/^v\d/)
    repo = Grit::Repo.new(repository.path)
    tag_name = name.split('/').first
    repo_tag = repo.tags.find do |tag|
      tag_name == tag.name or tag_name == "v#{tag.name}"
    end
    if repo_tag
      version = repo_tag.name #repo_tag.commit.id
      name = name.split('/')[1..-1].join('/')
    else
      # TODO: page not found
    end
  end

  return name, version, tag_name
end

#repositoryRepository (private)

Determines the repository to use based on the hostname.

Returns:

  • (Repository)

    Returns the matching repository.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/smeagol/app.rb', line 164

def repository
  # Match on hostname
  settings.repositories.each do |repository|
    next if repository.cname.nil?
    if repository.cname.upcase == request.host.upcase
      return repository
    end
  end

  # If no match, use the first repository as the default.
  settings.repositories.first
end

#sanitize_path(path) ⇒ String (private)

Removes all references to parent directories (../) in a path.

Parameters:

  • path

    The path to sanitize. [String]

Returns:

  • (String)

    Returns a clean, pristine path.



195
196
197
# File 'lib/smeagol/app.rb', line 195

def sanitize_path(path)
  path.gsub(/\.\.(?=$|\/)/, '') unless path.nil?
end