Organizing

This commit is contained in:
Gregory Eremin
2013-07-11 02:06:16 +07:00
parent e55a6af258
commit 23fee68790
14 changed files with 162 additions and 103 deletions
+28
View File
@@ -0,0 +1,28 @@
class Configuration
attr_reader :database_url,
:syntaxes_map,
:available_syntaxes
def initialize
load_database_config
load_syntax_config
end
def load_database_config
config = YAML.load_file(APP_ROOT.join('config', 'database.yml'))
@database_url = '%s://%s:%s@%s:%s/%s' % [
config['protocol'],
config['username'],
config['password'],
config['host'],
config['port'],
config['database']
]
end
def load_syntax_config
config = YAML.load_file(APP_ROOT.join('config', 'syntax.yml'))
@syntaxes_map = config
@available_syntaxes = config.keys
end
end
+50
View File
@@ -0,0 +1,50 @@
class Paste
attr_reader :contents, :type
class << self
def find(id)
record = DB[:pastes].where(id: id).first
record ? new(record[:contents], record[:type]) : nil
end
end
def initialize(contents, type = nil)
@contents = contents
@type = type if CONFIG.available_syntaxes.include?(type)
end
def save
encrypt!
DB[:pastes].insert(contents: contents, type: type)
end
def decrypt(key)
@key = key
decrypt!
self
end
def key
@key ||= SecureRandom.hex
end
def encrypt!
@contents = Base64.encode64(Encryptor.encrypt(value: contents, key: key))
end
def decrypt!
@contents = Encryptor.decrypt(value: Base64.decode64(contents), key: key)
end
def highlighted
Pygments.highlight(contents, lexer: type, options: { linenos: 'table' })
end
def paragraph
"<pre>#{contents}</pre>"
end
def html
type ? highlighted : paragraph
end
end