Recent Posts

RubyConf - 3 Things I'm Looking Forward To

Posted on 30 Oct 2012

This post was originally published on the SmartLogic Blog.

As Hurricane Sandy pummels the East Coast, I’m crossing my fingers that I’ll still be able to get out of Baltimore to head to RubyConf in Denver. This is the first big conference I’ve been to that’s not RailsConf, and I’m looking forward to the new perspectives and knowledge I’ll gain — weather willing.

3 reasons why I'm looking forward to RubyConf 2012

Here’s three things I’m particularly excited about, besides being dry:

Learning About the Wonderful World of Ruby from Matz

This one’s a no-brainer. I’m pumped to hear from the creator of Ruby, and maybe get a sneak peek of what’s coming with Ruby 2.0.

Focusing on Ruby in Sessions

When I went to RailsConf, it was all Rails sessions, with nothing focusing just on Ruby. I’m excited to focus exclusively on Ruby, with, of course, some Rails and other web stuff sprinkled in. There’s one about dRuby (distributed Ruby) that I’m particularly interested in.

Hearing about Scalable Web Architectures

There’s two sessions related to scalable web architectures that I’m looking forward to, because I’m working with apps using scalable web architectures every day. First, I hope Chris Hunt from Square, who’s covering Service Oriented Architecture (SOA) at Square, mentions rspec_api_documentation when he covers API documentation! But really, I’m also interested to hear his perspective on Square’s approach to SOA. Second, I am interested in learning more about Cross Origin Resource Sharing (CORS) from Michael Bleigh, co-founder of Divshot. This isn’t directly related to what I’m doing at work right now, but I’m looking forward to bringing takeaways home to our team.

Are you headed to RubyConf? What are you looking forward to? Comment below, or tweet me @ericoestrich.

Three Vim Tips

Posted on 25 Oct 2012

As a ruby programmer, my editor of choice is vim. Here are some cool tips I used earlier this week.

Macros are in registers

One of the cool things about macros is that when you record a macro you assign it to a register, eg qq starts a recording a macro to the q register. Once you finish recording the macro you can then paste the macro out and edit it, eg "qp will paste the q register and "qy will yank the selected text into the q register. A handy trick to have when you messed up one thing in a macro.

Recursive Macros

During the SLS internal conference (slides here), Paul gave a presentation on vim tips. One of them was a recurisve macro. Using the tip above you can append @q to the end of the macro and make it be recursive.

I used this tip to delete a column out of a csv file.

WARNING!

Make sure you have the macro go down a line at the end of it, otherwise vim won’t hit the end of the file and stop the macro.

An empty search will use the previous item

When testing out a search I was going to use to mass replace text in vim I used / first. It ended up being a pretty complex regex and didn’t want to copy and paste from my terminal, surely vim could do better than that. Turns out that it can. If you search for something complex via /, when you do a search and replace you can just leave the search section empty and vim will use the complex search you already have. Eg: %s//new text/.

This tip was also courtesy of Paul.

cherry-pick'ing From Another Repo

Posted on 17 Oct 2012

Cherry picking

We’re in the process of splitting out a fairly large rails app that contains very clear “mini” apps. Each “mini” app will get their own full app and of course their own git repo.

As we split out the multiple apps, some commits were bound to be missed because others continued working in the main repo. After doing a bit of searching I found that there was a nice way to import these commits via cherry-pick.


git remote add other-repo ../other-repo/
git fetch other-repo
git cherry-pick cf0dbfe3c63ab0f49bb8fa97d3b28bb0bd9eb896

I’ve ended up using this trick several times this week and it’s turned out to be an excellent time saver.

Resources
  1. StackOverflow
  2. git-cherry-pick
  3. Photo by InGale2005

Abusing ActiveModel::Serializers for HAL

Posted on 11 Oct 2012

For my recent APIs I’ve used JSON HAL as the media type and ActiveModel::Serializers as an alternative to #as_json. It’s worked out fairly well except I’ve had to bend backwards in order to get ActiveModel::Serializers to spit out HAL.

Below are some common things I’ve had to use. You can check out an example here.

Disable Root

This one is pretty easy, we just need to not have the element type be the root key.

class OrderSerializer < ActiveModel::Serializer
  root false
end

Embedded Resources

Since HAL resources have other resources in the “_embedded” key, we need to make sure collections show up there.

class OrderSerializer < ActiveModel::Serializer
  has_many :items

  def as_json(*args)
    hash = super

    hash[:_embedded] = { :items => hash.delete(:items) }

    hash
  end
end

Adding links is pretty easy with ActiveModel::Serializers, just add a new attribute. One thing to watch out for, the collection serializers don’t have access to url helpers.

class OrderSerializer < ActiveModel::Serializer
  attributes :_links

  def _links
    {
      :self => {
        :href => orders_url(order)
      }
    }
  end
end

Slimming Down Serializers

I wanted to use the same serializer for both an index and a show resource, but they didn’t show exactly the same amount of information. I fixed this by sending in an extra option on the show page: respond_with order, :expand => true.

class OrderSerializer < ActiveModel::Serializer
  attributes :email, :extra_info

  has_many :items

  def include_extra_info?
    @options[:expand]
  end

  def include_associations!
    if @options[:expand]
      include! :items
    end
  end
end

Creating a Faraday middleware

Posted on 04 Oct 2012

I’ve been playing around with Faraday recently and wanted to make a middleware. The guide on the wiki leave a little to be desired. Googling didn’t seem to help much either. I eventually found a Faraday Middleware github repo that helped me.

Here is a sample middleware that I ended up using:

class MyMiddleware < Faraday::Middleware
  def call(env)
    env[:request_headers]["My-Custom-Header"] = "my-custom-value"

    @app.call(env)
  end
end

connection = Faraday.new("http://google.com/") do |faraday|
  faraday.use MyMiddleware
  faraday.adapter Faraday.default_adapter
end
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.