2013-07-09 08:04:03 +00:00
|
|
|
class Paste
|
2013-07-10 12:02:19 +00:00
|
|
|
attr_reader :contents, :type
|
2013-07-09 08:04:03 +00:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def find(id)
|
|
|
|
record = DB[:pastes].where(id: id).first
|
2013-07-10 12:02:19 +00:00
|
|
|
record ? new(record[:contents], record[:type]) : nil
|
2013-07-09 08:04:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-10 12:02:19 +00:00
|
|
|
def initialize(contents, type = nil)
|
2013-07-09 08:04:03 +00:00
|
|
|
@contents = contents
|
2013-07-10 19:06:16 +00:00
|
|
|
@type = type if CONFIG.available_syntaxes.include?(type)
|
2013-07-09 07:53:49 +00:00
|
|
|
end
|
|
|
|
|
2013-07-09 08:04:03 +00:00
|
|
|
def save
|
|
|
|
encrypt!
|
2013-07-10 12:02:19 +00:00
|
|
|
DB[:pastes].insert(contents: contents, type: type)
|
2013-07-09 08:04:03 +00:00
|
|
|
end
|
2013-07-09 07:53:49 +00:00
|
|
|
|
2013-07-09 08:04:03 +00:00
|
|
|
def decrypt(key)
|
|
|
|
@key = key
|
|
|
|
decrypt!
|
|
|
|
self
|
2013-07-09 07:53:49 +00:00
|
|
|
end
|
|
|
|
|
2013-07-09 08:04:03 +00:00
|
|
|
def key
|
|
|
|
@key ||= SecureRandom.hex
|
|
|
|
end
|
2013-07-09 07:53:49 +00:00
|
|
|
|
2013-07-09 08:04:03 +00:00
|
|
|
def encrypt!
|
|
|
|
@contents = Base64.encode64(Encryptor.encrypt(value: contents, key: key))
|
2013-07-09 07:53:49 +00:00
|
|
|
end
|
|
|
|
|
2013-07-09 08:04:03 +00:00
|
|
|
def decrypt!
|
|
|
|
@contents = Encryptor.decrypt(value: Base64.decode64(contents), key: key)
|
|
|
|
end
|
2013-07-10 12:02:19 +00:00
|
|
|
|
|
|
|
def highlighted
|
2013-07-10 18:20:10 +00:00
|
|
|
Pygments.highlight(contents, lexer: type, options: { linenos: 'table' })
|
2013-07-10 12:02:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def paragraph
|
2013-07-10 18:20:10 +00:00
|
|
|
"<pre>#{contents}</pre>"
|
2013-07-10 12:02:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def html
|
|
|
|
type ? highlighted : paragraph
|
|
|
|
end
|
2013-07-09 07:53:49 +00:00
|
|
|
end
|