Wednesday, January 15, 2014

How to get mobile data in Cusco, Peru for your phone using Claro


Getting mobile data to work in Cusco was fairly easy. You should make sure you have an unlocked phone that can take a SIM card. I was using a Nexus 4 so it comes unlocked.

Basically you can buy a SIM card at a Claro store. It will cost you 8 soles and will come with no money on it.

There are lots of places that say "Claro" in a big red logo but only some of them are actually official stores that sell the SIM card. When you ask for it they will ask for your passport.

After you get the SIM card you will need to recharge it or put money on it. This is the easiest part because you can then go to any of the shops that say "Claro Recharga". They are like everywhere. The process to put money on a card for a merchant only seems to require a cell phone so almost any merchant can do it.

Now for pricing. It can be found on claro.com at  http://www.claro.com.pe/wps/portal/pe/sc/personas/internet/internet-movil#info-03 You are looking for prepaid internet plans or "Paquetes de Internet". What package you decide to purchase depends on how long you are staying in Peru and how much data you use.

For me I was on T-Mobile back in the US, $50 per 30 days, 500MB worth of 4G internet then drops to 3G. 500MB carried me about the end of the month. I wish it was like 600 or 700MB. Of course that is with only light uploading and streaming.

The thing with Claro is that you have to buy a data pack and it will be good for only a certain amount of time. So the data packs come in 1 hour, 1 day, 3 day, 5 day, 10 day, 15 day, and 30 day amounts. I purchased the 5 day amount at 200MB.

To purchase you first give money to a recharging person. 3 soles is the minimum. I recommend you put 10 soles to buy the 5 day pack. After it is loaded you will get an SMS saying it is successful.




Here are the SMS messages I got. I actually did it 3 times for 5 soles, 5 soles, and the minimum 3 soles.

After your account is loaded then the trick is to SMS the phone number 779 with the text "5d" or "1h" or "3d" depending on what plan you want. This will activate the data pack. It is case sensitive by the way.

After this you should be able to configure your APN settings. These are settings specific to Claro used to access their data network. If you don't have them you can go here http://www.unlockit.co.nz/mobilesettings/settings.php?id=149.

That should be it. The 3G is stupid fast in Cusco. Much faster than in the US.

Thursday, May 23, 2013

3 Reasons Why Startup Marketers Should Know SQL


Startups Live and Die by Data Analysis


How often have you been in a startup and someone comes up and asks for you to pull "a few quick numbers" ?

One of common themes when running any company is the need for data analytics.
  • When do your customers buy?
  • How much do they buy?
  • When do they come back?
If you don't have an easy way to answer these questions you are driving without a map. As we collect more and more data there is a huge need to process and understand that data. Marketing and business development teams need this information to make data driven decisions.

The faster they can get this information the better the startup will be.

Developer Time is a Scarce Commodity


All to often the responsibility of generating and producing these reports falls on a developer. They rae the ones that understand the underlying data model.

The problem is that development time is valuable. Sure there are complex reports that only a developer may be able to put together. However there are a plethora of other common questions that I am sure could easily be answered with a few hours of SQL training:
  • How many users signed up today? Last week? Last month?
  • How many users do we have total?
  • Who are our top purchasers?

Data Empowerment is a "Good Thing"


By teaching non-technical people SQL they will end up asking better and harder questions. A lot of businesses can be analyzed through just their data model. What objects are in the system? How are they stored?

Exposing this knowledge can yield valuable insights into the business.


How to Get Started


From a technical standpoint there is some risk in giving non technical individuals access to run arbitrary SQL queries. A good setup is to replicate to a read only slave and give them read only access to that database.

I still haven't figured out a good tool for storing the SQL queries and sharing them though so if anyone has any suggestions I'd love to hear them.




Saturday, February 23, 2013

Knockout Advanced Tips: Binding Custom Parameters

It is possible in Knockout to bind custom parameters to a call in JavaScript. I believe this is intrinsic to JavaScript. It also kind of reminds me of currying though at the moment I am not entirely sure.
For example, suppose you wanted to call myFunction with some parameters besides event and item (because those are what you get normally by Knockout). I need to do this because I want a callback after the click event. So here is my function:



var myFunction = function(callback, data, event) {

   //value of "this" would be $data
};


Here is how I would do the call:


click: myFunction.bind($data, myCallbackFunction);
The first argument to "bind" is the context (value of "this") that you want the function to execute with. Any arguments after that will be passed to the function prior to the actual arguments.

Data and event are always passed after any extra arguments that you provided to "bind".

Saturday, December 29, 2012

Architecting KnockoutJS Applications

One of my favorite front end javascript libraries is KnockoutJS.

KnockoutJS is a javascript library that helps you bind javascript variables to DOM data. When you change the javascript variable the HTML automatically changes to the new value of the variable.

The magic behind how this happens isn't particularly important. If you really want to know you can read the source. The basic pattern is commonly referred to as event pooling.

Some of the best parts:

 - You should use knockout if you have a bunch of $(element).click(function()) functions and want to clean it up. If your frontend code is even a bit complicated you probably know what I am talking about.
 - It is easy to integrate with an existing project. AngularJS is probably best off when you are starting from scratch. Knockout is great because you can optionally use it when you have a really complicated frontend.
 - Really quick to learn. I found knockout super easy to pick up. The thing you have to get over is your elements looking like:

<span data-bind="text: airplane"></span>


It irked me a bit to see a data-bind attribute when it isn't really HTML. Yes you are mixing presentation logic with code. Honestly though I've seen learned that it is pretty pragmatic. Lots of HTML editors play nice with knockout and people who only know HTML and CSS can pick it up quickly.


Some of the parts I don't particular like about Knockout:

 - Lack of a good PJAX/history.js style navigation. You get this with AngularJS.
 - Getting your view models to be semi reusable is a pain in the butt. I used a combination of server side script includes and knockout statements.

When first getting started you may wonder how should you architect your application so it is somewhat less like spaghetti. A good way to organize your Knockout code is first think of all the objects in your application. If you are using Rails (or in my case Django) you will have a bunch of models.

For each of these models it is nice to have some sort of front end analogue in Javascript.

Here is an example of an object:


function Airplane(viewModel, data) {
  var self = this;
  ko.mapping.fromJS(data, {}, self);

  self.visible = ko.observable(true);
  self.selected = ko.observable(false);

  self.land = function(item, event) {
    $.ajax({
      url: "/api/airplane/" + self.id() + "/land",
      type: 'POST',
      success: function() {
        // Update myself with the fresh data from the server
        ko.mapping.fromJS(data, self);
      },
      error: function(jqXHR, textStatus, errorThrown) {
      }
    });
  }
}

I usually like to stick this in some namespace that is available globally. A few comments about this object. I take in the viewModel which created it so I have access to the parent (this is useful later). I also take in this variable called data which is used for an awesome knockoutjs plugin called ko.mapping. It basically iterates through your javascript object and creates ko.obserables() out of all of attributes. Super useful and you want to use this. I didn't use it before and I had to do self.variable = ko.observable(data.variable) over and over. Yeah not a good idea.

Another awesome thing about the plugin is when your AJAX REST API call returns you can update the object in one line by doing ko.mapping.fromJS(data, self).

Hope you give knockout a try. If you have any questions I've noticed the knockout community is awesome. I am also available so just drop me a note.

-Jeff

Saturday, August 25, 2012

Introducing Django Friendlyurls


Django Friendly URLs



Django Friendly URLs is a Django application allows you to create urls that directly return views. There is no 302 Redirect.

Why would you want such a thing?

The most common use case is that you have URLs which return user profiles such as http://www.example.com/user/1

Friendly URLs allow you to create a URL so that the user can also be accessed by going to:

http://www.example.com/jeff

The key is that the address bar in the browser does not change to http://www.example.com/user/1

The web server returns the same HTML document as http://www.example.com/user/1


Flow

The application looks up the string 'jeff' If it finds a friendly url match it will read out the absolute_path and attempt to resolve it by putting it through the URL resolver a 2nd time.
If it finds a resolution it will return that view.

Usage

Here is an example of how to implement friendly_urls for a user:
I put this in models.py:
from django.contrib.contenttypes import generic from friendlyurls import UrlMapping from django.contrib.auth.models import User
def get_absolute_url(self):
return u'/user/%s' % self.id
User.add_to_class('friendly_urls', generic.GenericRelation(UrlMapping)) User.get_absolute_url = get_absolute_url
This basically monkey patches the User model to add a field. You can then do
user = User.objects.get(pk=1) user.friendly_urls.all()
This will get a list of all the friendly urls.
You shouldn't have to monkey patch all the models.

Problems / Questions

Don't forget to run syncdb because friendlyurls creates a table to store the URL mappings.

Additional Settings

FRIENDLYURLS_IGNORE_CASE - Set this to True if you want to ignore the case when trying to find a path. Default: False

Todo Features

  • Need to cache the results returned by a lookup for performance.
Probably do a from django.core.cache import cache

Friday, July 27, 2012

Update to backing up Google Apps Accounts

It turns out there is an even easier way to backup Google Apps Accounts.

Since we are still on the free tier I had to manually go in to each account and enable IMAP.

After this was done I found a tool called imap2maildir.

https://github.com/rtucker/imap2maildir/ 

$ ./imap2maildir -u jeff@example.com -d jeff@example.com --create

Created a directory called "jeff@example.com".

Thursday, October 27, 2011

Backing up and Archiving Google Mail Accounts

The easiest way to backup Google Mail accounts is to enable IMAP on the account and download the messages using getmail.

Once getmail is installed you need to create a file called getmailrc. If you plan to download multiple gmail accounts then you might want to create a directory for each account and point the getmail script to that directory. Here is an example of a getmailrc file for Google Mail:


[retriever]
type = SimpleIMAPSSLRetriever
server = imap.gmail.com
username = username@example.com
password = examplepassword
mailboxes = ("[Gmail]/All Mail",)
port = 993

[destination]
type = Maildir
path = ~/username@example.com/

[options]
received = false
delivered_to = false
read_all = false
verbose = 1


After this file is saved you can procede to run getmail. I needed getmail to run all night and in the background. I outputted all the stdout to a logfile so I used the following command:

getmail --getmaildir . > output.txt 2>&1 &

Don't forget to create the directories cur, new, tmp as these are the directories that are needed for IMAP.


Now that you have your mail in a Maildir format what do you do with it? In my case I wanted to delete the account off Google Apps but still be able to search the mail if I needed it at a later date.

The strategy I came up with to bring up a copy of courier and serve the Maildir using a webmail script (in this case Roundcube).

I installed PHP through Nginx first. The easiest way to get a PHP environment up and running on Nginx is to use the Ubuntu packages:

php5-cgi
php5-common

For additional functionality such as PostgreSQL support you can install the package:
php5-pgsql

I then configured nginx with the following script:


server {
listen 80;
server_name webmail.example.com;
access_log /var/log/nginx/access.log;
log_subrequest off;

location / {
root /www/webmail.example.com;
index index.php;

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME /www/webmail.example.com/$fastcgi_script_name;
}
}
}

I then created the script: /etc/init.d/php-fcgi
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
echo -n "Starting PHP FastCGI: "
start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
RETVAL=$?
echo "$PHP_CGI_NAME."
}
stop() {
echo -n "Stopping PHP FastCGI: "
killall -q -w -u $USER $PHP_CGI
RETVAL=$?
echo "$PHP_CGI_NAME."
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL


After php was up and running I installed courier:
apt-get install courier-imap courier-imap-ssl

I then downloaded roundcube and configured roundcube as necessary.