Skip to content

Instantly share code, notes, and snippets.

View Gattermeier's full-sized avatar
🪲
developing bugs

Matthias Gattermeier Gattermeier

🪲
developing bugs
  • New York Times @nytimes
  • Planet Earth
View GitHub Profile
@Gattermeier
Gattermeier / oak-newsletter.json
Created October 27, 2023 16:21
JSON payload for Oak Newsletter
{
"documents": {
"body": {
"type": "doc",
"content": [
{
"type": "related_links",
"content": [
{ "type": "related_links_title" },
{ "type": "related_links_description" }
@Gattermeier
Gattermeier / oak-schema-structs.go
Last active October 27, 2023 16:25
go structs from oak schema
// GO STRUCTS FROM OAK SCHEMA https://scoop.stg.nyt.net/oak/v1/spec/#
// OakAsset schema: https://scoop.stg.nyt.net/oak/v1/spec/#/schemas/OakAsset
type OakAsset struct {
ID string `json:"id"`
URI string `json:"uri"`
EditorLink string `json:"editorLink"`
Documents Documents `json:"documents"`
Metadata OakMetadata `json:"metadata"`
}
@Gattermeier
Gattermeier / scoop-oauth.go
Last active October 11, 2023 15:47
go implementation to get access token for oak from scoop auth
// https://github.com/nytimes/nodetools/blob/main/scoop/auth.js#L15C1-L37C3
func getToken() (string, error) {
conf := &oauth2.Config{
ClientID: "interactive",
Endpoint: oauth2.Endpoint{
TokenURL: tokenURL,
AuthURL: authURL,
},
}
token, err := conf.PasswordCredentialsToken(context.Background(), username, password)
@Gattermeier
Gattermeier / lower_bound.go
Last active August 17, 2020 18:14
Lower bound of Wilson score confidence interval for a Bernoulli parameter
// Golang implementation of Evan Miller's algorithm for ranking stuff based on upvotes:
// http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
// playground link: https://play.golang.org/p/dV6yk6pBY5y
func lowerBound(upvotes, n int, confidence float64) (float64, error) {
if n == 0 {
return 0, errors.New("n can't be 0")
}
@Gattermeier
Gattermeier / async-await-iife.js
Last active February 7, 2018 17:32
Async / Await IIFE return in Hapi 17 routing
server.route({
method: 'GET',
path: '/api/endpoint',
handler: (request, h) => {
return (async () => {
try {
const { payload } = await Wreck.get('URL')
return h.response(payload && payload.toString())
}
catch (ex) {
@Gattermeier
Gattermeier / lower_bound.js
Created April 13, 2017 17:55
Lower bound of Wilson score confidence interval for a Bernoulli parameter
// Node.js implementation of Evan Miller's algorithm for ranking stuff based on upvotes:
// http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
const stats = require('simple-statistics')
const lower_bound = (upvotes, n = 0, confidence = 0.95) => {
if (n === 0) return 0
// for performance purposes you might consider memoize the calcuation for z
const z = stats.probit(1-(1-confidence)/2)
@Gattermeier
Gattermeier / endpoint.js
Last active March 25, 2017 01:12
Default Express Router File for APIs
/*
My routing starter template for api endpoints when using Express.js
app.use('/api/endpoint', require('./endpoint.js'))
*/
const express = require('express')
const router = express.Router()
router.use((req, res, next) => next())
@Gattermeier
Gattermeier / once.js
Created April 25, 2016 14:51
memoize a function to only fire once.
var once = function(func) {
var alreadyCalled = false;
return function() {
if (!alreadyCalled) {
func.apply(this, arguments);
alreadyCalled = true;
}
};
};
@Gattermeier
Gattermeier / Button.jsx
Last active December 17, 2015 22:20
Sample React Component for testing via Tape.
'use strict';
import React from 'react';
class Button extends React.Component {
static propTypes = {
label: React.PropTypes.string,
className: React.PropTypes.string
}
static defaultProps = {
label: 'button',
@Gattermeier
Gattermeier / tape-react.js
Last active September 15, 2016 08:55
Unit Testing for React with Tape
import React from 'react';
import { createRenderer } from 'react-addons-test-utils';
import createComponent from 'react-unit';
import tape from 'tape';
import addAssertions from 'extend-tape';
import jsxEquals from 'tape-jsx-equals';
const test = addAssertions(tape, {jsxEquals});
// Component to test
import Button from '../components/Button';