Skip to content

Instantly share code, notes, and snippets.

View chrisroos's full-sized avatar

Chris Roos chrisroos

View GitHub Profile
@chrisroos
chrisroos / README.md
Last active November 11, 2018 15:50
Bookmarklet that allows me to add a link to more information for specific emails I receive

I receive a number of periodic emails that prompt me to do something (e.g. take an electricity meter reading). I wanted to be able to add information to those emails to remind me how to perform the task.

This JavaScript allows me to configure rules so that I can display a link to a web page when I'm viewing an email with a specific subject in Gmail.

I'm currently hosting the JavaScript and installer web page using rawgit.com.

Visit this page to install the bookmarklet.

@chrisroos
chrisroos / README.md
Last active November 8, 2018 19:24
Exploring Todoist's API

The todoist.rb script uses their API to print a list of all my completed tasks in CSV format.

The standard Todoist backup doesn't contain completed tasks.

It looks as though using Release Phase commands (available as of 2 June 2016) is the preferred way of running things like migrations after the slug has been compiled and deployed.

The Buildpack API explicitly suggests avoiding side effects during the build:

Buildpack developers should avoid generating side effects during the build. For example, it is not recommended to perform database migrations in the compile script.

The following example creates a Rails app with a single migration and demonstrates that the migration is run after deployment.

# Create empty Rails app
# These were the IDs returned by the identify-affected-attachments.rb script when it was run in staging on 20 Mar 2018.
#
[3625, 6002, 6007, 7874, 7875, 12248, 12249, 16333, 16381, 16383, 27263, 27607, 28153, 28154, 48876, 65687, 69606, 97881, 118296, 137087, 137587, 151994, 151995, 151996, 152690, 156319, 163466, 163467, 163468, 163469, 163470, 163471, 167849, 167851, 172237, 172238, 172239, 176752, 176753, 179306, 180955, 182610, 182636, 184031, 190436, 190437, 190438, 190439, 198104, 198144, 203773, 203952, 206956, 210410, 210933, 211644, 211646, 212271, 222544, 222545, 226017, 226119, 230027, 232152, 245882, 246519, 249795, 249796, 251493, 255473, 256038, 256798, 257471, 259192, 259868, 260418, 260904, 263013, 263014, 263359, 266717, 266834, 268678, 268679, 268680, 268681, 268682, 268683, 268684, 268685, 268686, 268687, 270767, 271065, 273957, 273959, 276018, 276347,276696, 276698, 278305, 279330, 281943, 284025, 284200, 284202, 284203, 284204, 284205, 284208, 284962, 286668, 286670, 287941, 288107, 2881
# See https://github.com/alphagov/asset-manager/issues/525 for more information
ids = [1, 2, 3, 4, 5, 6, 7, 11, 17, 33, 44, 47, 79, 80, 82, 88, 94, 98, 99, 101, 106, 108, 135, 136, 137, 138, 139, 140, 145, 146, 153, 154, 160, 168, 182, 187, 231, 350, 374, 378, 379, 382, 389, 413, 437, 636, 765, 773, 774, 776, 863, 876, 966, 1223, 1224, 1456, 1486, 1519, 1724, 1725, 1751, 1752, 1753, 1754, 1755, 1765, 1766, 1767, 1773, 1776, 1779, 1781, 1782, 1783, 1785, 1786, 1787, 1788, 1789, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1819, 1827, 1831, 1832, 1833, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1843, 1844, 1845, 1846, 1848, 1849, 1850, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1901, 1
[1, 2, 3, 4, 5, 6, 7, 11, 17, 33, 44, 47, 79, 80, 82, 88, 94, 98, 99, 101, 106, 108, 135, 136, 137, 138, 139, 140, 145, 146, 153, 154, 160, 168, 182, 187, 231, 350, 374, 378, 379, 382, 389, 413, 437, 636, 765, 773, 774, 776, 863, 876, 966, 1223, 1224, 1456, 1486, 1519, 1724, 1725, 1751, 1752, 1753, 1754, 1755, 1765, 1766, 1767, 1773, 1776, 1779, 1781, 1782, 1783, 1785, 1786, 1787, 1788, 1789, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1819, 1827, 1831, 1832, 1833, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1843, 1844, 1845, 1846, 1848, 1849, 1850, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916
# Script used to save the URL paths of all the AttachmentData records in the Whitehall database
#
# To use:
#
# 1. SSH to a whitehall_backend box in integration/staging
# 2. Save the script as /tmp/save-attachment-data-urls.rb
# 3. Run `sudo su - deploy`
# 4. Run `cd /var/apps/whitehall`
# 5. Run `govuk_setenv whitehall bundle exec rails r /tmp/save-attachment-data-urls.rb`
require 'net/http'
file_path = Rails.root.join('tmp', 'tmp.txt')
File.open(file_path, 'w') { |file| file.puts(Time.now) }
asset = Asset.create!(file: File.open(file_path))
def display_headers(response)
puts "Code: #{response.code}"
puts "Etag: #{response.header['Etag']}"
puts "Content-Type: #{response.header['Content-Type']}"
@chrisroos
chrisroos / README.md
Created September 11, 2015 11:10
Using pcregrep to extract reference links from Markdown documents

Using grep (well, pcregrep) to display the labels of reference style links in a Markdown document:

$ pcregrep --only-matching=1 \  # Only display the match from group 1
  "\[.*?\](\[.*?\])" \          # Find [text][label] reference style links
  /path/to/document.md | \      # Path to the markdown document
  sort                          # Optionally sort the output

I had the regexp working in grep but couldn't capture just the group I was interested in. This Stack Overflow answer suggested pcregrep.

assets_checked = 0
assets_without_content_type = 0
percentage_of_assets_to_test = 0.1
assets = Asset.all
assets.each.with_index do |asset, index|
next unless (rand * 100) < percentage_of_assets_to_test
id = asset.id