Trinity As Seen By A Programmer

Any thoughts on the following: Trinity As Seen By A Programmer?


class God
  # John 1:3
  def can_know_everything?
    true
  end

  # Matthew 19:26
  def can_do_everything?
    true
  end

  # Isaiah 55:8
  def is_like_a_human?
    false
  end
end

class Father < God
end

class Son < God
  # Mark 13:32
  def can_know_everything?
    false
  end

  # Hebrews 2:18
  def is_like_a_human?
    true
  end
end

class HolySpirit < God
end

TeleSMS gem released

The TeleSMS gem was recently released. This gem helps with sending and receiving SMS messages using emails. For example, Verizon has an email-to-sms gateway that allows users to send SMS messages by sending an email to NUMBER@vtext.com.

TeleSMS also encapsulates the logic for receiving SMS messages. Since there are many cases of incorrectly formatted messages, TeleSMS will attempt to return a nicely formatted message along with its metadata.

To send a message, you can do the following:


Telesms::Outgoing.deliver('john@example.com', '555555555', 'Verizon', 'Message body')

To receive a message, do the following:


Telesms::Incoming.receive(params[:mail])
# => { from: '555555555', to: 'john@example.com', body: 'Message body', provider: 'Verizon' }

If you have any suggestions or improvements, please let me know or create a pull request on Github.

Telefio uses the TeleSMS gem.

Proof that God is a programmer

Joke of the day: Proof that God is a programmer

“So shall my word be that goeth forth out of my mouth: it shall not return unto me void, but it shall accomplish that which I please, and it shall prosper in the thing whereto I sent it.” – Isaiah 55:11


void a_void_function() {}

Youtube Live & JW Player – Custom Playlist

Youtube recently allowed regular users to create live events using Youtube Live. It seems like a great service if you are looking for an affordable (Youtube is currently free) streaming service for church or whatever else you want to do.

I couldn’t find anything that would fit my needs so I decided to try Youtube Live and create a custom playlist for JW Player. The playlist displays an upcoming event (the next one in sequence) at the top and recorded videos in a playlist under that.

Here is the JW Player config:


<div id="player">Player</div>

<script src="http://jwpsrv.com/library/YOUR_JW_KEY.js"></script>
<script>
    jwplayer("player").setup({
        primary: "flash",
        aspectratio: "16:9",
        height: 354,
        width: 860,
        listbar: {
           position: "right",
           size: 230,
           layout: "extended"
        },
        playlist: "playlist.php"
    });
</script>

And the playlist that is being generated in playlist.php:


<?php
$youtube_user_id = "" // Can be found in your youtube account settings;
$youtube_playlist_id = "" // View the playlist in the browser and get the ID (starts with PL);

header('Content-type: application/xml');
date_default_timezone_set('America/New_York');
echo "<rss version=\"2.0\" xmlns:jwplayer=\"http://rss.jwpcdn.com/\">";

$live_event_ids = array();

function sort_by_date( $a, $b ) {
    return $a["date"] - $b["date"];
}

function upcoming_live_event() {
  global $live_event_ids;

  $feed = "http://gdata.youtube.com/feeds/api/users/" . $GLOBALS['youtube_user_id'] . "/live/events?v=2";
  $feed = file_get_contents($feed);
  $feed = str_replace('<yt:when', '<ytwhen', $feed);

  $rss = simplexml_load_string($feed);

  $live_events = array();

  foreach ($rss->entry as $item) {
    preg_match("/live\/videos\/(.*)\?v/", $item->content['src'], $matches);
    $start_time = $item->ytwhen['start'];
    $end_time = $item->ytwhen['end'];
  
    if (strtotime($end_time) > time()) {
      $live_events[] = array(
        'id' => $matches[1],
        'title' => $item->title,
        'date' => strtotime($start_time)
      );
      $live_event_ids[] = $matches[1];
    }
  }

  usort($live_events, "sort_by_date");
  
  return $live_events[0];
}

function recorded_videos() {
  global $live_event_ids;
  
  $feed = "http://gdata.youtube.com/feeds/api/playlists/" . $GLOBALS['youtube_playlist_id'] . "?alt=rss";
  $feed = file_get_contents($feed);

  $rss = simplexml_load_string($feed);
  
  $videos = array();
  
  foreach ($rss->channel->item as $item) {
    $link = str_replace('&feature=youtube_gdata', '', $item->link);
    preg_match("/\?v\=(.*)$/", $link, $matched);

    if (in_array($matched[1], $live_event_ids)) {
      continue;
    }
    
    $videos[]= array(
      'id' => $matched[1],
      'title' => $item->title,
      'date' => strtotime($item->pubDate),
      'description' => $item->description,
      'date' => ''
    );
  }
  
  usort($videos, "sort_by_date");
  
  return array_reverse($videos);
}

echo "<channel>";

$live_event = upcoming_live_event();

if (isset($live_event)) {
  echo "<item>";
  echo "<title>Live Video</title>";
  echo "<description><![CDATA[" . date("l, M jS", $live_event['date']) . "
" . date("g A", $live_event['date']) . " EST]]></description>"; echo "<jwplayer:image>YOUR_IMAGE.JPG</jwplayer:image>"; echo "<jwplayer:source type=\"youtube\" file=\"http://youtube.com/watch?v=" . $live_event['id'] . "\"/>"; echo "</item>"; } foreach (recorded_videos() as $video) { echo "<item>"; echo "<title>" . $video['title'] . "</title>"; echo "<description>" . $video['description'] . "</description>"; echo "<jwplayer:source type=\"youtube\" file=\"http://youtube.com/watch?v=" . $video['id'] . "\"/>"; echo "</item>"; } echo "</channel>"; echo "</rss>"; ?>

Hope this helps someone.

Passing custom attributes to mail observer

I was trying to figure out how to pass custom attributes to a Ruby on Rails mail observer (should be the same for an interceptor) and couldn’t find any documentation about it. Since the observer only receives the Mail object as a parameter in


self.delivered_email

method, we will need to pass the custom attributes through the mail object. One way to do is as follows:


mail(
  :to =>'email@sample.com',
  :custom_attr_1 => 'This is my custom string'
)

And you can receive it in the observer with the following:


def self.delivered_email(mail)
  puts mail.header[:custom_attr_1].value
end

If you have an interceptor, use the following:


def self.delivering_email(mail)
  puts mail.header[:custom_attr_1].value
end

I hope this helps someone.

Strong Parameters – Restricting Collection

Recently I was trying to find a way for strong parameters to restrict a collection to the items a user owns but I could not find anything so I decided to do something myself.

I want a Category to accept an array of member_ids but I want them to be only the members the user owns (preventing a user from assigning someone else’s members).

In the controller we expose the members and the category and only use the organization’s members:


class CategoriesController < ApplicationController
  expose(:organization)

  expose(:members) { organization.members }

  expose(:category, attributes: :category_params)

  def new
  end

  def create
    category.members &= members
    
    if category.save
      redirect_to category_path
    else
      render :new
    end
  end

private

  def category_params
    params.require(:category).permit(:name, {:member_ids => []})
  end
end

And now the category will only contain members that the organization (or user) owns. It may not be the most efficient way of doing this so if you have any suggestions please leave a comment.

LiteShops – Create a Shop in 30 Seconds

liteshops_logo
Over the weekend four of us worked on a Rails Rumble application for the annual Ruby on Rails competition. We decided to create an application that would help with selling things online easily. It is called Lite Shops.

To post a product online, simply:

  1. Take a picture(s) of your item.
  2. Email the picture(s) to sell@liteshops.com with the name as the subject (add a price with a dollar symbol, e.g. $20) and description as the body.
  3. LiteShops will do the rest: Create a shop for you and post your item on LiteShops.com.

Give it a try and tell us what you think. Also, don’t forget to vote/favorite this application at Rails Rumble.

Christians and Computer Programming

I’ve been thinking about writing a blog entry about this topic but never could get to it. What does the Bible say about computer programming? Can Christians be programmers? If yes, how should we be writing code?

This is a very interesting topic and many developers out there who are Christians are wondering what the Bible says about programming. I will attempt to bring up a few verses from the Scriptures that will help us understand what and how we should be coding.

Do Everything For The Lord

The Bible clearly states that we should be doing everything for God’s glory – whatever it may be.

“And whatsoever ye do in word or code, do all in the name of the Lord Jesus, giving thanks to God and the Father by him.” – Colossians 3:17

Notice how I substituted the word “deed” with “code”. Deeds are the works that we do and for us programmers that means our coding. We should be striving to do everything in the name of Jesus – all for His Glory.

Give Our Best to God

Let’s say that you were making something for your wife. Would you do it carelessly without paying attention to details? No. You would sit down, plan and work on it careful so it turns out very good. How much more we should be striving to give our best to God when writing code!

“And whatsoever ye do, do it heartily, as to the Lord, and not unto men; Knowing that of the Lord ye shall receive the reward of the inheritance: for ye serve the Lord Christ.” – Colossians 3:23-24

We need to be writing code “heartily” – as to the Lord. We are not trying to impress men. We are trying to praise and serve our God with our talents.

Excel At What We Do

Giving our best can only go so far. When you don’t have much you can’t give much. This means that we need to be constantly growing and improving ourselves and what we do.

“Do you see a man who excels in his work? He will stand before kings; He will not stand before unknown men.” – Proverbs 22:29

The majority of people deceive themselves into thinking that maturity and growth will come with time. That is a big myth. We need to intentionally work on ourselves. We need to research, experiment and improve what we are good at. God gave us a brain for a reason, He wants us to use it wisely and grow in our strengths.

So let us go serve the Lord with the talents He gave us and daily working on ourselves and our skills to give only the best to God.