Kevin Coleman

makes apps for businesses with Ruby on Rails.

I Graduated From a Code School. Now What?

I attended a Dev Bootcamp hiring fair in search of a junior web developer to assist me at Zugata. Most of the applicants at the fair were recent graduates (less than 2 weeks out of the program) and they presented projects that they coded together in the previous weekend.

Unfortunately, after several interviews, I wasn’t able to find a candidate in the bunch that could fit the role I was looking for. Many of them came really close, but they needed a few more months of study before I thought they would be ready for the position.

If you just graduated from a bootcamp, here is what you should be doing:

  1. Don’t waste time applying for jobs, yet.

    Code schools give you an awesome foundation for breaking into the tech space, but they can only do so much in the 12-16 weeks of their program. There are still many things you need to learn before you are ready to start interviewing and hopefully the code school you attended has given you the tools you need to learn these new skills.

  2. Build a cool project

    When you graduate from a code school, you are a wild card. No one really knows what you are capable of doing. Building a side project demonstrates your ability to have creative ideas and to execute them. Projects ideally should be something you are passionate about and are more advanced than what the code generators spit out.

    Startups want to see that you are creative and can solve problems. Having a side project shows that you can identify problems and can build solutions to them.

    At the end of the day, you should pick a project that solves a problem in your life. I really like the idea of building tools that can help you in your job search. When I lived in Atlanta, I wanted to work for a startup, but I struggled finding what startups were hiring. I built Atlanta Startup Jobs to help me in my job search. As part of the project, I had to write a web crawler to search the internet for job postings which made my project much more interesting than a simple generated app.

  3. Focus on programming fundamentals

    Spend a couple hours every day studying algorithms and programming techniques. Interview Cake is a great place to get started, but I would search around the internet for interesting coding challenges. The daily programmer on reddit has some fun coding challenges to help prep you for your interview.

    In SF, there are coding interview prep meet up groups which are awesome for solving these types of coding challenges. When learning how to solve algorithm questions, focus on the process of determining the answer and not so much on the answer itself. You will be hit with a question you have never heard of before, but if you figure out a system that works for you for solving algorithm questions, then I am sure you will be able to solve it :).

  4. Attend local meet up groups

    Show your face in your local community. Start establish a brand for yourself in your area as a developer. Meetup.com is the king for finding these.

  5. Blog!

    Write about what you learned and what cool things you are doing. I write this blog because when people want to find more information about me, I get to control what pops up in google.

    People that are good at what they do like talking about what they do. So start building yourself an audience.

  6. Apply for jobs

    After you have continued your education and feel confident in your interviewing skills, start applying for jobs. Hired.com accepts code school graduates, but they are very picky about who they choose.

    Hacker news has a monthly job posting requesting applications for tech startups around the world. The post usually happens on the first of the month. So setup a calendar notification to catch that early.

    At meet up events, many recruiters or hiring managers attend coding meetups to help source candidates for open positions. Typically they announce at the start up of the meet up who is hiring and what they are looking for. Definitely a good way to get face to face with someone.

    I would also consider not mentioning you attended a code school. I think that the industry itself has a few premonitions about code school graduates that may not help you in your job search. I would focus your resume more on what cool projects you have done and what you know rather than where you got your education.

Bulk Email Sending With Ruby

A friend of mine last week has a mailing list from 5+ years ago from an old niche website that he would like to market a new website too. He asked for my advice on the cheapest and most effective solution for blasting out to this 100k user list.

I approached this problem with a few things in mind. Mail providers don’t trust emails from new domains and new email servers. Mail servers, much like websites, have a reputation score. If a mail server thinks you blasting too much unwanted email, then they assume you’re a spam king and mark all emails sent from your address as spam.

So the key take away is don’t let mail servers think you are spamming their users. The best way to protect yourself getting a bad reputation is making sure you are sending to a legit email and you only send it once.

Since Mailgun’s IP addresses are have a high reputation, we opted to use them instead of rolling our own POP server. Plus they offer 10k emails sending for free.

In my script I validate an email 3 different ways before I determine if it is a legit email account.

1
2
3
4
5
6
7
8
9
10
11
12
def verify_domain(email)
  c = Whois::Client.new
  c.lookup(domain_from_email(email))
  c.registered?
end

def domain_from_email(email)
  split = email.split('@')
  domain = split[-1]
  dd = domain.split('.')
  dd[-2] + '.' + dd[-1]
end

The first check is to see if the domain is even a valid domain name. I use the gem [whois](https://github.com/weppos/whois) to verify the domain of the email is legit. domain_from_email is just a hacky way of pulling out the domain from the email account. This would break if your email had a .co.uk domain, or anything with 2 dots in the tld, but most of the emails don’t fall into that category.

So, now we know the domain is real, lets check the MX records to see if the domain even supports receiving emails.

1
2
3
4
  def validate_email(email)
    ValidateEmail.valid?(email)
    ValidateEmail.mx_valid_with_fallback?(email)
  end

This is using the valid_email gem.

We could actually ask the mail server if the email address is legit, but that can be an unreliable source of information depending on how the machines are configured.

Since there could be duplicate email addresses in this list or in case I need to restart the process, I need to keep track of who I have emailed already.

1
2
3
  def already_sent?(email)
    SentEmail.where(email: email).count > 0
  end

So I check to see if I think I have seen this email before.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  @emails = load_emails

  @emails[0..50].each do |email|
    begin
      raise "verify domain failed" unless verify_domain(email)
      raise "email validate failed" unless validate_email(email)
      raise "already sent" if already_sent?(email)

      Inviter.invite(email).deliver
      SentEmail.create!(email:email)
    rescue StandardError(ex)
      FilteredEmail.create(email:email, reason:ex.message)
    end
  end

Wrapping it all together, we have a check each of our validation methods. If any one of them fails, we throw an exception which makes note of it in the rescue method.

I limit the run to 50 emails initially just so I can keep an eye on my mailgun reputation.

Because mailgun limits the number of emails that can be processed per hour, I am not going to bother threading this, since this will process much faster than mailgun will let me.

Rails Tip: Returning

If you have been programming in Rails for pretty much any length of time, you will definitely run into a method that looks like this:

1
2
3
4
5
6
def some_method
  foo = []
  foo << Cat.new
  foo << Dog.new
  return foo
end

You initiate some local variable, do some things to it, then return it.

There is a lot of boiler plate code here and the smart developers behind rails added a method to the object class called returning. This method simplifies this workflow. Here is an example of the returning method in action.

1
2
3
4
5
6
def some_method
  returning foo = [] do
    foo << Cat.new
    foo << Dog.new
  end
end

This is a much more elegant solution since it just reads more like a sentence than the previous example. “Add Cat and Dog to foo then return” vs “create foo. Add Cat and Dog. return foo”. Ruby’s ability to easily add DSL to objectss on the fly makes ruby so powerful and easy to maintian.

Rails Tips: Fast Relation Detection

When working with active_record, you often need to establish if a row exists to see if you can do operations against it. Here is an interesting technique when checking if objects or relations exist in a collection.

Many developers use the present? method, but this is bad practice, because it performs a standard SELECT query against your table which, relative to a count, is very slow.

1
2
blog.posts.present?
# SELECT id FROM `posts` WHERE ("posts".blog_id = 1)

The better trick would be to use the exits? method to more efficiently determine if a record exists since it just has to return a number instead of a large collection of objects

1
2
blog.posts.exists?
# SELECT id FROM `posts` WHERE ("posts".blog_id = 1) LIMIT 1

Alternatively, if you know you are going to need the object later, it is better to just load exactly what you need and use it later:

1
2
3
4
5
if blog.posts.length > 0
# SELECT id FROM `posts` WHERE ("posts".blog_id = 1)
  do_something_with_posts(blog.posts)
  # No query
end

If you had done a exists or a count on the post, then you would have to run back to the database to fetch the rest of the objects. While this is more memory intensive, it can be much faster technique.

Regex: Escaping Slashes

Escaping slashes in can leave regex expressions difficult to read. A regex statement that is meant to identify a url like this one http://atdc.org/wp-admin/admin-ajax.php.

would look something like this using the standard /expression/ notation:

1
/http:\/\/atdc.org\/wp-admin\/admin-ajax.php/

But if you use the %r{expression} notation, then your regex expressions do not need to be muddled with escaping slashes:

1
%r{http://atdc.org/wp-admin/admin-ajax.php}

Much easier to read :)

Limited Retry Pattern

The limited retry pattern is a great tool for retrying method calls a limited number of times. In instances where I want to make an API with a limited number of tries. I would have my method look like this:

limited_retry.rb
1
2
3
4
5
6
7
8
9
def api_call(args)
  @retries = 0
  begin
    do_api_call(args)
  rescue ApiError, Timeout
    @retries += 1
    retry if @retries < 5
  end
end

The retry method is really powerful in ruby. If a block is rescued, by calling “retry” it will attempt to run that block again. For example, if your server has a spotty connection, it would make sense to retry an API request twice if it failed to reach the server.

The DefRetry gem by DiegoSalazar can be used to add DSL to your ruby classes that you want to retry a few times.

Adding Queues to Your Models

At work recently, we needed to introduce the concept of a queue into our rails app. MySQL really doesn’t do a great job with these types of things, because it is so slow. Redis is a much better solution, because its speed and ease of use.

I built the queueing concern on top of RedisObjects, because it has such a great DSL wrapper for storing ruby types into redis. The main feature I use is their list object.

queueable.rb
1
2
3
4
5
6
7
8
9
10
module Queueable
  extend ActiveSupport::Concern

  included do
    include Redis::Objects
  end

  # more code here

end

This adds Redis::Objects to the models that I use this concern on so I can leverage their list DSL method.

In my model, I want to write my own DSL to make it super easy for me to create new queue attributes in all of my models. I want to be able to just do this in my model:

user.rb
1
2
3
4
5
6
7
class User < ActiveRecord::Base
  include Queueable

  queue :scoring_queue
  queue :email_queue

end

For the purposes of my application, I needed to be able to:

  • rotate the queue, such that the top element returned and is moved to the bottom of the queue.
  • set the entire queue to something new.
  • re-queue a element, so that if it exists in my queue, it gets sent to the bottom.
user_controller.rb
1
2
3
4
5
6
7
class UserController < ActiveController::Base
  def show
    user = User.find(params[:id])
    next_score = user.rotate_scoring_queue #gets the top element and moves it to the bottom
    user.requeue_scoring_queue invited_score # moves this score to the bottom of the queue
  end
end

To accomplish this, I use a meta programming to dynamically create these methods. The define_method in ruby is an extremely powerful concept that allows developers to create methods based with dynamic names.

queueable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  class_methods do
    def queue(queue_name)
      list queue_name

      define_method("rotate_#{queue_name}") do
        queue_list = send(queue_name)
        queue_list.rpoplpush queue_list
      end

      define_method("#{queue_name}=") do |vals|
        queue_list = send(queue_name)
        Redis::Objects.redis.multi do
          queue_list.clear
          queue_list.push *vals.reverse if vals.length > 0
        end
      end

      define_method("requeue_#{queue_name}") do |target|
        queue_list = send(queue_name)
        number_removed = queue_list.delete target
        return if number_removed == 0

        last_item_in_queue = queue_list.first

        if last_item_in_queue
          queue_list.insert :before, last_item_in_queue, target
        else
          queue_list.push target
        end
      end

The first line def queue adds in my DSL method to the model to allow queues with a given name to be created.

Unfortunately RedisObjects doesn’t come with an out of the box way to set an entire list at once. So I had to write my own using Redis transactions. So the second method overrides the setter method of the list to accept an array of values that we will set.

The re-queue method removes a target value from the queue. Then it sets it to the queue, if it can’t find a last item (then that means it was the only item in the queue), it adds it back to the list.

Hosting Side Projects for Free

With Heroku’s latest pricing restructure announcement, I decided it was time to re-evaluate my side-project hosting strategies. Historically, Heroku has been my go to as a hosting provider for my various rails application endeavors, because it is so easy to setup and, most importantly, free.

Previously my strategy for free hosting on Heroku without any server sleeping was to configure new relic to measure server uptime. This was a great trick to keep your Heroku server from sleeping and it let you keep an eye out for server down time. I shall be sad to see that go :(.

My new recipe for creating new side project deployments is as follows:

Buying a domain for free

With the new influx of TLD extensions, many registrars are heavily discounting these new extensions in order to encourage main stream adoption. You can snag a new domain name for dirt cheap, and often free, using one of these new domain extensions.

If you are still a student or still have a .edu email address (thanks, Georgia Tech!), you can register a .me domain for free and/or a heavily discounted .com or .io domain using Github.com’s education pack.

Checkout register.science, register.party, and get on the Alpnames mailing list to be notified of when free domain names are available for registration.

Hosting

Heroku (free-ish)

You can stick with Heroku dyno’s but like I said before, they are no longer allow websites to be active 24 per day. Heroku will still be my go to for scheduled rake tasks, or anything that doesn’t need to be awake all day since it is so easy to deploy to.

Heroku also has great free database hosting for side projects. As long as your db doesn’t exceed 10,000 rows, Heroku will host it for free and give you a nice interface to run queries on.

Apigee (free)

Apigee Edge promises a lot of really nice features for their developer accounts: a buttload of storage space, unlimited bandwidth, and api analytics. The big gotcha’s though are they only host nodejs applications and their documentation is terrible. Once if you can figure out how to get your website running, it is pretty simple to maintain, but getting it up there may take hours. I used them for the Sears Hackathon and the struggle was real.

You can also do some pretty sweet stuff with combining api’s into new apis. They also have a service that competes with Parse for managing mobile app data, but again, the documentation is terrible. Good luck.

Github pages (free)

I host this blog on Github pages and build things with Octopress. Deployments and settings things up are a breeze, but github only hosts static content.

A small part of me is considering moving AtlantaStartupJobs.com to github, because the content there only changes once per day. I would just need to write ruby tasks to grab the latest Atlanta start up jobs, generate the html for them, and then push up to Github.

Since it is static, there is not backend database other than static hosting .json files, which could be a thing…

Amazon Web Services (free-ish)

For one year, you can utilize the basics of AWS for free. One big catch here is setting up AWS servers and configuring them can be a weekend project on its own, unless you are familiar with devops. It is not where near as simple as Heroku and you are limited by the resources of one ec2 machine. It doesn’t scale well if you plan on hosting a bunch of different projects.

AWS has RDS which makes it super easy to throw up a database. They support mysql, postgres, and a few others.

OpenShift (use this one!)

OpenShift seems like the best drop in replacement for Heroku. They have a free tier with reasonably powerful servers. Unfortunately the database shares the same limited resources the application server utilizes, so you get a bit less than 1GB of storage space for your application and database to share.

My strategy here is to keep my Heroku database up and just configure my OpenShift application to connect directly to the Heroku one, so I don’t have to deal with shared resources. This may make my website a little bit slower, but it should be good enough.

Cloud99

I have heard bad stories about Cloud99 and they were hacked a few years ago, plus they are not free. My advice is to stay away.

Free email

Having an @gmail.com account as the linked support email for your side project is so not classy. You can configure your free custom domain with Zoho and be able to email your customers from a more legit looking domain. Be sure to configure your MX records properly so your new domain doesn’t get sent to the spam folder.

Free transactional email sending

I really like Mailchimp’s Mandrill. I have been using them for years and haven’t had any problems. They offer a generous number of emails sent (10k) to be exact and don’t limit their services in features, just mailing volume.

Free SSL certs

Another sweet trick you can do for your side project websites is to use Cloudflare to deliver valid SSL certificates for your projects. From an SEO perspective, google loves SSL and from a safety and privacy perspective users love SSL. You can read up on how to do with here.

It took me 15 minutes to configure kcoleman.me and AtlantaStartupJobs.com to be behind Cloudflare.

Adding Search to Atlanta Startup Jobs

I found a few minutes today to finally add search to Atlanta Startup Jobs. I had been putting it off for a long time, thinking it would be a long laborous process, but I found it was shockingly easy. Here is what I did:

Install pg_search

The two leading gems that drive postgress fulltext search are pg_search and textacular. I went with pg_search, because it has more love on github.

Gemfile
1
  gem 'pg_search'

and run bundle

console
1
  bundle

Modify model

For the models you want to search, add include PgSearch to the top of them in order to enable the pg_search DSL. Since Atlanta Startup Jobs only has one searchable model (job_posts), I include this line in the model:

Gemfile
1
  pg_search_scope :search_generic, :against => [:title, :company_name, :description], :order_within_rank => "created_at DESC"

I included the “order_within_rank”, to protect the order across different pages, since postgres randomly ranks items with equivalent rank score.

Modify controller

Here is what my search action looks like in my controller code:

job_posts_controller
1
2
3
4
def search
  @job_posts = JobPost.search_generic(params[:q]).page params[:page]
  render :index
end

Rails Patterns: Builders and Models

A huge problem with ruby on rails apps is many of them end with overly cluttered models and controller methods. An attempt to solve this would be to move what you can to your active record models, but then you end up with heavily bloated model classes that extend way beyond their intended active record design patterns. Active record models should just manage the communication between the rails app and the database. Anything else is just code smell.

In all of my experience with rails projects, I found that my active record models typically ended up having a group of methods that helped me create the model itself (think when you create a user, you also need to create a user_profile or if you want to find or create a model).

Builders

You can easily break this type of code away from the model by following a builder pattern. Builder patterns are great for creating objects with business logic based on given parameters. It adds an easy way to add business logic to model creation without adding bloat to your active record model class.

To follow this pattern, create a directory called builders in the /models directory.

Here is an example of my user class before and after a refactor with the builder pattern.

before:

user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  def self.create_admin_user email, password
    user = User.find_or_create_by email: email, password: password
    user.add_role :admin
  end

  def self.create_staff_user email, password
    user = User.find_or_create_by email: email, password: password
    user.add_role :staff
  end

  def self.create_user_with_invite email, password
    user = User.find_or_create_by email: email, password: password
    user.send_invitation
  end

after:

user.rb
1
  # nothing to see here
/builders/user_builder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  class Builders::UserBuilder
    def self.create_admin_user email, password
      user = User.find_or_create_by email: email, password: password
      user.add_role :admin
    end

    def self.create_staff_user email, password
      user = User.find_or_create_by email: email, password: password
      user.add_role :staff
    end

    def self.create_user_with_invite email, password
      user = User.find_or_create_by email: email, password: password
      user.send_invitation
    end
  end

Non-AR models

Models also can get bloated with controller-like actions that have groups of methods that should be broken out into their own classes that the controllers will leverage. An example of this type of class would be one that manages to send an email to a user. These models don’t extend active record, because they focus is performing business logic on active_record models, not storing them in the database. Models take in a limited scope of the Params of a request, perform their function, and then return their results to the controller.

Before breaking out email sending, active record models would end up with a collection of email sending methods (send_password_reset, send_notification, etc). All of these should be moved to a model classes because classes have one purpose and active record models are for Savin data, not sending emails.

An example of this could be:

user_invitor.rb
1
2
3
4
5
6
  class UserInvitor
    def invite_email email
      Invitation.create email: email
      UserMailer.invite_email(email).deliver_later
    end
  end