Skip to content

Instantly share code, notes, and snippets.

View philsturgeon's full-sized avatar

Phil Sturgeon philsturgeon

View GitHub Profile
@philsturgeon
philsturgeon / urf.sh
Last active November 6, 2015 18:11
PHP being PHP
$ irb
irb(main):001:0> "text" * 4
=> "texttexttexttext"
$ php -a
Interactive shell
php > var_dump("text" * 4);
int(0)
@philsturgeon
philsturgeon / example.rb
Last active December 16, 2015 18:17
Using AMS? Whitelist includes to avoid overuse and n+1 slowness
# Somewhere, maybe in ApplicationController
def whitelist_relationships(params)
whitelist = %w(driver seats driver.avatar)
params[:include].split(/,/) & whitelist if params[:include].present?
end
# Then when rendering the resource
render json: Array(@resources), root: :trips, include: whitelist_relationships(params)
@philsturgeon
philsturgeon / example.php
Created March 16, 2016 18:27 — forked from anonymous/example.php
Instance variable type checking needed?
<?php
class Example
{
public $number;
public $text;
public function __construct(int $number, string $text)
{
$this->number = $number;
@philsturgeon
philsturgeon / talk.md
Created March 31, 2016 21:53
What Rails Can Teach PHP About Building APIs

As somebody who's built APIs with PHP since 2009, and built APIs with Rails for the last two years, the contrast in some of the tooling available is mind-blowing. When it comes to factories for generating test data, spec-driven testing with tools like RSpec, mutation testing, simplistic state machines, serialization and deserialization in JSON-API, REPL debugging with breakpoints, file upload handlers, etc., Ruby (and Rails) very often has a strong lead in maturity of the tooling.

Objectively speaking, PHP either does not have some of these tools, or they're immature in comparison. This is by no means a fault of PHP as a language or a community. Ruby has had Gems for far longer than PHP has had Composer, and while PHP is starting to learn how to get this done in a post-framework-everything world, it has a way to go.

This talk looks at some of the cool stuff Ruby can do, and some of the lesser known tools that provide similar functionality in PHP.

@philsturgeon
philsturgeon / options.md
Created October 20, 2016 14:28
RESTful Liking

Option 1

Modifying a global resource can feel a bit weird, but essentially as an authorized user, you are looking at your customized representation of the resource, and not just a direct 1:1 of whats in the database.

  • Like: PATCH /bars/123 with field liked: true
  • Unlike: PATCH /bars/123 with field liked: false

This can feel a bit weird if you consider it as corrupting the global value, but headers often change the response and that's probably fine. It can be weird to show different attributes depending on the user (I avoid this) but you can probably jam it in meta as the JSON-API allows this already? It's relevant data, just not necessairily part of the resource.

Option 2

@philsturgeon
philsturgeon / fucking-tests.sh
Created November 2, 2016 19:51
Get those tests outta here!
bundle exec rspec | grep -ohi 'rspec \./[^\:]*' | sed -e 's/^rspec //' | while read f; do [[ -a "$f" ]] && git rm "$f"; done

Taken verbatim from https://www.congress.gov/bill/114th-congress/house-bill/2029/text

   TITLE &lt;&lt;NOTE: Visa Waiver Program Improvement and Terrorist Travel 
  Prevention Act of 2015.&gt;&gt;  II--TERRORIST TRAVEL PREVENTION AND VISA 
WAIVER PROGRAM REFORM
SEC. 201. &lt;&lt;NOTE: 8 USC 1101 note.&gt;&gt; SHORT TITLE.

    This title may be cited as the ``Visa Waiver Program Improvement and 
Terrorist Travel Prevention Act of 2015''.
@philsturgeon
philsturgeon / gist:64f7b611a035084b39673853c7e43f0d
Last active June 8, 2017 16:54
Janky Quick Example of RPC API being cool

POST /check-v1

Request

{
  "user_uuid" : "12345",
  "policy_action" : "do-a-thing"
}
@philsturgeon
philsturgeon / gist:bfabe7461ec6faf9457b6a75f25c0d9c
Created August 7, 2017 20:29
The most basic-ass presenter ever
class Api::V3::CompanyPresenter
attr_accessor :company, :includes
def initialize(company, includes: [])
@company = company
@includes = Array(includes)
end
def as_json
{
@philsturgeon
philsturgeon / wtf.md
Last active December 7, 2017 18:44
Postman disagrees with itself

Hey, I’m super lost.

Currently im trying to make API Flow work:

luckymarmot/API-Flow#149

There is an output.json which is a test case, and accoring to Postmans JSON Schema it is invalid.

I'm fixing a few (headers cannot have a null value) but the auth section is triggering errors. It looks like this: