Skip to content

Instantly share code, notes, and snippets.

//
// KittenTest.swift
// KittenTest
//
// Created by Paul on 2015/9/13.
// Copyright © 2015 Kodama Software. All rights reserved.
//
import Foundation
@pcantrell
pcantrell / Gemfile
Created November 29, 2011 17:42
Get app profiling straight from the browser. Usage: (1) http://localhost:3000/rubyprof/start (2) Exercise slow requests (3) http://localhost:3000/rubyprof/stop (be patient) to get a report.
gem 'ruby-prof'
@pcantrell
pcantrell / Siesta+SwiftyJSON.swift
Created November 6, 2015 19:00
Siesta SwiftyJSON integration
extension TypedContentAccessors {
/**
Adds a `.json` convenience property to resources that returns a SwiftyJSON `JSON` wrapper.
If there is no data, then the property returns `JSON([:])`.
Note that by default, Siesta parses data based on content type. This accessor is only a way
of conveniently donwcasting and defaulting the data that Siesta has already parsed. (Parsing
happens off the main thread in a GCD queue, never in response one of these content accessors.)
To produce a custom data type that Siesta doesn’t already know how to parse as a Siesta
resource’s content, you’ll need to add a custom `ResponseTransformer`.
protocol Tensor {
static var numberOfDims: Int { get }
}
struct NestedTensor<Element: Tensor>: Tensor {
static var numberOfDims: Int {
return 1 + Element.numberOfDims
}
// ...etc...
@media (-webkit-min-device-pixel-ratio: 2) {
img {
image-rendering: -webkit-optimize-contrast;
image-rendering: optimize-contrast;
}
}
import Foundation
extension Array
{
func median() -> Element
{ return self[count / 2] }
}
func time(name: String, iters: Int, closure: Void -> Void) -> NSTimeInterval
{
class Recording < ActiveRecord::Base
has_many :artistic_contributions, dependent: :destroy, include: [:artistic_role, :artist]
has_many :artists, through: :artistic_contributions
has_many :editions, class_name: 'RecordingEdition', dependent: :destroy
has_many :blog_posts, foreign_key: :podcast_recording_id
validates :title, :base_file_name, presence: true
before_save :refresh_editions

Proposed changes to Siesta’s API before it hits the 1.0 freeze. Please post your feedback on this document to the accompanying Github issue.

Argument names

“Prefer to follow the language’s defaults for the presence of argument labels”

  • These changes seem good:

    • Entity.init(_:_:) → init(response:content:)
  • Error.init(_:_:_:userMessage:) → init(response:content:cause:userMessage:)

Siesta.enabledLogCategories = LogCategory.all
let service = Service(baseURL: myBaseURL)
service.configureTransformer("**") { (content: NSData, entity) -> NSXMLDocument? in
return try? NSXMLDocument(data: content, options: 0)
}
service.resource("foo.xml").load()
@pcantrell
pcantrell / puzzler.swift
Created August 7, 2016 16:57
Swift type erasure type puzzler
import Foundation
// ——— The Setup ———
protocol Foo
{
associatedtype Bar
func modify(bar: Bar) -> Bar
}