Skip to content

Instantly share code, notes, and snippets.

@ivan3bx
ivan3bx / gist:327614
Created March 10, 2010 07:11
Stubbing HTTP status code with OCMock
NSInteger result = 401;
const void *val = &result;
id mockResponse = [OCMockObject mockForClass:[NSHTTPURLResponse class]];
[[[mockResponse stub] andReturn:[NSValue value:val withObjCType:@encode(NSInteger)]] statusCode];
@ivan3bx
ivan3bx / gist:327641
Created March 10, 2010 07:33
Alternative to using OCMock for simple stubbing of NSHTTPURLResponse statusCode
/*
* Subclassing in this case wins over mocking
*/
@interface MockHTTPURLResponse : NSHTTPURLResponse {
NSInteger statusCode;
}
@property(readwrite,nonatomic) NSInteger statusCode;
@end
@ivan3bx
ivan3bx / loading_itunes_xml.m
Created September 7, 2013 04:35
just mucking around with iTunes XML data..
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *path = [NSString stringWithFormat:@"%@/Music/iTunes/iTunes Library.xml", NSHomeDirectory()];
NSData *xmlData;
NSDictionary *plist;
xmlData = [NSData dataWithContentsOfFile:path];
NSString *error;
NSPropertyListFormat format;
@ivan3bx
ivan3bx / ITLibraryExample.m
Created September 7, 2013 18:40
Example of the ITLibrary API
NSError *error = nil;
ITLibrary *library = [ITLibrary libraryWithAPIVersion:@"1.0" error:&error];
if (library)
{
NSArray *playlists = library.allPlaylists;
NSArray *tracks = library.allMediaItems;
NSLog(@"Playlists count: %lu", [playlists count]);
NSLog(@"Tracks count: %lu", [tracks count]);
@ivan3bx
ivan3bx / read_schema.m
Last active December 30, 2015 16:19
Get the current schema out of a SQLite database..
- (NSString *)schemaFor:(NSString *)dbFile
{
sqlite3 *db;
NSString *query = @"select name, sql from sqlite_master where type = 'table'";
NSString *result;
if (sqlite3_open([dbFile UTF8String], &db) == SQLITE_OK) {
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
@ivan3bx
ivan3bx / filter_by_artwork.m
Created January 27, 2014 07:09
Forced to use @autoreleasepool in order to avoid memory leak in MPMediaItemArtwork.
NSMutableArray *albumIDs = [[NSMutableArray alloc] initWithCapacity:1000];
for (MPMediaItemCollection *collection in items) {
@autoreleasepool {
MPMediaItem *props = collection.representativeItem;
NSString *albumTitle = [props valueForProperty:MPMediaItemPropertyAlbumTitle];
NSString *artistName = [props valueForProperty:MPMediaItemPropertyAlbumArtist];
MPMediaItemArtwork *artwork = [props valueForProperty:MPMediaItemPropertyArtwork];
@ivan3bx
ivan3bx / NSTextViewDelegate_textchange.m
Created April 15, 2014 05:36
Just a snippet of code for detecting a possible match. This seems inefficient for just single-character matches..
- (void)textDidChange:(NSNotification *)aNotification
{
NSTextView *textView = self.detailTextView;
NSString *contents = textView.textStorage.string;
NSRange selectionRange = textView.selectedRange;
if (selectionRange.length == 0 && contents.length > 0) {
NSString *possibleMatch = [contents substringWithRange:NSMakeRange(selectionRange.location - 1, 1)];
if ([possibleMatch isEqualToString:@"{"]) {
[textView complete:nil];
@ivan3bx
ivan3bx / detect_quote_header.rb
Created June 10, 2014 03:59
grabbing the last 200 characters preceeding a 'blockquote'
#
# Brute force method of grabbing 200 characters preceeding first <blockquote> element
#
p = Post.last
parts = p.split_as_html
root = Nokogiri::HTML.fragment(parts.first.raw_content)
bq = root.search('blockquote')[1]
test_string = root.inner_html[(root.inner_html.index(bq.to_html) - 200)...(root.inner_html.index(bq.to_html))]
# ">\n<br><br><div class=\"gmail_quote\">On Tue, Jun 29, 2014 at 11:19 AM, John Smith <span dir=\"ltr\">&lt;
@ivan3bx
ivan3bx / url_handler.swift
Created November 24, 2014 06:45
NXOAuth2 handler using AppleScript to open Safari
func createAuthHandler() -> NXOAuth2PreparedAuthorizationURLHandler {
return { (url: NSURL!) -> Void in
let sourceScript = "tell application \"Safari\"\n"
+ " open location \"\(url.absoluteString!)\"\n"
+ " activate\n"
+ "end tell\n"
var err : NSDictionary?
let script = NSAppleScript(source: sourceScript)!
if script.compileAndReturnError(&err) {
@ivan3bx
ivan3bx / ansi_color_output.swift
Created December 7, 2014 23:01
ANSI color sequence example in Swift (Unicode)
let escape = "\u{001B}["
let none = escape + "0m"
let red = escape + "0;31m"
let green = escape + "0;32m"
let yellow = escape + "0;33m"
println("\(red)Everything \(green)or \(yellow)nothing\(none).")