Get Fuzzy with LEVENSHTEIN

Discovered a super handy Postgres extension tonight: fuzzystrmatch. This lil cutie is a real godsend when dealing with potentially crummy user input, such as, oh say, for a Rails project where you’re requiring your less-than-tech-savvy relatives and future inlaws to input their email address in order to access your wedding website. Note: said website is badass, built-from-scratch, and open source.

Here’s a visual:

Login Modal

Imagining that scenario, one might be concerned about poor conversion due to typos, misspellings, or any other of the myriad problems that plague “uncontrolled” user input. And extra sadly, poor conversion here translates to a sparsely attended wedding and future full of angry inlaws… no bueno.

Enter fuzzystrmatch. Adding this extension to your Postgres database gives you some super handy fuzzy string matching functions, my favorite of which so far is levenshtein. levenshtein calculates the difference between two strings, aka the “edit distance”, and it’s based on this metric named after this guy.

You can use it to make some extra forgiving ActiveRecord queries, as laid out below.

> User.find_by('levenshtein(lower(email), lower(?)) <= 3', 'test@email.c') => #<User id: 1, email:...
      
Read More

BASH Tips and Tricks

All tips courtesy @devinburnette

Basics

$ w

see who else is on box

$ w 14:15 up 5 days, 19:23, 7 users, load averages: 1.47 1.56 1.82 USER TTY FROM LOGIN@ IDLE WHAT ktravers console - Mon11 3days - _mbsetupuser console - Mon11 3days - ktravers s000 - Mon11 1:35 gulp ktravers s001 - Mon11 - w ktravers s002 - Mon11 4:10 node /Users/ktravers/.nvm/versions/node/v6.2.2/bin/sails lift ktravers s004 - Mon11 3days /usr/local/Cellar/rabbitmq/3.5.1/erts-6.1/bin/../../erts-6.1/bin/beam.smp -W w -K true -A30 ktravers s005 - Mon11 3days redis-server *:6379 

$ write [user]

send message to specific user

$ wall

broadcast to all users on box

$ whoami

get your current user

$ whoami ktravers 

$ who am i

verbose get current user

$ who am i ktravers ttys001 Aug 29 11:06 

$ clear

put command line at top of window

$ man [whatevs]

get manual for [whatevs] command

$ man man man(1) man(1) NAME man - format and display the on-line manual...
      
Read More

React Code Talk Notes

Overview

  • library that renders data to the DOM
  • can add Flux architecture after you’re comfortable with React

Setting up a React app in our codebase

General Notes

  • avoid using jQuery so we can remove that dependency
  • use vanilla JS where possible instead
  • Gulpfile is not watching these files, so will need to re-compile manually after making changes

Setup

1. Add _src file (_src/code-reading.js)
// _src/code-reading.js var React = require('react') , ReactDom = require('react-dom') , AppComponent = require('code-reading/app.jsx') ; document.addEventListener('DOMContentLoaded', function () { ReactDom.render(<AppComponent/>, document.getElementById('js-main')); }); 
2. Create manifest (javascripts/code-reading.js) and require src file in via sprockets
// javascripts/code-reading.js //= require _bundles/code-reading 
3. Add to asset pipeline (application.rb) and blacklist
4. Create AppComponent (/javascripts/code-reading/app.jsx)
  • React.createClass(){} requires render function
  • React can only return one...
Read More

Best Ways for Beginners To Contribute to Open Source

When I first started out learning to code, the idea of contributing to an open source project was really intimidating. I got that advice from everyone - “Contribute to open source! It’s so easy! Employers love it!” - but I was still hesistant. I’d only been writing Ruby for a couple months; how was I going to contribute anything useful to someone else’s project?

Well, I’m happy to report from the other side, it’s actually pretty painless and fun for beginners at any level to make meaningful contributions to open source projects.

So shake off that imposter syndrome; the open source community needs you. They need everyone, in fact. Open source projects depend on a large and diverse community of contributors to really robustify their codebase. That includes people from all skill levels and all areas of expertise. Plus, the benefits are reciprocal. You’ll get to know the community better, build your support system, and get your name (and your code) out there in a really positive way.

Getting started

So where do you smart? The two best pieces of advice I got in this department were to 1) start small and 2) (in the immortal words of...

Read More

Code Talk Notes - Javascript Testing and Analytics

Javascript Test Updates

jsdom: npm module
- load into node environment
- recreate DOM
- run inside of node using Jasmine

Jade templating
- require in templates
- doesn’t need to rely on Rails asset pipeline

Spies:
- PushStreamSpy
- spyOnBackboneHistory
- can have it substitute the function, wrap the function then delegate to function
- Note: mocks, spies & stubs are all very similar (functionally, conceptually)

Can write specs for Marionette views, Backbone models
- run in node (npm t)
- load in view (same as front end)
- stub out fixtures
- create view then test view

** eval(locus) == binding.pry for node **

Analytics

Tracking should be done client-side; inconsistent if tracked from backend
- in our system, controller will delegate to client-side
- concept of current_visitor bc we don’t have a user yet

To have access to Segment system, be sure to include analytics script call (helper method)

Use aliasing to transfer data from before user is logged in to when after user has logged in
- data stays attached to same user
...

Read More

Code Talk Notes - Librato and Ironbroker

Librato Update

Librato has two stats types: counter vs gauge

When using counter (aka #send_increment), be sure to set summary function to sum and check Service-Side Aggregation.

Librato counter

Ironbroker Refactor

Payload == immutable
State == mutable

Validation for each source

RabbitMq handler - rescue channel failure by creating new channel

begin # do bunny stuff # if channel closes, rescue rescue @channel = @bunny.create_channel # bunny keeps hopping end 

Oj: much more efficient JSON parser

Checkout Source model for some cool meta-programming

New Hierarchy Builder

Structure determined by depth level

Query uses Postgres’s WITH RECURSIVE method
- give it an initial SELECT statement which is run once
- give it another SELECT statment that it calls recursively until it runs out of results
- then combines the SELECT statement

Holds a path array while building, so you can see how you got to your final result

Test failures were a nightmare, but solved by creating a rollout_helper support method.

...
Read More

Code Talk Notes - Promises and Asynchronous Code

Callback Hell

Promises are a pattern for managing async callbacks.

Calling then() generates another promise object, when then resolves at the end of the chain. When promise object resolves, it resolves with the data coming back, which can then be passed to the next promise in the chain. Helps maintain the idea of a return value to asynchronous code - brings return values back.

jQuery

jQuery has promises built in, but not fully up to true A+ promise spec.

When.js

When.js is a much more robust promises library.

When.js library provides many additional helper methods for promises. You can call return when.promise(function(resolve, reject){...}) to return a promise object and pass resolve and reject functions into the promise chain.

Use tap() to add in something asynchronously to the chain.
Use catch() at the bottom of the chain to handle an error at any point in the chain.
Use delay() to add in a delay at any point.
Use timeout() to throw an error if promise isn’t resolved after a certain amount of time. Use with() to pass in a context (usually Read More

Sublime Text Key Bindings

Keyboard shortcuts are a programmer’s best friend, and Sublime Text lets you write your own. I use these five below on a daily basis, and you’re going to want to, too. Here’s the setup:

  1. Open Sublime Text
  2. Type command + shift + p to open your Command Pallette (aka super+shift+p in Sublime lingo)
  3. Select Preferences: Key Bindings - User
  4. Insert the following into this file:
[ { "keys": ["super+shift+w"], "command": "close_all"}, { "keys": ["ctrl+alt+shift+down"], "command": "goto_definition" }, { "keys": ["alt+c"], "command": "insert_snippet", "args": { "contents": "console.log(${1:}$SELECTION);${0}" } }, { "keys": ["alt+d"], "command": "insert_snippet", "args": { "contents": "debugger;" } }, { "keys": ["alt+b"], "command": "insert_snippet", "args": { "contents": "binding.pry" } } ] 

Quick summary of each key binding:

super+shift+w => close_all

Closes all open tabs.


ctrl+alt+shift+down => goto_definition

Navigates to selected method or class definition (based on your cursor’s position).


alt+c => insert_snippet ‘console.log();’

Inserts console.log(); at your current cursor position.


alt+d => insert_snippet ‘debugger;’

Inserts debugger; at your current cursor position.


alt+b => insert_snippet ‘binding.pry’

Inserts binding.pry at your current cursor...

Read More

POODR Recap, or How I Learned To Stop Injecting Dependencies and Love OO

I’ll keep this concise, in the spirit of less is more.

Reading POODR has been one of the most beneficial things I’ve done in my long, illustrious career as a programmer (clocking in at approx $("Feb 02 2015").timeago();). It’s been recommended to me since ~week 2 of my immersive program at Flatiron School, but I’m glad I put it off until I’d built some un-POODR-like apps of my own. That context helped me draw connections between the abstract principles discussed in the book and very real production code I’m working on now. So without further ado:

Main takeaways from POODR:

1. Think about code in terms of objects and messages, not procedures

As Chapter 2 emphasizes, classes (and methods, really) should have a single responsibility. If you find your class carrying multiple responsibilities, break each one into its own separate class. You should conceptualize a class as an object that passes and receives messages, not something that executes some long procedure. Objects that execute a procedure can find themselves performing multiple responsibilities.

One strong example of this is a Ruby Code Challenge feature I’m currently working on, conveniently written entirely in Javascript. Its basic functionality...

Read More

Monkey Patching truncate_html

The truncate_html gem is a really useful tool for clipping off a string of html at a designated point. It has some nice customizeable config options, and best of all, zero third party dependencies. Per its docs, it even does the unthinkable:

This library…parses HTML using regular expressions.

Today I discovered I could extend its usefulness even further with a small monkey patch. Now, before the haters come out in full force, YES - I understand that monkey patching is dangerous and needs to be handled with deft and delicacy. That said, the small size and limited dependencies of this gem make it a prime candidate for practicing safe patching, Justin-Weiss style.

Moving right along…

Desired functionality:

### 1. Multiple break tokens

Per truncate_html’s docs, you can truncate your html after a after a certain number of characters (options[:length]) or at a designated piece of content, like <!-- break --> (options[:break_token]).

Setting a single break_token is great if I’ve created the HTML and set my own unique break_token wherever necessary. But what about situations where I want to pass in any old HTML and...

Read More

AcademyAwardable Polymorphic Associations

If you ever find yourself in a Rails-situation where you need one model to belong to multiple other models, consider using an ActiveRecord polymorphic association. Don’t let the multisyllabic name fool you; polymorphic associations aren’t as complex to build as they might seem. You can do it.

Meryl Streep Can Do It

Let’s consider a completely relatable and engaging example: The Academy Awards. Maybe you’re building an Oscar ballot app for a family member who’s particularly obsessed with this time-honored, hallowed awards show. Your app at minimum would need to contain Actor, Movie, Director, and Vote models (plus all those technical award categories, but we’re going to ignore them for now - just like the Academy). We’ll need some savvy schema design to keep things simple and DRY.

Let’s start by trying to set up our schema the old-fashioned, non-polymorphic way first. Actor, Movie, and Director would all have_many Votes, which means Vote would belong_to Actor, Movie, and Director. Here’s how that would look in our CreateVotes migration:

class CreateVotes < ActiveRecord::Migration def 
      
      Read More
    

It's `document.write(new Date().getFullYear());` - Update Your Footer

As I was updating some content on my personal website today, I noticed the copyright date in the footer still read “2014”. Yeah, it’s May. Go me.

Sadly, I believe future-me from 2016 would probably make this same mistake, so to help myself out, I did a quick google search for “How to auto update year website”, which yielded this extremely helpful site: UpdateYourFooter.com. It offers Javascript and PHP solutions, with JS being the easier option for my needs.

Just add the following wherever you want the year displayed:

<script>
  document.write(new Date().getFullYear());
</script>

You can find more info on the Javascript Date object here. And best of all, this solution is supported in all browsers except IE8. Finally, there’s (of course) a longer discussion about this on StackOverflow, for anyone who wants some additional reading.

Read More

Rendering from a Model with ActionView::Base.new => WAT?

I’m a firm believer in the “make it work” philosophy - solve problems first, then refactor. That said, my team may have gotten a little too creative making our last project work. Just take a look at this gnarly method we cooked up:

class Recipe < ActiveRecord::Base def recipe_card ActionView::Base.new( Rails.configuration.paths['app/views'] ).render( partial: 'menus/recipe', format: :txt, locals: { recipe: self } ) ###🙊🚷 W A T ☠😿### end end 

Yep. We instantiated a new instance of ActionView::Base in a model. And used it to render a view outside a controller. WAT?!

Before I go any further, let’s take care of the necessaries:
- Yes, we know what we did was wrong.
- Yes, we are deeply sorry for our crimes against MVC.
- Yes, we’ll refactor and never repeat this grievous offense again.

But the thing is, it worked. And I’d like to explore how and why before purging this from...

Read More

Dynamic Duos - Dependent Select Menus with JQuery and Rails

The first time I tried to apply “dynamic selection” within a Rails form - where selecting an option in one select field dynamically updates the menu options in a second select field, I had a surprisingly hard time with it. I’d watched the Railscast and read multiple tutorials, but just couldn’t crack the code.

Problem was, I was trying to use a collection of model attributes in the first collection_select menu and a collection of model instances in the second menu, organized using the grouped_collection_select helper. What I learned after much trial, error and a deep dive into the documentation, was that the menu options should both be collections of model instances, where the models are associated through a join table. <– tldr: central lesson of this post.

I could have benefitted from more explicit discussion of those mechanisms, one with a slightly more complex example than countries and states. Towards that end, let’s walk through the steps I took to apply this solution in my Rails application, Parkster.

SECTIONS:
1. Correcting the schema
2. Updating the seeds file
3. Adding appropriate ActiveRecord associations...

Read More

Sublime Linter Rubocop Reboot

This week’s Ruby Weekly had a nice post from Matt Brictson on “Setting up Sublime Text 3 for Rails Development”. It reminded me to finally install the SublimeLinter-rubocop package. This package syncs your linter up with rubocop, highlighting “bad code” as you type (according to the community Ruby Style Guide). Great addition, right?

Well, word to the wise: don’t install a new linter package a couple hours before a technical interview. I didn’t have a chance to tweak the default settings, which as you’ll see below, are a bit…aggressive.

Mark Style Outline

I knew I’d be pair-programming with my interviewer, and no way was I gonna subject her to literally ugly code. I had to scramble to find a fix, and after jumping through a couple different sections in the SublimeLinter documentation, I found the solution. Here’s the lowdown.

Turns out you have 5 different linter mark styles to choose from. The package defaults to "mark_style": "outline". That style’s a little visually overwhelming with my current color scheme, so I decided to switch to the "mark_style": "none", so marks appear in the gutter only.

The...

Read More

Remote True is Magic

I had a really fun time this weekend building a small side project with Sophie DeBenedetto, Jeremy Sklarsky, and Rachel Nackman. We learned a lot of very cool things that you can do with Rails + jQuery, but the coolest was easily remote: true. This one little option is crazy powerful. Case in point: by adding remote: true to our Search form, our searches.js file went from this:

$(function () { submitListener(); }) function submitListener () { $('#new_search').on('submit', submitSearch); } function submitSearch (e) { e.preventDefault(); var url = $(this).attr('action'); var $form = $('form#new_search'); $.ajax(url, { method: 'POST', url: url, data: $form.serialize(), dataType: 'script', complete: function(response){} });...
      
Read More

Listen Up - Tech Podcasts

Back in my former life as an art historian / art shipper, it wasn’t enough to simply know about art. You had to know about the other people who know about art - the key figures shaping the field, your peers working on the same stuff you were - that was just as critical as being able to recite off Greek column types or the core members of the Arte Povera movement (which I can still do, thanks very much, humanities degrees).

Now that I’m joining this new realm of web development, I have a whole world of new people to get to know. Honestly, it’s been one of my favorite parts so far; the Ruby world especially has so many weird,wonderful voices, it’s been a blast reading up on and listening to the amazing cast of coders behind all these concepts I’m learning about.

One invaluable resource in this process has, of course, been podcasts. I pretty much only listen to podcasts these days (mostly comedy, with a dash of NPR and PRX staples), so it seemed natural to turn there for my tech education as well. Below are some of my favorites I’ve found so far....

Read More

Who's Ahead Now?

After week one of learning Rails, I was eager to test out my new skills on a new side project. I wanted to build something fairly simple, that would utilize skills I felt comfortable with - JSON parsing, ERB, simple search and routing - while also pushing me into uncharted territory, like deploying on Heroku. I spent the afternoon brainstorming, circling around some sort of app that would answer a single simple question (like Is It Raining?, one of my fave single serving sites). I had a couple good ideas, but my partner came up with the best one: is Hillary winning?

Now obviously a website that just says “aw yass” every time it renders doesn’t have much utility. So I decided to expand the concept a bit and build a site that yields the current frontrunners for each party in the current presidential race. A quick rubygems search pointed me to HuffPost’s Pollster API, which is really nicely maintained. Armed with data and a plan, I started building.

Texts from Hillary

The app ended up being pretty simple:
- One model (Search)
- Three controllers (ApplicationController,...

Read More

--no-test-framework

Note to self:

Bart Simpson chalkboard

Why? Without the --no-test-framework flag, Rails generate will do the following:

a) overwrite any existing tests that you, your colleagues, or your instructors have already written for that particular object

b) generate unnecessary / unwanted tests (see Thoughtbot’s helpful post on diminishing test coverage returns).

Better to write your own tests. Here’s hoping repetition leads to retention…

UPDATE: my classmate Rachel Nackman wrote an excellent blog post on Rails generators, which included an extremely helpful tip for not overwriting tests. Just add the following code to your config/application.rb file:

module Testing
  class Application < Rails::Application
    ...
    config.generators.test_framework false
  end
end

Voila! Your generators are now auto---no-test-frameworked.

Read More

Flatiron Twitter CLI

Last week, Avi pitched a pretty cool idea to the class: build an “auto-follow bot” for Flatiron student twitter accounts - something we could use to easily auto-follow everyone in the class.

Avi Flatiron Twitter pitch

I’d been looking for a side project to work on (inspired by my classmates who’ve built some very sweet side-projects already: ex.1, ex.2, ex.3, ex.4) and this seemed like a perfect opportunity. I’ve never built a bot, but I know how to build a CLI, so I put labwork on pause and spent my Saturday building the FLATIRON TWITTER CLI instead (available at your local github).

Let’s walk through the build, starting with setup. I tried to follow best practices, setting up bin, config, and lib folders, along with a Gemfile and README.

Right off the bat, my bin/run file almost sunk me. I wanted users to be able to initialize the CLI by simply cd’ing into the directory and running bin/run.

#!/usr/bin/env ruby require_relative '../config/environment' FlatironTwitterCLI.new.call 

I thought the code above would do it, but...

Read More

Refash Your Bash - Bash Prompt Customization

When I was just starting out learning to code, it quickly became clear I’d be spending a lot of time in Terminal. Never one to skimp on workspace feng shui - just ask my old art world colleagues - I set about post haste to fine tune the CRUD out of my bash profile, starting with the bash prompt.

Now, I’ll caution, there’s an endless plethora of resources out there on the ol’ internet about customizing your bash prompt, and my first time through, I think I read nearly all of them (really went down the .bash_profile rabbit hole, I’ll admit). To save you some time and tedium, I’ll summarize my specific mods below, with tl:dr links to resources for further customization.

Note for fellow rookies: My bash prompt is contained inside a nice little function at the top of my .bash_profile file. I’d recommend putting this function towards the top since 1) your terminal loads the prompt first and 2) that way it won’t interfere with any of the rest of your bash code below. Now without further ado …

###My Complete Bash Prompt Function

function parse_git_branch { git branch --no-color 2>...
      
Read More

Using Github for Lecture Notes

Flatiron School has a great system for sharing lecture notes: a dedicated Github repo. I forked the repo during week one, thinking I’d pull down the files to my local, take notes on the notes (Inception-style meta-noting), then push back up to my forked repo. No sweat.

But then comes the scary part for us git-n00bs - getting updates. I felt real comfortable pushing to / pulling from my own repositories. But to get the next days’ lecture material, I was gonna have to add my instructor’s repo as a remote. Yikes. Now that’s a scary connection to make. Any mistakes, and I’m screwing up the lecture docs for Avi, who might have to redo them, and my classmates, who need this material asap for labs + projects. I needed to handle this delicate operation with extreme care.

Indiana Jones Raiders of the Lost Ark animated gif

As I now do in all these types of situations, I went straight to Github support. Here’s the solution, specific to our Flatiron repo.

1) Open Terminal and cd to whatever folder you want to use for lecture notes storage.

...
Read More

Adventures in DNS Configuration

###Or How I Stopped Worrying, and Learned to Love GitHub Support

One of the main tenants of the The Flatiron School is “Always be a beginner,”* which has been pretty easy for me to embrace so far, since I am the noobiest of coding noobs right now. Pretty much every day, I’m reminded of my beginner-dom, even in realms I thought I’d more or less mastered.

Case in point, this very blog.

Day one, we were asked to set up technical blogs, which we’d use to write about all the cool new concepts we’re learning. I was pretty excited to dive right in, especially since I’d just built my own portfolio site at my newly-purchased custom domain, kate-travers.com. When I found out GitHub offered free hosting through GitHub pages, I about flipped. This was gonna be great. I could build out my new site with a SEO-friendly blog feature, perfect my git skillz and gain all kinds of Github coder cred in one fell swoop. I was a subdomain and CNAME record away from blogging like hacker.

Now, this was not my first time at the DNS-configuration rodeo. I’d set up probably +10...

Read More