Skip to content

Instantly share code, notes, and snippets.

@andykirk
andykirk / truncate_html.php
Last active October 8, 2019 10:00
PHP Truncate HTML Function
/**
* truncate_html()
*
* Truncates a HTML string to a given length of _visisble_ (content) characters.
* E.g.
* "This is some <b>bold</b> text" has a visible/content length of 22 characters,
* though the total string length is 29 characters.
* This function allows you to limit the visible/content length whilst preserving any HTML formatting.
*
* @param string $html
@shurup14
shurup14 / mimes.json
Last active March 19, 2020 21:10
Mimes types
{
"text/vnd.wap.wmlscript": "Wireless Markup Language Script (WMLScript)",
"application/cu-seeme": "CU-SeeMe",
"application/vnd.mobius.plc": "Mobius Management Systems - Policy Definition Language File",
"application/vnd.dna": "New Moon Liftoff/DNA",
"application/mathml+xml": "Mathematical Markup Language",
"image/x-cmx": "Corel Metafile Exchange (CMX)",
"application/vnd.oasis.opendocument.text": "OpenDocument Text",
"application/vnd.ezpix-album": "EZPix Secure Photo Album",
"application/xslt+xml": "XML Transformations",
@davidjrice
davidjrice / redcarpet.rb
Created June 29, 2012 00:34
Rails 3.2 Markdown Template Handler
# config/initializers/redcarpet.rb
module ActionView
module Template::Handlers
class Markdown
class_attribute :default_format
self.default_format = Mime::HTML
def call(template)
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
markdown.render(template.source).html_safe.inspect
@botimer
botimer / yesno.rb
Created June 7, 2012 19:56
Handy yes/no prompt for little ruby scripts
# This is a reasonably well-behaved helper for command-line scripts needing to ask a simple yes/no question.
# It optionally accepts a prompt and a default answer that will be returned on enter keypress.
# It keeps asking and echoes the answer on the same line until it gets y/n/Y/N or enter.
# I tried to get Highline to behave like this directly, but even though it's sophisticated, I didn't like the result.
# This isn't especially elegant, but it is straightforward and gets the job done.
require 'highline/import'
def yesno(prompt = 'Continue?', default = true)
a = ''
s = default ? '[Y/n]' : '[y/N]'
@vajradog
vajradog / auto_expiration.md
Last active April 5, 2021 22:33
Set auto expiration for rails models

##Set auto expiration for rails models.

This is a simple view logic that moves objects based on a predetermined date.

Let's say we have a bunch of events(or posts) on our index page and we want to sort out which of these are still active and which are not (based on the time).

We want the active ones to be clickable and prominent, we want the non-active ones to be greyed-out or even hidden. So how do we do this?

Rails provide a simple yet powerful method: future?()

@equivalent
equivalent / gist:b492f6779e99ee9defb2
Created March 23, 2016 23:54
Ruby AES Encryption using OpenSSL
#!/usr/bin/env ruby
require "openssl"
require 'digest/sha2'
require 'base64'
# We use the AES 256 bit cipher-block chaining symetric encryption
alg = "AES-256-CBC"
# We want a 256 bit key symetric key based on some passphrase
digest = Digest::SHA256.new
@frane
frane / ArrayObjectDemo.coffee
Created December 31, 2011 02:19
Traversing arrays and objects in CoffeeScript
# Traversing arrays and objects in CoffeeScript
# The array and object we use for testing
arr = [1, 2, 3, 4, 5]
obj = {a: 1, b: 2, c: 3, d: 4, e: 5}
# 'in' has a different meaning in CoffeeScript than in JavaScript
# CS: element in array -> JS: array.indexOf(element) >= 0
console.log '5 in arr: ' + (5 in arr)
@jcasimir
jcasimir / friendly_urls.markdown
Created September 11, 2011 15:48
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@leonklingele
leonklingele / client.sh
Last active July 19, 2023 20:35
netcat – encrypt transfer with openssl
IP="127.0.0.1"
PORT="8877"
SHARED_SECRET="shared secret"
OPENSSL="/usr/local/opt/libressl/bin/openssl"
OPENSSL_CMD="$OPENSSL enc -a -A -aes-256-gcm"
while IFS= read -r MSG; do
echo "$MSG" | $OPENSSL_CMD -e -k "$SHARED_SECRET"
echo
@db0sch
db0sch / regenerate_credentials.md
Last active October 31, 2023 20:30
How to regenerate the master key for Rails 5.2 credentials

If your master.key has been compromised, you might want to regenerate it.

No key regeneration feature at the moment. We have to do it manually.

  1. Copy content of original credentials rails credentials:show somewhere temporarily.
  2. Remove config/master.key and config/credentials.yml.enc
  3. Run EDITOR=vim rails credentials:edit in the terminal: This command will create a new master.key and credentials.yml.enc if they do not exist.
  4. Paste the original credentials you copied (step 1) in the new credentials file (and save + quit vim)
  5. Add and Commit the file config/credentials.yml.enc