1
0
Fork 0
fatkitten/app/models/paste.rb

55 lines
1.0 KiB
Ruby

class Paste
attr_reader :contents, :syntax
class << self
def find(id)
record = DB[:pastes].where(id: id).first
record ? new(record[:contents], record[:syntax]) : nil
end
end
def initialize(contents, syntax = nil)
@contents = contents
@syntax = syntax if CONFIG.available_syntaxes.include?(syntax)
end
def save
encrypt!
DB[:pastes].insert(contents: contents, syntax: syntax)
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: syntax, options: { linenos: 'table' })
end
def parsed_for_links
contents.gsub(/(https?:\/\/[\S]+)/, '<a target="_blank" href="\0">\0</a>')
end
def paragraph
"<pre>#{parsed_for_links}</pre>"
end
def html
syntax ? highlighted : paragraph
end
end