Tag Archives: ruby

Using Ruby to Send Targeted Email to an Apple Watch

The other day I ran into a post about sending emails that could fall back to support the limited HTML that the Apple watch can display called hidden Apple Watch email. After reading the post I wondered if I could write a quick example to do what they demonstrated. I turned to Ruby + Ruby mail gem to give this a try and found that there are a few things to know but generally it works well.

To get started make sure you have the mail gem installed:

[code language=”shell”]
gem install mail
[/code]

Next there are two main points to remember before diving into the examples:

  • The sort order of the mime types is important, they need to come in the order listed in the examples or you will end up with the plain text version of the email on the watch
  • You need to include something in the full featured HTML section that can’t render on the watch or you will see the full featured HTML on the watch. See the article above for some pointers but generally the watch isn’t going to fetch an image from the web so that should do it and is what I have in the following images.

First a simple example that will show plain text for mail clients that don’t support HTML at all, normal HTML for full featured clients and a subset of HTML for the Apple watch.

[code language=”ruby”]
require ‘mail’

mail = Mail.new do
to ‘user@something.com’
from ‘person@company.com’
subject ‘Watch mail example’
end

#
# The order supplied here matters
#
mail.body.set_sort_order [ "text/plain", "text/watch-html", "text/html" ]

#
# The order here doesn’t matter
#
text_part = Mail::Part.new do
body ‘This is plain text’
end
mail.text_part = text_part

watch_part = Mail::Part.new do
content_type ‘text/watch-html; charset=UTF-8’
body ‘<b>This is HTML for the Apple watch</b>’
end
mail.add_part watch_part

#
# If this part has something in it that can’t display on the watch then
# the watch part will display. Keep that in mind if you want to force the
# watch part to display. Here the link out to an image will force the
# fallback to happen.
#
html_part = Mail::Part.new do
content_type ‘text/html; charset=UTF-8’
body ‘<h1>This is HTML</h1><img src="http://images.company.com/someimage.jpg"/>’
end
mail.html_part = html_part

mail.deliver
[/code]

Here is an example that includes an image that will display on the watch. It is important that in this case the image comes first in the sort order.

[code language=”ruby”]
require ‘mail’

mail = Mail.new do
to ‘user@something.com’
from ‘person@company.com’
subject ‘Watch mail example with image’
end

#
# The order supplied here matters
#
mail.body.set_sort_order [ "image/png", "text/plain", "text/watch-html", "text/html" ]

#
# The order here doesn’t matter but you will need to
# reference the image later.
#
mail.attachments[‘test.png’] = File.read(‘/tmp/test.png’)
image_cid = mail.parts.first.url

text_part = Mail::Part.new do
body ‘This is plain text’
end
mail.text_part = text_part

watch_part = Mail::Part.new do
content_type ‘text/watch-html; charset=UTF-8’
body ‘<b>This is HTML for the watch</b> <br/> <img src="’ + image_cid + ‘"/>’
end
mail.add_part watch_part

html_part = Mail::Part.new do
content_type ‘text/html; charset=UTF-8’
body ‘<h1>This is HTML</h1><img src="http://images.company.com/someimage.jpg"/>’
end
mail.html_part = html_part

mail.deliver
[/code]

Browser Based Push Notifications with Mongrel2 and EventSource

One of the interesting things about Mongrel2 is its ability to send output to multiple clients with a single handler message. This has a lot of potential for push applications and while I was investigating Mongrel2 a new version of iOS came out that included changes to Safari. While looking at the list of Safari changes in iOS 4.2 I noticed something called EventSource and went to investigate what it was.

As it turns out EventSource is a newer way of doing browser push currently supported by Chrome, Opera and Safari (mobile Safari as well). There is a good HTML5Rocks post on Server-Sent Events that goes into more detail on the differences of using it over something like WebSockets. One of the differences is that EventSource specially addresses mobile device use with the ability to do a “Connectionless push” through a proxy so the end device can sleep but still receive push notifications.

Before reading on check out my example Mongrel2 ruby handler post if you haven’t already. The following examples will be based on the code from that post. I’m also going to use Modernizr to detect support for EventSource so check out my post on using Modernizr to detect browser support as well.

Continue reading

Example Mongrel2 Handler in Ruby

Right before I went to Rubyconf I started looking at Mongrel2 so I had something to hack on while I was there. I grabbed the two Ruby handlers listed on the main site, Ruby Mongrel2 and Rack Mongrel2, to get started. I noticed right away that I couldn’t kill the handler process once it started using either one of these libraries. I started down the path of figuring out why they wouldn’t respond to SIGINT and eventually to writing my own handler in Ruby that demonstrates how to fix the issue

Continue reading

Using Daemon-Kit and RobustThread to Build Ruby Daemons

On a number of occasions I have found myself needing to assemble a daemon process for some type or processing done using Ruby. Each time I roll things a little different and I finally started to wonder if someone had already put together tools for doing the daemon parts. After some quick digging I ran into Daemon-Kit and after adding it together with a couple other tools it seems like what I’ve needed. I’ve put together a few recipes here to help guide others who might be looking for something similar.

Continue reading

How I Used Hpricot and Mechanize in GeeQE

While building GeeQE I wanted to enhance the CC dump of Stack Overflow’s data. The main reason I wanted to do this was to capture Gravatar hashes and user badges. To do this I decided to continue using Ruby as I did with the XML loading (see my previous post on XML parsing with Ruby). The easy choice was of course Hpricot to parse the HTML from the users page and Mechanize to move from one page to the next.

Continue reading

Fast XML parsing with Ruby

One of the first things I needed to do while building the GeeQE iPhone application was process the CC data dump from Stack Overflow. The dump contains XML files representing tables from Stack Overflow with the largest file being posts.xml weighing in at 1.2G as of September. I decided it would be pretty easy to use Ruby to parse the XML and load the data into MySQL so I went about finding the right parser for the job.

If you haven’t processed large amounts of XML before one thing to realize is that you don’t want to use a DOM parser because it is going to load the entire XML structure into memory. What you want is a SAX parser that can work on the XML stream as it comes in. With this in mind I started looking around and quickly found an older benchmark post that gave me an educated guess that the LibXML library was going to be the fastest parser for Ruby. After figuring out how to use it I decided to also give a couple other libraries a shot to see how they stacked up, the other two I looked at were REXML and Nokogiri.

Continue reading

Using Ruby and HTTParty to consume web services the easy way

Web services seem to be multiplying like rabbits these days. For a good sampling of just how many there are check out the Programmable Web API list. In general it is pretty easy to consume basic REST web services but after you have done it enough it starts getting old. Thankfully for those of us who like to tinker with a lot of the new APIs there is HTTParty to make it quick and easy.

Continue reading

Cleaning up stale rails sessions (removing ruby_sess files)

I’m not sure if something isn’t set up correctly of if this is just a fact of life with rails but the sessions it creates never seem to go away. I think before rails 1.1 the sessions where stored in /tmp and now they are stored in the apps directory along with everything else so they is probably no internal mechanism to delete them. I only noticed because after about a month of an certain app running the disk on the machine started to fill up. After digging a little I found 50K ruby_sess.* files hanging out in the rails session directory.

Anyway it was easy enough to clean up the stale ruby_sess files by going into the rails webapp/session directory and then running the following command:

[code lang=”ini”]find -type f -name “ruby_sess*” -exec rm -f {} \;[/code]

I’m not sure why the app is creating sessions but it isn’t something that stores state so I didn’t have to worry about killing active sessions here. If you do need to worry about that you will probably want to toss a time on the find command.

After looking a little more I found a post about this that has a ruby way of cleaning up the sessions.

[tags]ruby, rails, rails session[/tags]