Skip to content

Instantly share code, notes, and snippets.

@phawk
phawk / NSImage+QuickLook.swift
Created Feb 15, 2018
Swift macOS generate image previews of files on disk
View NSImage+QuickLook.swift
import AppKit
import QuickLook
extension NSImage {
static func previewForFile(path fileURL: URL, ofSize size: CGSize, asIcon: Bool) -> NSImage? {
let dict = [
kQLThumbnailOptionIconModeKey: NSNumber(booleanLiteral: asIcon)
] as CFDictionary
@phawk
phawk / getToken.js
Created Aug 23, 2019
Shopify exchange OAuth code for token and create user account
View getToken.js
const crypto = require("crypto")
const querystring = require("querystring")
const fetch = require("isomorphic-fetch")
const Account = require("./models/account")
exports.handler = async (event, context) => {
const { shop, hmac, code, timestamp } = event.queryStringParameters
const apiKey = process.env.SHOPIFY_API_KEY
const apiSecret = process.env.SHOPIFY_API_SECRET
@phawk
phawk / .screenrc
Last active Jan 22, 2021
Sample screenrc
View .screenrc
# Save this in ~/.screenrc
# Use bash
shell /bin/bash
autodetach on
# Big scrollback
defscrollback 5000
@phawk
phawk / install.js
Created Aug 23, 2019
Shopify install lambda
View install.js
exports.handler = async (event, context) => {
const shop = event.queryStringParameters.shop
const redirectUri = event.queryStringParameters.redirect
const apiKey = process.env.SHOPIFY_API_KEY
const scopes="read_content,write_content,read_products,read_themes,write_themes"
if (shop) {
const installUrl = 'https://' + shop +
'/admin/oauth/authorize?client_id=' + apiKey +
'&scope=' + scopes +
@phawk
phawk / payhere_embed_sdk.html
Last active Dec 29, 2020
Payhere embed SDK example
View payhere_embed_sdk.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Embed Test Site</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
.container {
margin: 4rem auto;
@phawk
phawk / graphql.js
Created Aug 23, 2019
Lambda Shopify graphql proxy endpoint
View graphql.js
const https = require("https")
const fetch = require("isomorphic-fetch")
const Account = require("./models/account")
const authenticated = require("./lib/auth")
exports.handler = authenticated(async (event, context) => {
const { id: shopId, shopifyToken } = context.account
try {
const resp = await fetch(`https://${shopId}/admin/api/2019-07/graphql.json`, {
@phawk
phawk / authentication.js
Created Aug 23, 2019
Lambda HoC for authenticating requests with dynamoDB
View authentication.js
const Account = require("../models/account")
module.exports = (handler) => async (event, context) => {
let auth = event.headers.authorization
auth = auth.replace(/^Basic\s/, "")
auth = Buffer.from(auth, 'base64').toString()
const [id, token] = auth.split(":")
const account = await Account.get({ id })
@phawk
phawk / PHP Recursive Closures.php
Created Jan 14, 2012
A quick example of a recursive closure in PHP, passing itself into the scope by reference.
View PHP Recursive Closures.php
<?php
$closure = function ($params) use (&amp;$closure) {
// ...
$closure($params);
}
// Start the magics
$closure($params);
@phawk
phawk / payhere_webhooks_controller.rb
Created Feb 20, 2019
Benmudge.com PayHere Webhooks integration
View payhere_webhooks_controller.rb
module Events
class PayhereWebhooksController < ApplicationController
skip_before_action :authenticate_user!
skip_before_action :verify_authenticity_token
def create
return head 401 unless valid_signature?
if payment_params[:status] == "success"
user = find_or_create_user
View payhere_embed_demo.html
<button data-payhere-embed="https://payhere.co/altlabs/membership/pro-plan">Subscribe now</button>
<script src="https://payhere.co/embed/embed.js"></script>