Sha256: 633c1fd0fb46710983832f08fa97d501b50a4ff9b99e752b08cc17bb27c748b4

Contents?: true

Size: 1.67 KB

Versions: 5

Compression:

Stored size: 1.67 KB

Contents

require "sinatra"
require "rcredstash"

module SecretConsole
  class App < Sinatra::Base
    DELIMITER = '.'

    set :views, File.expand_path('../views', __FILE__)
    enable :method_override

    Record = Struct.new(:path, :value)

    get '/*' do
      path = params['splat'].first.gsub("/", DELIMITER)

      keys = CredStash.list.map { |k, v| k }

      if keys.include?(path)
        return haml :show, locals: { value: CredStash.get(path), path: request.path_info }
      end

      unless path == ""
        keys = keys.grep(Regexp.new("\\A#{path}\\#{DELIMITER}#{DELIMITER}*"))
      end

      if keys.size == 0
        return haml :new, locals: { path: request.path_info }
      end

      values = {}
      links = {}

      keys.each do |key|
        child_path = key.gsub(path, '')
        child_path = child_path[1..-1] if child_path.start_with?(DELIMITER)
        childs = child_path.split(DELIMITER)

        if childs.size == 1
          link = (request.path_info.split('/') + [childs.first]).join('/')
          values[childs.first] = Record.new(link, CredStash.get(key))
        else
          link = (request.path_info.split('/') + [childs.first]).join('/')
          links[childs.first] = link
        end
      end

      haml :index, locals: { values: values, links: links }
    end

    post '/*' do
      path = params['splat'].first.gsub("/", DELIMITER)
      value = params['value']


      CredStash.put(path, value)

      # TODO Set flash message
      redirect to(request.path_info)
    end

    delete '/*' do
      path = params['splat'].first.gsub("/", DELIMITER)

      CredStash.delete(path)

      redirect to(request.path_info.split("/")[0..-2].join("/"))
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
secret_console-0.2.3 lib/secret_console/app.rb
secret_console-0.2.2 lib/secret_console/app.rb
secret_console-0.2.1 lib/secret_console/app.rb
secret_console-0.2.0 lib/secret_console/app.rb
secret_console-0.1.0 lib/secret_console/app.rb