My First Interview


After my first developer interview, I had an “inspiration” to make a toy blog app to see if I could implement a News Feed feature where the News Feed would display the last six “things” I did.

Here are a few specs I think I remember:

To save time I stole bits of this guys implementation (Leonard Garvey) of a Rails Blog in 15 minutes app.

Then, I had to do some sleuthing for a Twitter Rails gem. I found this and it was super cool to use.

Man! I learned a whole lot just playing with this application.

  def self.query_stale?
    # expire @query every five minutes
    @query_refreshed_at ||= Time.now
    @objects.blank? || (Time.now - @query_refreshed_at > 5.minutes)
  end

Things to think of for next steps

Actual Code (for your convenience)

Here is the main bread and butter of the NewsBundle

# file at: app/models/news_bundle.rb
class NewsItem
  attr_accessor :news_object
  def initialize(object)
    @news_object = object
  end

  def object_text
    if self.tweet?
      news_object.text
    else
      news_object.body
    end
  end

  def type
    if tweet?
      "Tweet"
    else
      "Blog Post"
    end
  end

  def tweet?
    self.class == Twitter::Tweet
  end

  def class
    news_object.class
  end

  def created_at
    news_object.created_at.to_datetime.utc.to_formatted_s(:long)
  end
end

class NewsBundle < NewsItem
  def self.get_objects
    if query_stale?
      @objects =  tweet_objects
      @objects += Post.last(6)
    end

    sorted_and_mapped
  end

  def self.query_refreshed_at
    @query_refreshed_at
  end

  def self.query_stale?
    # expire @query every five minutes
    @query_refreshed_at ||= Time.now
    @objects.blank? || (Time.now - @query_refreshed_at > 5.minutes)
  end

  def self.tweet_objects
    tweets = Twitter::REST::Client.new do |config|
      config.consumer_key        = ENV['TWITTER_CONSUMER_KEY']
      config.consumer_secret     = ENV['TWITTER_CONSUMER_SECRET_KEY']
      config.access_token        = ENV['TWITTER_OAUTH_TOKEN']
      config.access_token_secret = ENV['TWITTER_OAUTH_TOKEN_SECRET']
    end

    tweets.user_timeline("Jason_Data")
  end

  def self.sorted_and_mapped
    @objects.map do |object|
      NewsItem.new(object)
    end.sort_by {|obj| obj.created_at }.reverse[0..6]
  end
end

The controller side of things

I called the NewsBundle.get_objects from the ‘posts#index’ action

# file at: app/controllers/posts_controller.rb

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts        = Post.all
    @news_objects = NewsBundle.get_objects
    @query_time   = NewsBundle.query_refreshed_at
  end

  # template stuff omitted

end

And finally the view

# file at: app/views/posts/index.html.erb

  # template stuff omitted AGAIN! Muhaha

<h1>News Feed</h1>

<h3>Queried Fresh At: <%= @query_time %></h3>

<table>
  <thead>
    <tr>
      <th>
        <%= "Date and Time" %>
      </th>
      <th>
        <%= "Type of Article" %>
      </th>
      <th>
        <%= "Text" %>
      </th>
    </tr>
  </thead>

  <%- @news_objects.each do |news_object| %>
    <tbody>
      <tr>
        <td>
          <%= news_object.created_at %>
        </td>
        <td>
          <%= news_object.type %>
        </td>
        <td>
          <%= news_object.object_text %>
        </td>
      </tr>
    </tbody>
  <% end %>
</table>

In conlusion, I’m going to bed!

All in all, this was a great experience and I’ve learned a lot from taking something from idea to implementation. Getting my first interview was a great experience and I’m sure there will be other opportunities for me to stretch myself and show progress. If you have any questions or comments, leave them below or email me at:

Jason.Data@roadbytes.me