Skip to content

Instantly share code, notes, and snippets.

View dtorres's full-sized avatar

Diego Torres dtorres

View GitHub Profile
@dtorres
dtorres / Retina goodie.js
Created October 7, 2012 16:30
This gist allows you to serve proper images according to the resolution of the window.
/* Server side check if there is a cookie that defines
devicePixelRatio, if not, serve this javascript */
//Does it support it? no, default to 1;
if (window.devicePixelRatio === undefined) {
window.devicePixelRatio = 1;
}
//Setup cookie date.
var now = new Date();
@dtorres
dtorres / NSObject+NoSubscription.h
Created June 1, 2013 07:53
Disallow object subscripting in your project. NOTE: Do not include in Final Version of your project
//
// NSObject+NoSubscription.h
//
// Created by Diego Torres on 6/1/13.
// Copyright (c) 2013 Onda. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject (NoSubscription)
#import "UIViewController+designatedInitializerOverride.h"
@class Model;
@interface ModelViewController : UITableViewController
- (instancetype)initWithModel:(Model *)model NS_DESIGNATED_INITIALIZER;
DTTableViewControllerDesignatedInitializerOverrideHeader
@end

It wasn´t hard. We needed to reimplement/copy a few things that were taken for granted in AFNet such as user agent, accept and other headers. Those were easy to do with NSURLSessionConfiguration.

In terms of request and url construction they are created by each model doing the request, it is a bit more of code and maybe duplicated but explicit as there is no implicit baseURL. The query/body is also constructed in place being explicit and not having to worry if the serializer is JSON or form data. Also it has the advantage of performance since the request serializer of AFNetworking creates mutable copies the request around a lot to do checks in methods that take immutable ones.

Also I created this category for convenience

@interface NSMutableURLRequest (JSONBody)

- (void)setJSONBody:(id)body;
- (BOOL)setJSONBody:(id)body options:(NSJSONWritingOptions)options error:(NSError **)error;
@dtorres
dtorres / NSURLBackgroundSessionBits.m
Created November 2, 2015 21:18
Storing Info in background requests.
/*
Basically what we are going to do is use and profit from NSURLRequests being conformant to NSCoding,
and a little known API from NSURLProtocol which allows us attach info to requests.
*/
//Step 0: For the purpose of this gist, we'll already have a background session setup and assume a bunch of stuff.
NSURLSession *bgSession = [NSURLSession magicMethodWhichGivesMeTheAlreadySetupSession]; //Geeez, Methods are long in Obj-C.
//IMPORTANT: Request must be mutable in order for this to work. Got an immutable one. Make a copy. Can't? Well, Make it so!.
//Step 1: Setup your basic request.
protocol reuseIdentifierClass {
static var reuseIdentifier: String { get }
}
extension UICollectionView {
func registerClass<T: UICollectionViewCell where T: reuseIdentifierClass>(aClass: T.Type) {
registerClass(aClass, forCellWithReuseIdentifier: aClass.reuseIdentifier)
}
func dequeueReusableCellWithClass<T: UICollectionViewCell where T: reuseIdentifierClass>(aClass: T.Type, forIndexPath indexPath: NSIndexPath) -> T {

#The GIF is dead

Nowadays when we use the word GIF, hardly it refers to the file format, rather we talk about a really short motion picture (often loopable) without sound.
We hardly think of the actual file format used to display these (unless it takes too long to load, then it surely is a G.I.F.)

As such I think we can at last please Steve Wilhite and give him the pronunciation he always wanted by introducing this separation of concepts apt for today:

G.I.F.: Graphics Interchangeable Format. This file format was originally preferred to deliver animated short movies on the internet (Pronounced with a J)
GIF: Short (often loopable) motion picture without sound (This one is pronounced with a hard G)

Just came across a weird issue where my app would be handed production push tokens while all the metadata of the compiled app indicated it was in a development environment.
Compiling said app with Xcode 7 (no code nor project changes) makes everything work as expected

@dtorres
dtorres / ApproximateConversion.swift
Last active May 17, 2020 10:08 — forked from ekurutepe/ApproximateConversion.swift
Conversions in Measurement too accurate for you? Here take this…
import Foundation
extension Measurement where UnitType: UnitLength {
func approximatelyConverted(to otherUnit: UnitType) -> Measurement<UnitType> {
switch (unit, otherUnit) {
case (UnitLength.meters, UnitLength.feet):
return Measurement(value: value * 3, unit: otherUnit)
case (UnitLength.feet, UnitLength.meters):
return Measurement(value: value * 3 / 10, unit: otherUnit)
default:
@dtorres
dtorres / gcl-lunchMoney-bridge.mjs
Created April 10, 2024 16:56
JS Script to import transactions via GoCardless API
import { createHash } from 'crypto';
import fs from 'fs/promises';
import axios from 'axios';
import { exit } from 'process';
// Get these from http://bankaccountdata.gocardless.com
let gclSecretId = "GoCardlessSecretID"
let gclSecretKey = "GoCardlessAccountKey"