Skip to content

Instantly share code, notes, and snippets.

View scottmatthewman's full-sized avatar
🏳️‍🌈

Scott Matthewman scottmatthewman

🏳️‍🌈
View GitHub Profile
@scottmatthewman
scottmatthewman / SegmentedProgressView.swift
Created April 27, 2020 12:47
Building a custom progress view in SwiftUI
/// This code is designed to be copied and pasted into a Swift Playground.
/// It will work in Xcode (macOS only) or Swift Playgrounds (on iPad or macOS).
import SwiftUI
import PlaygroundSupport
struct SegmentedProgressView: View {
var value: Int
var maximum: Int = 7
var height: CGFloat = 10
var spacing: CGFloat = 2
@scottmatthewman
scottmatthewman / .rubocop.yml
Created October 7, 2019 09:31
My minimalist .rubocop.yml for new Rails projects
require:
# Ensure these gems are in your Gemfile
- rubocop-performance
- rubocop-rails
- rubocop-rspec
AllCops:
# Only exclude files that are auto-generated, and which could
# be regenerated at any time.
Exclude:
@scottmatthewman
scottmatthewman / AdaptsToSoftwareKeyboard.swift
Last active April 19, 2024 12:56
An example of using Combine to automatically adapt a SwiftUI scrollable view to accommodate an iOS onscreen keyboard
import SwiftUI
import Combine
struct AdaptsToSoftwareKeyboard: ViewModifier {
@State var currentHeight: CGFloat = 0
func body(content: Content) -> some View {
content
.padding(.bottom, currentHeight)
.edgesIgnoringSafeArea(.bottom)
@scottmatthewman
scottmatthewman / hooks.js
Last active February 25, 2019 17:25
An example of compositing useState and useEffect hooks
import { useState, useEffect } from 'react';
import xFetch from '../utils/x_fetch';
export const useFetch = (url) => {
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [data, setData] = useState([]);
useEffect(() => {
setIsLoading(true);
@scottmatthewman
scottmatthewman / test_has_many_autosave.rb
Last active August 29, 2015 14:19
Rails: has_many :through does not autosave when the parent is new
gem 'activerecord', '4.2.1'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
@scottmatthewman
scottmatthewman / between.swift
Last active August 29, 2015 14:02
A custom 'between' operator for Swift
operator infix >=< {}
@infix func >=< <T: Comparable> (lhs: T, rhs: (T, T)) -> T {
let (min, max) = rhs
var ret = lhs
ret = (ret < min) ? min : ret
ret = (ret > max) ? max : ret
return ret
}

We built sched.do for Yammer as an example open-sourced Rails application using the Yammer API. Like many services, Yammer API calls are subject to rate-limitations.

At times, the application would hit the Yammer API rate limit and encounter a 429 status code. To solve this, we leveraged delayed_job's functionality of retrying failed jobs. Our goal was to set the retry time to just outside the rate-limit time window.

To accomplish this, we overrode the reschedule_at and max_attempts methods, which delayed_job exposes for custom jobs. We also wanted to retry the job until success, so max_attempts was also overridden.

Code example:

module YammerRateLimited
@scottmatthewman
scottmatthewman / example_view.html.erb
Created December 13, 2011 10:58
Add a helper to aid prototyping using Placehold.it images
<%= placeholder_image(728, 90, name: 'IAB Leaderboard ad', background_color: 'ccc') %>
@scottmatthewman
scottmatthewman / url_validator.rb
Created September 20, 2011 15:04
Validating a URL in Rails 3.1 (and optionally updating it)
require 'net/http'
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
url = value
# Regex code by 'Arsenic' from http://snippets.dzone.com/posts/show/3654
if url =~ /^
( (https?):\/\/ )?
@scottmatthewman
scottmatthewman / Add post format to RSS entry title in WordPress 3.x
Created July 18, 2011 12:49
An example of how to tweak WordPress's RSS feed to prefix the title with the relevant post format ('Aside', 'Gallery', 'Video', etc.). Add to functions.php
function add_post_format_to_title_rss( $title )
{
$post_format = get_post_format();
if( false === $post_format )
return $title;
else {
$format_name = get_post_format_string($post_format);
return "{$format_name}: {$title}";
}
}