Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael de Silva bsodmike

View GitHub Profile
@bsodmike
bsodmike / Gemfile
Created Feb 23, 2021
RSpec example using Webmock to stub and API request
View Gemfile
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "faraday"
gem "json"
group :test do
@bsodmike
bsodmike / rust-resources.md
Last active May 22, 2020
Rust resources and code snippets to improve writing idiomatic Rust.
View rust-resources.md

Why Rust?

  • Memory safe. I like to think of it as being given a dull-rusty blade rather than a sharp finely crafted japanese katana/wakizashi. Now go run about and see which cuts you faster!
  • Support for Futures (aka Promises in JS parlance).
  • Threadsafe concurrency features: Arc vs. Mutex<T> etc.
  • Generics and Zero-cost abstractions
  • Runs on anything with a CPU. Even Android.
  • Supports full WASM (WebAssembly). Here's a demo.
  • Fabulous tooling via Cargo.
  • Much more covered by this great talk by Jon Gjengset (@jonhoo) https://www.youtube.com/watch?v=DnT-LUQgc7s
View rust-threads.rs
let delay = time::Duration::from_millis(1);
let now = time::Instant::now();
let thread_delay = Arc::new(1);
let mut done = false;
while !done {
let thread_delay = Arc::clone(&thread_delay);
let handle = thread::spawn(move || {
View rust-code-layout.txt
.
├── Cargo.lock
├── Cargo.toml
├── conf
│   ├── development
│   │   └── config.yml
│   └── production
│   └── config.yml
├── src
│   ├── errors.rs
@bsodmike
bsodmike / rust-mysql.rs
Created May 4, 2020
Fetching fields with Mysql crate in Rust
View rust-mysql.rs
#[macro_use]
use crate::mysql::*;
use crate::mysql::prelude::*;
use crate::errors::Error;
#[derive(Debug)]
pub struct ConnectorMysql {
}
@bsodmike
bsodmike / easter_egg.rb
Created Jan 29, 2020
CircleCI email easter egg
View easter_egg.rb
input = %w(01101000 01101111 01110111 00100000 01110100 01101111 00100000 01100100 01100101 01100011 01100101 01101110 01110100 01110010 01100001 01101100 01101001 01111010 01100101 00100000 01110100 01110111 01101001 01110100 01110100 01100101 01110010)
n = input.map { |e| e.to_i(2).chr }
n = n.join
puts n
# =>
# how to decentralize twitter
@bsodmike
bsodmike / post.py
Last active Jan 15, 2019
Python example to POST some JSON
View post.py
import requests
import json
url = 'http://localhost:2368/ghost/api/v0.1/posts'
payload = {
"posts": [{ "markdown": "<p>Some content goes here</p>", "title": "New", "status": "published"}]
}
headers = {
"Authorization": "Bearer 7CVoMaf68zd6jeWo9oz4syKvTyizrdDf5Uyv7vEAJHHwU8ceKoRcXw4tLw0V6m4z7MI3uXc4aI8iPFkPaTRGWXrKt4myAMJsK5YgWdDy8N26MqwNNxq2XC0bKOooU1hgyCJpfNecnBztRyXkFZUZUqbRFbkt4kBBYDhD6O7hJ7qzpSwjeX4E0JEvEU3R4ye"
@bsodmike
bsodmike / zfs-delete-snapshots.sh
Created May 20, 2018
FreeNAS ZFS delete snapshots retaining the last 'N' only.
View zfs-delete-snapshots.sh
#!/bin/bash
# Retain last 10 snapshots only.
set -e
zfs list -t snapshot -o name -S creation | grep ^big/media@auto | tail -n +10 | xargs -n 1 zfs destroy -vr