public
Last active

  • Download Gist
contact_signature.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# contacts_controller.rb
def share
@contact ||= Contact.find_by_signature(params[:contact_id] || params[:id])
end
 
# contact.rb
def self.find_by_signature(secret)
return nil unless secret.kind_of?(String)
id, signature = secret.split('-')
contact = self.find(id)
raise ActiveRecord::RecordNotFound if signature != self.sign(contact.id)
contact
end
 
def self.sign(id, length=8)
Digest::SHA1.hexdigest([self, id, SECRET].join)[0, length]
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.