Skip to content

Instantly share code, notes, and snippets.

@mahemoff
mahemoff / document.rb
Created May 27, 2012 22:16
A simple HTTP-like document, with some headers on top and a body
class Document
attr_accessor :body, :headers
def initialize(text)
matches = text.match /^(?:([\s\S]+)\n\s*\n)?([\s\S]+)$/
self.body = matches[2]
self.headers = {}
headers_text = matches[1] || ''
headers_text.split(/\n/).map { |header_text|
@mahemoff
mahemoff / states.css
Created June 3, 2012 23:26
Classy HTML Overdrive (Using classes to determine visibility)
.is-left-aligned
.when-left-aligned
display: block
.when-right-aligned
display: none !important
.is-right-aligned
.when-left-aligned
display: none !important
.when-right-aligned
display: block
@mahemoff
mahemoff / README.md
Created June 7, 2012 10:33
subtract.rb

This is like a-b, where a and b are sets, but it effectively uses a case-insensitive comparison function.

The reason for this is that Rails/MySQL find_by is case-insensitive. So if you have a data feed where someone's changed only the case, but don't want that to actually affect anything, you can use this function to see if there's a real difference (beyond just flipping the case).

@mahemoff
mahemoff / twitvise.rb
Created June 15, 2012 15:25
Authetnicated Twitter calls using Devise
module Twitvise
def search
return unless twitter_client = client
twitter_client.search 'groo'
end
private
def client
@mahemoff
mahemoff / gist:3014921
Created June 29, 2012 00:25
Player FM Channels Snapshot
accounting agriculture american-football android animation apostolic apple arts aussie-rules auto aviation baptist baseball basketball biotechnology bodybuilding boxing breaking-bad business business-news careers catholic christian christian-orthodox cinema comedy comics cooking cricket current-affairs cycling daily-business-news daily-news daily-tech-news darts digital-marketing doctor-who ecommerce economics entrepeneur equestrian finance fishing fitness future-trends gadgets gambling game-dev game-development game-of-thrones gaming geekery golf harry-potter health health-care history hockey horseracing islamic java latest-news law lifestyle literature lost mad-men marathon mba media military-history mma mormon motorsports ms-dev musicality nascar non-profit oracle pentecostal personal-finances photography poetry poker presbyterian pro-wrestling productivity prog-languages programming publishing real-estate rugby running sales science security seo sherlock-holmes showbiz skeptic soccer social-media spiritua
@mahemoff
mahemoff / gist:3139852
Created July 19, 2012 00:10
jPlayer Gotcha
I couldn't get jPlayer to play on Firefox.
At first, I thought it must be the fact that Firefox needs Flash (for MP3s) and Chrome doesn't. So I was trying to check the SWF is loading and then the config.
Finally, I realised it was visibility of the player element. So yes, this might be something to do with Flash. But the point is, the player must be on displayed (not `display: none`). Which is tricky if you want the player to slide down...means you need to use various tricks instead of just a slideDown() or CSS animation.
Anyway, it plays now.
@mahemoff
mahemoff / gist:3151194
Created July 20, 2012 15:03
Feature Request for Screenflow: YouTube publishing
I often find myself uploading the same video several times to YouTube, because I have to re-do something after I see how YouTube has processed it.
Screenflow makes this harder work than necessary. It doesn't remember the form details, so I have to re-enter all the settings and content again...and some of them are mandatory.
And, the old video will still be there. I realise YouTube doesn't let publishers replace a video, but you could provide a checkbox (always defaulting to off) that would automatically delete the previously uploaded video. This way, I could do a quick edit in Screenflow, just hit Publish To YouTube, and done.
@mahemoff
mahemoff / gist:3173700
Created July 25, 2012 00:56
split_lines.rb
# A word splitting algorithm optimised for laying out arbitrary text into a fixed-width, variable-height, space.
#
# Splits the string into lines, where each line is smaller than some MAX characters (related to the width of the space).
# If the first word is longer than MAX, it is *not* split, but becomes the entire line.
# The algorithm isn't (yet) smart enough to try balancing the length of each line. To compensate for this, it tries to avoid
# the naieve situation where the bottom line ends up being a few remainder characters, simply by reversing the order it
# processes the string. So if anything's likely to be unbalanced, it's the top line that will be small instead of the bottom line.
# walks through the string in reverse so as to end up with a longer string at the end.
#
# Caveat: Needs more testing
@mahemoff
mahemoff / gist:3332033
Created August 12, 2012 14:18
tire search
# search by tag
tire.search(per_page: params[:per_page]||20) {
query do
boolean do
must { term 'keywords', keyword} if keyword.present?
must { term 'tags', tag} if tag.present?
must { string q } if q.present?
end
end
highlight 'title', 'description'
@mahemoff
mahemoff / ios.c
Created August 16, 2012 22:17
iOS example
// Async web fetch with JSON parse
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"url"]]
queue:nil completionHandler:^(NSURLResponse *r, NSData *d, NSError *e) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:d
options:0
error:nil];
}];
// Using the camera