Skip to content

Instantly share code, notes, and snippets.

View perlmunger's full-sized avatar

Matt Long perlmunger

View GitHub Profile
@perlmunger
perlmunger / Regex.swift
Created June 18, 2024 20:59
Property to Convert String to Shipper URL if Tracking Number Matches Regex
import UIKit
infix operator =~
func =~ (value : String, pattern : String) -> RegexMatchResult {
var err : NSError?
let nsstr = value as NSString
let options = NSRegularExpression.Options(rawValue: 0)
let re: NSRegularExpression?
do {
@perlmunger
perlmunger / azure_blob_service.rb
Last active October 28, 2023 19:17
Azure Blob REST API in Ruby
require 'faraday'
require 'base64'
require 'openssl'
require 'digest'
class AzureBlobService
CONNECTION_STRING = Rails.application.credentials.azure_storage_connection_string
STORAGE_ACCOUNT = CONNECTION_STRING.match(/AccountName=(.*?);/)[1]
ACCESS_KEY = CONNECTION_STRING.match(/AccountKey=(.*?);/)[1]
@perlmunger
perlmunger / logMilestone.swift
Created January 20, 2021 17:11 — forked from atomicbird/logMilestone.swift
Sometimes you just want to print a message that tells you a line of code was executed. Inspired by a tweet from Paige Sun: https://twitter.com/_PaigeSun/status/1161132108875796480
/// Log the current filename and function, with an optional extra message. Call this with no arguments to simply print the current file and function. Log messages will include an Emoji selected from a list in the function, based on the hash of the filename, to make it easier to see which file a message comes from.
/// - Parameter message: Optional message to include
/// - file: Don't use; Swift will fill in the file name
/// - function: Don't use, Swift will fill in the function name
/// - line: Don't use, Swift will fill in the line number
func logMilestone(_ message: String? = nil, file: String = #file, function: String = #function, line: Int = #line) -> Void {
#if DEBUG
// Feel free to change the list of Emojis, but don't make it shorter, because a longer list is better.
let logEmojis = ["😀","😎","😱","😈","👺","👽","👾","🤖","🎃","👍","👁","🧠","🎒","🧤","🐶","🐱","🐭","🐹","🦊","🐻","🐨","🐵","🦄","🦋","🌈","🔥","💥","⭐️","🍉","🥝","🌽","🍔","🍿","🎹","🎁","❤️","🧡","💛","💚","💙","💜","🔔"]
let logEmoji = logEmojis[abs(
@perlmunger
perlmunger / SecCertificateWrapper.swift
Last active August 20, 2024 14:39
A wrapper around SecCertificate to simplify accessing iOS app certificate properties found in the embedded.mobileprovision file inside the bundle payload. The WrapperImplementation file shows how to extract both the provisioning profile and the details of each certificate. Works on iOS 12, iOS 13, and newer
struct SecCertificateWrapper : Comparable {
var data:Data
var cert:SecCertificate
// Initialize with a data object from the "DeveloperCertificates"
// array (see WrapperImplementation.swift)
init(data:Data) {
self.cert = SecCertificateCreateWithData(nil, data as CFData)!
// Use this later for parsing the date details from the cert
@perlmunger
perlmunger / profname.rb
Last active September 13, 2023 13:32
Display iOS Provisioning Profile (.mobileprovision) Details With Command Line Ruby Utility
#!/usr/bin/env ruby
# -----------------------------------------------------
# Usage: profname.rb /path/to/profiles/directory
# If no arguments are passed in, the default provisioning profiles directory
# on macOS is used: ~/Library/MobileDevice/Provisioning Profiles
#
# Script expects a directory, not a single file. Use grep on the command
# line to locate specific details.
# -----------------------------------------------------
// ISO 8601 Extension for Swift to send dates to Ruby on Rails
extension Date {
init(dateString:String) {
self = Date.iso8601Formatter.date(from: dateString)!
}
static let iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate,
.withTime,
@perlmunger
perlmunger / NumericSortedStringArray.swift
Created January 18, 2017 03:15
Swift sort strings numerically array extension
extension Sequence where Iterator.Element == String {
var sortedByNumberAndString : [String] {
return self.sorted { (s1, s2) -> Bool in
return s1.compare(s2, options: .numeric) == .orderedAscending
}
}
}
let sorted = ["8 Bob", "7 Joe", "11 Jimmy", "9 Larry", "1 Kyle"].sortedByNumberAndString
@perlmunger
perlmunger / Collections+Additions.swift
Last active April 27, 2016 15:27
Show how to add a mutating function for RangeReplaceableCollectionType that appends items only if they are not already in the collection.
extension RangeReplaceableCollectionType where Generator.Element : Equatable {
mutating func appendDistinct(object : Generator.Element) {
if !self.contains(object) {
self.append(object)
}
}
}
// A derivative solution using filter instead of contains
extension RangeReplaceableCollectionType where Generator.Element : Equatable {
// ImagePicker encapsulates UIImagePickerViewController functioality providing a convenient
// closure interface for responding to user interactions
import UIKit
import MobileCoreServices
class ImagePicker : NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var didFinishPickingMediaWithInfo:((info:[String:AnyObject]) -> ())?
var didCancelPickingMedia:(() -> ())?
let imagePicker = UIImagePickerController()
@perlmunger
perlmunger / String+Additions.swift
Last active February 22, 2017 01:06
Some helpful string extensions in Swift 2
extension String
{
var length: Int {
get {
return self.characters.count
}
}
func contains(s: String) -> Bool {
return self.rangeOfString(s) != nil ? true : false