A geek stranded on Martha’s Vineyard

Presenting at RailsConf 2017

October 28, 2017

One of the benefits of working at ActBlue is that you have the opportunity to play with lots of traffic.

In August of last year, I gave a talk at Boston Ruby Group sharing our experience building a high performance web application. The feedback I received from that audience and from colleagues allowed me to improve it and submit a proposal to RailsConf 2017.

My application was accepted in the High Volume track. I gave my talk last April, and although I was pretty nervous, I’m happy with the result. After 6 months, the talk is ranked #26 of 86 with 500 views in Confreaks. I recommend you to watch it.

Understanding Unicode Encoding in Ruby by Example

April 25, 2017
Unicode es fantástico

Every time I have to troubleshoot a problem with Unicode, it takes time to go through the documentation.

I compiled this list of methods and examples of how to use them. It has proven to save me time by quickly refreshing my memory.

Tested with Ruby 2.4.1 on macOS Sierra 10.12.4.

# the encoding is a property of String
utf8_resume = "Résumé"
=> "Résumé"
=> #<Encoding:UTF-8>

# translate the same string to different encodings
latin1_resume = utf8_resume.encode("ISO-8859-1")
latin9_resume = utf8_resume.encode("ISO-8859-15")
=> #<Encoding:UTF-8>
=> #<Encoding:ISO-8859-1>
=> #<Encoding:ISO-8859-15>

# specify the string using codepoints
lower_spanish_accents = "\u00E1\u00E9\u00ED\u00F3\u00FA\u00F1".encode("UTF-8")
=> "áéíóúñ"
upper_spanish_accents = "\u00C1\u00C9\u00CD\u00D3\u00DA\u00D1".encode("UTF-8")

# Length of the encoded text

# in UTF-8
#   'z' is 1 byte
#   'ñ' is 2 bytes
z = "\u007A"
=> "z"
z.each_byte.map{|c| "%X" % c}
=> ["7A"]
n_tilde = "\u00F1"
=> "ñ"
n_tilde.each_byte.map{|c| "%X" % c}
=> ["C3", "B1"]
=> 2

# but in Latin-1 'ñ' is only 1 byte
n_tilde.encode('iso-8859-1').each_byte.map{|c| "%X" % c}
=> ["F1"]

# but Unicode is universal, so in codepoints there's no difference
# between UTF-8 and Latin-1
n_tilde.each_codepoint.map {|c| "%X" % c}
=> ["F1"]
=> 1
n_tilde.encode('iso-8859-1').each_codepoint.map{|c| "%X" % c}
=> ["F1"]
=> 1

# codepoints are base-10 integers
=> [241]
=> [241]

# formats to specify codepoints

# single codepoint
# exactly 4 hex digits
#   \uXXXX            <==> U+XXXX
# multiple codepoints
# hex digits
# leading 0 is optional
#   \u{X XX XXX XXXX} <==> U+000X U+00XX U+0XXX U+XXXX
=> "España"
"\u{45 73 70 61 F1 61}"
=> "España"

# sometimes codepoint and byte sequence will match
"\u007f".each_codepoint.map{|c| "%X" % c}
=> ["7F"]
"\u007f".each_byte.map{|c| "%X" % c}
=> ["7F"]

# but this isn't always true
# see also example for ñ above
"\u0080".each_codepoint.map{|c| "%X" % c}
=> ["80"]
"\u0080".each_byte.map{|c| "%X" % c}
=> ["C2", "80"]

# not all byte sequences are valid encodings
=> true
=> false

# scrub to the rescue
scrubbed = "\u3042\x81".scrub('')
=> true
scrubbed.each_codepoint.map{|c| "%X" % c}
=> ["3042"]

# building the string using the internal representation, i.e. byte by byte
espana_utf8 = [0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x61]
=> [69, 115, 112, 97, 195, 177, 97]
=> "España"

# now in Latin1 (different byte sequence)
espana_latin1 = [0x45, 0x73, 0x70, 0x61, 0xf1, 0x61]
=> [69, 115, 112, 97, 241, 97]
=> "Espa\xF1a"
# although the ñ doesn't look correct, the enconding is correct
=> true

# currency symbols in UTF-8
currency_utf8 = "\u{20AC A3 A5}"
=> "€£¥"

# Convert a number from any base to any base
class String
  def convert_base(from, to)
# example: letter "~", from base 16 to base 10
"7E".convert_base(16, 10)
=> "126"
# example: decimal 255 to hexadecimal
'255'.convert_base(10, 16)
=> "ff"

Politics and Twitter Popularity

April 19, 2016

As an engineer of ActBlue I have been following closely the Sanders campaign. Leaving aside his political views, he is a unique candidate leading a historical effort, as the first person to run for president of the USA funded exclusively by small donations.

It is a fact that no political career is viable without the backing of millions of dollars. Citizens United made things worse by allowing unlimited election spending by individuals and corporations. The result is a few affluent people exerting a great deal of power in government.

ActBlue is trying to change this by providing a technology platform that allows the collection of money from ordinary people fast and efficiently. Although ActBlue has existed for over 10 years, Bernie is the first presidential candidate to rely on it as his only source of funding.

Sanders announced in April 30 of last year he was running for president and raised one million dollars in small donations in the first day. During the following 4 months, from May to August, he raised $5 million monthly.

Most people had never heard of Bernie before and challenging someone as popular as Hillary Clinton in the primaries was a little crazy. In August 22nd Hillary had 4 million Twitter followers compared to 590 thousand for Bernie, a huge difference. That is when I decided to write a script and record the number of followers daily for the most popular candidates. It would be a fun little project to see how the numbers evolved as the primaries progressed.

Obviously Twitter popularity does not translate to votes, but some correlation makes sense. To me more important than the follower count itself is its growth rate, how quickly each account is getting new followers.

The only candidate as popular as Hillary was Trump. So I am separating the data in 2 groups, the Multi Million Group with Hillary and Trump and the Single Million with everyone else.

Trump starts with fewer followers but surpasses her after the first Democratic debate in October 13. Growth is clearly on Trump’s side and in my opinion the graph shows that a face off between the two in a general election does not look for Clinton.

It would be a mistake to underestimate the entertainer. Ronald Reagan not only was elected, he is the best president in US history according to Republicans.

In the Single Million Group I am including additional candidates because I wanted to see their behavior after they had dropped out of the race. My apologies to any Kasich supporter reading this for not tracking his numbers.

It is interesting to see what happened on the days of large increases. Carson has the largest rise after the fourth Republican debate (Nov 10). All the candidates see a jump the day of the first Democratic debate (Oct 13). There are also big increases on the second Republican debate (Sep 16).

Ted Cruz has the highest steady rate of increase among Republicans.

Bernie leads on growth with his two accounts: BernieSanders and sensanders, each quadrupled its followers.

And that is all I have, 8 month of data until today, April 19. I am not going to make any predictions. I do not believe anyone can really foresee what is going to happen, even with better information, like poll numbers.

Although half of the states have voted we do not have a clear Democratic winner yet. Bernie has won 8 of the last 9 state primaries, and today is the turn of New York. Hillary has 1,307 delegates and Bernie 1,094 (2,383 are necessary to win the nomination).

Sanders not only has been able to sustain a campaign funded by small donations, he has actually raised more money than any other candidate: 7 million contributions with an average of $27 each, that is $189 millions.

I will keep recording the counts and will post new graphs in the future. In the meantime we, the engineers at ActBlue, are enjoying the challenge of making sure we take all those donations without any disruptions.

Because of the volume, this requires a continuous effort to improve performance. Our current record is from New Hampshire primary night, when Sanders gave his victory speech. He asked people to visit berniesanders.com and donate $27. Our traffic spiked to 333,000 requests per minute, at some point we were processing 44 credit cards per second. That’s right, we were feeling the bern.

The scripts to gather the data, process it and generate the graphs can be found in github

How to Upgrade to Strong Parameters in Rails

December 10, 2014

This article was originally published in the blog of ActBlue Technical Services.

How to Upgrade to Strong Parameters in Rails

In the previous blog post, using a series of tests, we described how strong parameters work. In this post we detail the steps we followed to upgrade our main application in ActBlue.

Starting Point

To help with the process Rails provides the protected_attributes gem. This allows you to run your app on 4.1 without having to make any changes related to mass-assignment protection. The gem brings backwards compatibility by implementing attr_accessible, attr_protected and other methods.

Add these lines to your Gemfile, run bundle install and make all your tests pass (you have tests, right?)

gem 'rails', '~> 4.1'
gem 'protected_attributes'

There are other incompatibilities you will have to resolve, but this post is about strong parameters and we are assuming your tests run green at this point.

Mixing Both

It is unlikely you can make all the changes in a single release and therefore you will want to have both protected attributes and strong parameters working at the same time. So the next step is to add this line to Gemfile and run bundle install:

gem 'strong_parameters'

In every model you want to upgrade add this line to include the ForbiddenAttributesProtection module. This is the way to indicate Rails which models are using the new mechanism, for example:

class Book < ActiveRecord::Base
  include ActiveModel::ForbiddenAttributesProtection

In the model also remove all calls to attr_accessible and attr_protected, for example:

attr_accessible :isbn, :title
attr_protected :price

In the corresponding controller it is useful to create a private method that whitelists its parameters, for instance:

def book_params
  params.require(:book).permit(:isbn, :title)

For simple models and controllers it is going to be similar to this, but for more complex cases you can check our previous blog post or the documentation.

Dropping Protected Attributes

When you think you have upgraded all your models and controllers and feel you are ready to pull the plug on the old protected attributes, these are the steps we recommend:

Remove these 2 lines from Gemfile and run bundle install.

gem 'protected_attributes'
gem 'strong_parameters'

In Rails 4 the default is strong parameters, so there is no need to include the gem.

Remove the configuration for protected_attributes, this means removing from config/application.rb:

config.active_record.whitelist_attributes = false

Remove from config/environments/test.rb:

config.active_record.mass_assignment_sanitizer = :logger

Remove any include ActiveModel::ForbiddenAttributesProtection from models, added in the previous step.

In the default configuration of strong parameters, when you whitelist only a subset of the params passed to the controller, instead of raising an exception, Rails is going to generate a notification. You can see a test illustrating this.

It is better instead to always raise an exception. This is done by adding this line to config/environments/test.rb:

config.action_controller.action_on_unpermitted_parameters = :raise

Now run all your tests. If you have a large application and you are getting errors you do not understand or the code is not behaving the way you expect, check our previous blog post Understanding Strong Parameters, this is the part where this information is most useful.

Final Step

When you are done with the tests you can remove the line from config/environments/test.rb:

config.action_controller.action_on_unpermitted_parameters = :raise

The default value in Rails is to generate a notification in test and development environments, and ignore otherwise.

You are done! We hope the 2 articles helped you to make the transition smoother.