1
0
Fork 0
fatkitten/app/paste.rb

51 lines
896 B
Ruby
Raw Normal View History

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 12:02:19 +00:00
@type = 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
Pygments.highlight(@contents, lexer: 'ruby', options: { linenos: 'table' })
end
def paragraph
"<pre>#{@contents}</pre>"
end
def html
type ? highlighted : paragraph
end
2013-07-09 07:53:49 +00:00
end