Mockra

This is the blog of David Ratajczak, a Ruby developer.

Jtv 1.0.0 Release


I just released version 1 of my Justin.tv API wrapper. It includes support for the main endpoints I’ve been using, and a much cleaner API than the original release. If you want access to a portion of the API I’m not currently supporting, please send a feature request.

You can check out the documentation here.

Ember Rake


I’ve been finding myself updating my ember build pretty frequently, so I decided to write a small rake task for updating to the latest build.

The script will clone the ember and ember-data repositories to ~/.ember and ~/.ember-data. The files are then placed in vendor/assets/javascripts in relation to your current directory.

The script copies the unminified versions, so you’ll need to alter it if you want the minified builds.

You can find the script here.

Jtv Update


I just released a beta version of the Jtv gem that features a much cleaner interface. Feel free to check it out, here.

Struct to Hash


I’ve been using Structs a lot lately, and recently needed to convert a Struct to a Hash. It’s a very simple process, and something that I’m sure I’ll use often.

Article = Struct.new :title, :text

essay = Article.new 'Poem', 'Roses are Red'
# #<struct Article title="Poem", text="Roses are Red">

Hash[essay.each_pair.to_a]
# {:title=>"Poem", :text=>"Roses are Red"}

Another cool thing I picked up was how to use map with indexes.

articles.each_with_index.map {
  |article, index| article.title = title_array[index]
}

Github Stories


I’ve been playing around with Clojure for the last couple of days, and built a small web application that displays a repository’s commit messages. It’s a really simple app, but was a lot of fun to make. You can check out the app here: github_stories.

The syntax is going to take a while to get used to, but there are a lot of things I like about the language. One of the best features has to be leiningen, which is a great tool for managing clojure dependencies. Creating a noir application was as simple as running:

lein new noir github-stories

Leiningen will even install clojure for you, just install the script found here. I’m looking forward to picking up a book on clojure in the near future and learning the language. I’ve heard a lot of good things about functional languages and the benefits of learning one of them.

ERB Email Templates


I was playing around with ERB the other day and ended up designing a really simple e-mail system with templates. Here’s the system I created.

class Email
  def deliver
    EmailApi.new(email_options).deliver
  end

  def template
    File.open "templates/#{handle}.erb"
  end

  def message
    ERB.new(template.read).result(binding)
  end

  def handle
    self.class.to_s.downcase
  end
end

class SignupEmail < Email
  attr_accessor :user
  def initialize user
    @user = user
  end

  def email_options
    { :body => message,
      :to => user.email,
      :subject => subject
    }
  end

  def subject
    "Welcome #{user.name}"
  end
end

In order to send a sign up email to a new user, you would call:

SignupEmail.new(user).deliver

EmailApi would represent the service you’re using to deliver e-mails in your application. The body of the e-mail would be created from the signupemail.erb file in the templates directory.

By passing binding to ERB, you’re able to access the @user variable in your template.