badpopcorn

Send your emails using Postmark

Written by Corey on April 12, 2010 | Comments Off



Wildbit recently launched a great new email delivery service named Postmark.  Once I read what it was about I knew that we had to integrate in to beyond the whiteboard.

First a little background.

Sending emails from your web application is one of those things that all the programming guides show you how to set up and get going in a few easy steps.  However, they never point out the problems that you might come across while trying to send out emails.

When launched the beta version of beyond the whiteboard we setup Gmail to be our smarthost for sending emails.  That was pretty easy to get going and worked out pretty well, there where copies of the sent emails in that Gmail account and emails where sent out.  However, there where a couple of things which I didn’t really like.  First off Gmail, replaces the from field with the user you are sending email through.  For example, if your Gmail user is ‘mycoolapp’, that’s what the from will be, even if you in your application set the from to be something else like ’support’.  A minor little issue, but we wanted some emails to be from the application and others be from individuals which just wasn’t possible.  Lastly, Gmail limits the number of emails that you can send through it in a given period.  This was the big killer for us, we hit limit and didn’t know it right away.  That cause us some headaches because there really wasn’t a good way to know if we where close to the limit or what.

After the few Gmail incidents I finally broke down and decided to set up an SMTP server to send out our application emails.  I really didn’t want to because, well email is kind of scary to me.  The server configurations are very complex and they have warnings everywhere that if you make a mistake every spammer in the world will use your server.  Then if that happens every ISP will hate you and little kids will point and laugh when you go to the mall.  Despite all that I did manage to get a configuration going and we didn’t let any evil spammers or anything bad like that.  Everything appeared to be going well, or so I thought.

You see, I setup our SMTP to only send out emails, the receiving part was still handled by Gmail.  I hadn’t keeping tabs on the Gmail inbox of the address we where sending from in some time.  When I did happen to remember to look in that account, several months later, I discovered that EarthLink and AT&T had decided that we where spammers and blocked all emails from our server.  What a drag that was, I had no clue how long that had been going on, and it wasn’t a very good feeling to be labeled a spammer, even if it was from an automated system.  They have methods to get you removed from list and I filled out their forms, but it happened at least one other time.  That’s when I decided that there had to be something better out there and that’s when I found Postmark.

Why Postmark is good.

Right when I read the description of the Postmark service I signed up, it was an easy decision for me.  Here’s how it works, you post your email to Postmark through their HTTP API and then they send your email to it’s destination.  See super simple, but wait there’s more, they have a nice overview with a graph of how many emails you’ve sent, how many bounced, and how many where marked as spam.  You can then dig deeper and see which emails bounced, why they bounce, and there a way to reactivate those emails if you think there was a mistake.

The integration was also pretty easy, they have published a Ruby gem that hooks right in to ActionMailer.  After a couple lines of configuration you can just use ActionMailer just like before and instead of sending emails via SMTP, they are sent to Postmark for delivery.  I also really liked the way the set it up by changing ActionMailer’s delivery method.  Why is that good?  Well, different ActionMailer subclasses can have different delivery methods, which is completely different from the SMTP settings which are shared across all instances.  That makes it possible to have emails sent to users got through Postmark, while system emails, like error reports can go through SMTP.  Which is exactly how we have it set up today.

I’m really happy with the service, now I know exactly how many emails to users we are sending out each day and how many are bouncing back.  Even though Postmark just launched a few weeks ago I know that the folks at Wildbit are going to continue to make it better because they use it with their other web applications. Personally I’m really looking forward to when they get some sort of webhooks going for bounced emails.  I would like to inform our users that we couldn’t send them an email at the address they have given us.

All of that totally justifies the cost of the Postmark service, which is currently $1.50 for 1,000 emails.  If you are building a web application and are sending out transactional emails to your users, I would really encourage you to investigate a service like Postmark.  Why worry about emails when you should be worrying about your application’s main features.

User tags from Hulu.com for the Tonight Show Page

Written by Moe on April 1, 2010 | Comments Off

You have to love tagging and user generated content.


leno_hulu

How an AD can ruin design

Written by Moe on January 27, 2010 | 1 Comment

Should design and good user interface be sacrificed for optimal ad viewing? Not in my opinion. CNN, compared to other big news media, has a pretty good site design. However, their new pulse page is ruined by horrible ad placement. In a way the design was successful, because I noticed the ad. However, I noticed it like a teenager notices a pimple on their face. I was disgusted, confused and worried it may leave a scar. I’m seeing this more and more online, and it’s getting more and more annoying. Insert Seth Godin quotes here.

Adding Memory To Your Mac

Written by Moe on November 8, 2009 | Comments Off

mac_memory

You need a small screwdriver.

Affion Crockett, Great Example of Online Branding/Marketing

Written by Moe on October 17, 2009 | Comments Off

Who is Affion Crockett?

Before this video I thought he was just a funny guy on Wild’n Out. I now know he is also a musician, oskamill, and was recently in the movie Dance Flick. Affion is using the internet to show off his versatility as an entertainer. Best of all, he is slowly eliminating the middle man (studios). He has over 47,000 subscribers on youtube (his latest video has about a million views as of now), over 7000 friends on myspace, about 7000 followers on twitter, and has 5000 friends on facebook ( i couldn’t find a real fan page). He has a, almost instant, connection to about 70,000 fans. Fans who will most likely share, retweet and email anything he puts in his feeds.

Using Yaml to stream lots of data

Written by Ben on September 24, 2009 | Comments Off

Most people just use Yaml for configuration. The prime example is the database.yml file found in almost every single Rails app. But here’s a way to take advantage of Yaml in Ruby as a way to serialize and transfer lots of separate records from one place to another.

The standard yaml library found in Ruby is all you need. First, to serialize the target set of records. For our example, we’re just going to stream to a file, but one can use any IO stream. And instead of streaming an indeterminate number of records, we’re going to bound this process with a fixed number of records.

require ‘yaml’
file = File.open(‘test.yml’, ‘w’)
for x in 0..100
hash = {
‘id’ => x,
‘field1′ => “somedata”,
‘field2′ => “someother data”,
‘option’ => true
}
file.write hash.to_yaml
end
file.close

Notice that all we needed to do is call the #to_yaml method to do the serialization. It also handles adds the Yaml document start character sequence automatically (i.e. “—”, three dashes alone on a new line).

To read that stream of data:

File.open(‘test.yml’, ‘r’) do |io|
YAML.each_document(io) do |record|
# Do something here
puts record.inspect
end
end

Question: What’s the advantage here?

Answer: Constant memory footprint. We only read in as much as we need to handle the next Yaml document (object, hash). So if you’re processing a 2 Gig file, the process won’t try to load the entire file into memory at once. Or you may not actually know how many records will come over the wire.

Some Uses:

  • Mass dumping data from one system to another where the source system lacks write permissions, or even network access, to the target system.
  • Data Archival in a common format. Yaml is accessible by tools without the need for a SQL database… if one archived everything in SQL dumps. And this lets you keep rows clustered in manageable sized files.
  • Stream real-time updates from one system to another. Either HTTP post a batch document stream to a Webhook, or just read and write on raw TCP sockets.

Can facebook predict if your girlfriend will break up with you?

Written by Moe on May 21, 2009 | Comments Off

The Wall Street journal has an interesting article on yet another innovative algorithm Google is working on. Venture beat summed up the article well; “In the wake of recent brain drain, the search giant has devised an algorithm that combines employee reviews, promotion histories, pay and other factors to predict which employees are most likely to leave.” Right after I read this article I found myself on the facebook “Suggestions” page, or as I like to call it, “The People I Don’t Know” page. I came up with ideas and decided to post my notes.

    Location

  • Where you should live. Based on the amount of people you interact with the most and their current locations.
  • Places you should avoid. Do the exact opposite of the logic above.
  • Music, Television, Hobbies etc
  • Display the top among your friends, and sort by the friends you interact with the most.
  • Friendships
  • Facebook needs to clarify why you should be friends with the people they suggest. Breaking the list into categories of; people who share the same type of information as you do, people who join and discuss the same type of topics you do, People who attend the same type of events as you do, and people that have things in common (profile details) as you do.
  • Relationships (for fun)
  • Your girlfriend/boyfriend will most likely break-up-with/cheat on you with “insert name here” in the next three to four weeks.
  • A “you could probably hit it” section.
  • A he’s just not that into you list.
  • The variables I would use to figure out the feelings/attitude person A has towards person B.

    • 1. The difference between the the average number of wall posts(per friend) person A has posted, and the number of wall posts person A has posted on person B’s wall.
    • 2. The difference between the the average time it takes person A to respond to a wall post and the average time it takes person A to post on person B’s wall.
    • 3. The average time person A spends on person B’s profile compared to person A’s average among their friends.
    • 4. Use 1 and 2 but for messages instead of wall posts.
    • 5. Use 1 and 2 but for photos tagged.
    • 6. Use 1 and 2 but compare the results, if possible, to person’s A last’s relationship during the time the relationship was active.
    • 7. Compare wall posts, messages, other activity of current couples, prerelationship, with the current posts, messages and other activity of person A and B.

Mysql Gem for Ruby Enterprise Edition on Mac OSX

Written by Ben on April 17, 2009 | Comments Off

I just recently installed Ruby Enterprise Edition on my Mac, but found the following errors:

ERROR: Error installing mysql:
ERROR: Failed to build gem native extension

The reason was that the Mysql files are installed under version specific directories and files when installing from Macports. For example, mysql5 or /opt/local/lib/mysql5. This messes up the mysql gem installation because it looks elsewhere by default. The solution is to specify the exact mysql config location in the installation process:

bash-3.2# /opt/ruby-enterprise-1.8.6-20090201/bin/ruby \
/opt/ruby-enterprise-1.8.6-20090201/bin/gem install mysql \
— –with-mysql-config=/opt/local/bin/mysql_config5

That last line is two dashes (-) followed by the –with-mysql-config option.

Easy as pie.

Google Voice, First Impression

Written by Moe on April 10, 2009 | Comments Off

Our Grand Central account magically turned into our Google Voice account the other day, and I couldn’t be more pleased. Google took the robust look of Grand Central and made it look as simple as Gmail. I can easily browse call history, send a sms or update our voicemail. I’m really excited to see how well the voicemail transcript will work also, and if it will be offered with recorded calls. The most popular feature might just end up being the call widget; “You can allow others to call you from your website or blog by adding a call widget to it. Visitors to the website can click the widget, enter their phone number, and Google Voice will call them and connect the call to your Google number.”

You can also call out of the country with competitive VOIP rates. Wait a minute…Google might generate a large revenue source other then ads!?

How to Quickstart Merb Slice Development

Written by Ben on November 2, 2008 | 6 Comments

What are Merb Slices?

Merb Slices are a kind of mini Merb Application that can be packaged up as gems and used as is (or with customizations) within actual Merb Applications. They are full Model-View-Controller stacks to support a large feature within a larger application. Examples could be a full blogging system, user management system or a file upload system.

Where else can I find good overview information about Merb Slices?

There are a few places, but you should start with the MerbCamp 2008 MerbSlices talk by Daniel Neighman aka hassox. His slides are found here and here. Watch the other MerbCamp videos for more Merb info.

So what does this article cover?

This just provides some missing details for a developer to immediately get a slice working. The above material gives a great overview of slices in general and the why/how to use/install them in main Merb applications.

Let’s start by creating our slice.

$ merb-gen slice myslice
Generating with slice generator:
     [ADDED]  app/controllers/application.rb
     [ADDED]  app/controllers/main.rb
     [ADDED]  app/helpers/application_helper.rb
     [ADDED]  app/views/layout/myslice.html.erb
     [ADDED]  app/views/main/index.html.erb
     [ADDED]  config/init.rb
     [ADDED]  config/router.rb
     [ADDED]  lib/myslice.rb
     [ADDED]  Rakefile
     [ADDED]  README
     [ADDED]  spec/myslice_spec.rb
     [ADDED]  spec/controllers/main_spec.rb
     [ADDED]  spec/spec_helper.rb
     [ADDED]  stubs/app/controllers/application.rb
     [ADDED]  stubs/app/controllers/main.rb
     [ADDED]  TODO
     [ADDED]  public/javascripts/master.js
     [ADDED]  public/stylesheets/master.css
     [ADDED]  LICENSE
     [ADDED]  lib/myslice/merbtasks.rb
     [ADDED]  lib/myslice/slicetasks.rb
     [ADDED]  lib/myslice/spectasks.rb
$ cd myslice

Our goal here is to set up our slice so we can actually do development without needing to install it within a Merb application, and to give an example run through of creating a resource.

Let’s start by editing the slice’s init.rb file. This file is solely used in your slice development cycle; it is omitted from the final packaged gem; it is NOT used in production. If you look at the slice’s Rakefile, you will see that NO file in the config/ directory is included in the gem.

$ vi config/init.rb
# Add the following to the top of the slice's config/init.rb file.
# USE THE CORRECT GEM VERSIONS.
merb_gems_version = "0.9.12"
dm_gems_version   = "0.9.6"

# Uncomment the following two lines to develop with haml instead of erb.
# dependency "merb-haml", merb_gems_version
# use_template_engine :haml

dependency "dm-core", dm_gems_version
dependency "dm-aggregates", dm_gems_version
dependency "dm-migrations", dm_gems_version
dependency "dm-timestamps", dm_gems_version
dependency "dm-types", dm_gems_version
dependency "dm-validations", dm_gems_version

use_orm :datamapper

What we did above is to declare a dependency on the DataMapper ORM. You can use whatever ORM you wish, but I’ll be using DataMapper as the example in this article.

Also, I have included commented lines to show how one would use Haml instead of Erb as the templating engine. I highly suggest that developers write views for BOTH Erb and Haml when developing slices. This gives the users of such slices a choice.

Since we’re editing files in the config/ directory, we’ll go ahead and create the database.yml file we’ll need.

$ vi config/database.yml
# This is a sample database file for the DataMapper ORM
development: &defaults
  # These are the settings for repository :default
  adapter:  sqlite3
  database: sample_development.db

Next, we go ahead and try creating a resource as most developers will be doing. It’s just the same merb-gen command as one would do for any regular Merb application.

$ merb-gen resource article
     [ADDED]  spec/models/article_spec.rb
     [ADDED]  app/models/article.rb
     [ADDED]  spec/requests/articles_spec.rb
     [ADDED]  app/controllers/articles.rb
     [ADDED]  app/views/articles/index.html.erb
     [ADDED]  app/views/articles/show.html.erb
     [ADDED]  app/views/articles/edit.html.erb
     [ADDED]  app/views/articles/new.html.erb
     [ADDED]  app/helpers/articles_helper.rb

When we edit the articles controller, we’ll see that it looks exactly like a generated resource that one would get in a Merb application. In fact that’s probably the whole point since we want our slices to be full MVC stacks to implement our subsystem features. But if we tried to use this resource right now, we’ll find that our slice just won’t work. The main reason is how we define a slice’s controller versus a controller in a full Merb application. The whole issue is about namespacing.

$ vi app/controllers/articles.rb

We need to change the following class declaration:

class Articles < Application

to

class Myslice::Articles < Myslice::Application

This article class needs to inherit from the slice’s application class instead of whatever Merb app it is installed in. And the Articles class needs to be in the Myslice namespace so the slice router rules will actually be able to find the class.

The rest of the controller looks good. It’s got all the default DataMapper access code for its methods. NOTE: If one did not call “use_orm :datamapper” in the slice’s init.rb file, then all this ORM access code will be omitted; one would have a plain class whose methods just called `render`.

As a next step, one would generally verify that merb-gen would have updated the config/router.rb file with this new resource. WARNING! The config/router.rb file is NOT where routes are configured for slices. Everything is done in lib/myslice.rb, or whatever it is named in your real slice. In fact, this is also where we’ll find other configuration options.

$ vi lib/myslice.rb

Let’s go ahead and update our slice meta data. Replace the following code with your own stuff.

# All Slice code is expected to be namespaced inside a module
  module Myslice

    # Slice metadata
    self.description = "Myslice is a chunky Merb slice!"
    self.version = "0.0.1"
    self.author = "Engine Yard"

I won’t cover the other slice hooks in this file except for the “def self.setup_router(scope)” method. This is where you SHOULD to add your resource. Although the scope.default_routes line will correctly route to your resource, I find it cleaner to explicitly declare the slice’s routes. Watch the video mentioned above to understand why we setup routes in this hook instead of a slice’s router.rb file.

def self.setup_router(scope)
      # Add the following resource line
      scope.resources :articles
      # The lines that follow are the pre-generated ones.

      scope.match('/index(.:format)').to(:controller => 'main', :action => 'index').name(:index)
      # the slice is mounted at /myslice - note that it comes before default_routes
      scope.match('/').to(:controller => 'main', :action => 'index').name(:home)
      # enable slice-level default routes by default
      scope.default_routes
    end

I personally would delete the “scope.default_routes” line because I’m all about explicitly specifying routes.

At this point, we’ve got routes and a fixed up controller.

Now we look at the Article model. This model contains the code required to define it as a DataMapper resource.

$ cat app/models/article.rb
class Article
  include DataMapper::Resource

  property :id, Serial
end

Again, as with controllers, the DataMapper code would have been omitted without the “use_orm :datamapper” in the slice’s config/init.rb file. We would have had an empty class.

I will skip over how we develop Models for Datamapper in Merb. One should watch the other MerbCamp videos for that. Let’s just assume that you’ve added a few other properties to the Article model class.

Let’s create the model’s sqlite3 tables.

$ rake db:automigrate
Don't know how to build task 'db:automigrate'

Oops. We don’t have that kind of default support in our slice’s rake tasks. But no fear, we’ll just invoke the auto_migrate! directly:

$ echo 'DataMapper.auto_migrate!' | slice -i
Loading init file from /Users/notroot/projects/myslice/config/init.rb
 ~ Connecting to database...
 ~ Loaded slice 'Myslice' ...
 ~ Parent pid: 9145
 ~ Activating slice 'Myslice' ...
DataMapper.auto_migrate!
[Merb::DataMapperSessionStore, Article]

Be sure to use single quotes since the `!’ character is special in bash. But if you want to do it interactively, just start up the slice irb console.

$ slice -i

The only other thing to note is how slice sql tables are named. Our “articles” table in the slice’s development database becomes “myslice_articles” in a Merb application’s database.

And to finally get it all together, we need to start mogrel to serve up the slice… but NOT using the `merb` command. We use the `slice` binary.

$ slice
Loading init file from /Users/notroot/projects/myslice/config/init.rb
 ~ Connecting to database...
 ~ Loaded slice 'Myslice' ...
 ~ Parent pid: 9147
 ~ Activating slice 'Myslice' ...
merb : worker (port 4000) ~ Starting Mongrel at port 4000
merb : worker (port 4000) ~ Successfully bound to port 4000

And off to the browser you go; and off to developing your slice.
Example) http://localhost:4000/articles

So what’s next once I finish developing my slice?

Install your slice directly into your gem repository.

$ sudo rake install

Add your slice to the application’s list of dependencies.

$ cd ~/projects/myapp
$ vi config/dependencies.rb
# Add your slice dependency to the bottom of the file.
dependency "myslice", "0.0.1"

Install the Slice into your Merb Application.

$ rake -T slices
$ rake slices:myslice:install

And go ahead and add your slice to your application’s router.rb file.

$ vi config/router.rb
# Find the following method call and add your slice.
Merb::Router.prepare do
  # This mounts your slice to the default http://example.com/myslice/
  # "namespace". See Merb's rubydocs for more info about options.
  slice(:myslice)

  # other stuff omitted.
end

Now, you can run `merb` to start up mongrel for your application and
hit away under the /myslice url path namespace.

Example) http://localhost:4000/myslice/articles

Go forth and slice!