davidchua https://dchua.com/ Recent content on davidchua Hugo -- gohugo.io en-us Fri, 08 Sep 2023 02:43:00 +0000 Diagnosing too many open files https://dchua.com/posts/2023-09-08-diagnosing-too-many-open-files/ Fri, 08 Sep 2023 02:43:00 +0000 https://dchua.com/posts/2023-09-08-diagnosing-too-many-open-files/ When facing a “Too Many Open Files” error, it is normally a result of a process taking up too many file descriptors. To diagnose this, find out the PID of the process which gave out the error. You can do this with a: $ ps auwx | grep <name_of_process> Verify open file limit per process 📝 Note: Each process that runs have a number of open files limit. By default, most of the time the limit is 1024 Capturing Groups in Sed https://dchua.com/posts/2021-09-19-capturing-groups-in-sed/ Sun, 19 Sep 2021 07:44:01 +0000 https://dchua.com/posts/2021-09-19-capturing-groups-in-sed/ While migrating this blog from Jekyll to Hugo, I found I needed to replace all my old markdown posts which has my title params using single-quotes which Hugo does not appreciate, to double-quotes. For example, I have the following across multiple files (over 200+ files) --- title: 'abcdefg' tags: - new - stuff --- And I needed to replace title: 'abcdefgh' with title: "abcdefgh". To do that, I’m going to use sed because its going to be terribly painful to do this manually. About https://dchua.com/about/ Sun, 19 Sep 2021 01:59:04 +0000 https://dchua.com/about/ Hello My name is @davidchua and I’m based in Singapore. I currently work for Talenox.com as a Devops Engineer. I am a backend-engineer/sysadmin who loves everything relating to infrastructure, cloud-native, devops, observability and engineer productivity. I am comfortable writing in golang, ruby/rails and bash scripts on production. I can also write in python, java and some php on the side. My usual development environment consists of vim + tmux in my homelab that I connect to via wireguard whenever I’m outside. Sending your Structs across the wire (tcp connection) https://dchua.com/posts/2017-06-23-sending-your-structs-across-the-wire/ Wed, 28 Jun 2017 00:00:00 +0000 https://dchua.com/posts/2017-06-23-sending-your-structs-across-the-wire/ Sending your structs across the wire and receiving them on the other side. Using encoding/gob can help ensure your data structures can receive it on the other side. // server.go package main import ... // Create your custom data struct type Message struct { ID string Data string } func main() { // for purpose of verbosity, I will be removing error handling from this // sample code server, err := net. The Signal Pattern https://dchua.com/posts/2017-06-28-the-signal-pattern/ Wed, 28 Jun 2017 00:00:00 +0000 https://dchua.com/posts/2017-06-28-the-signal-pattern/ The signal pattern can be used to ensure your long-lived processes (ie. servers) are able to handle signals being sent to the process. import ... type Handler struct { } func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{}`)) } func main() { h := &Handler{} sigCh := make(chan os.Signal, 1) errCh := make(chan error, 1) signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP) go func() { err := http.ListenAndServe(":3000", h) errCh <- err }() for { select { case sig := <-sigCh: switch sig { case syscall. Using S2i (source-to-image) like buildpacks to deploy apps https://dchua.com/posts/2017-06-21-using-s2i-like-buildpacks-to-deploy-apps/ Wed, 21 Jun 2017 00:00:00 +0000 https://dchua.com/posts/2017-06-21-using-s2i-like-buildpacks-to-deploy-apps/ Openshift’s S2i (Source-2-image) is an alternative framework to buildpacks which uses Dockerfiles and Openshift to create applications out of reproducible container images from source. Objective The goal is to be able to create a container image that contains the necessary application dependencies just by uploading the application code without having to write Dockerfiles. Creating S2i builder images S2i builder images are the base images that your deploying application will be injected its code/binary into to create a new deployment image. Load env variables from ConfigMaps and Secrets upon Pod boot https://dchua.com/posts/2017-04-21-load-env-variables-from-configmaps-and-secrets-upon-pod-boot/ Fri, 21 Apr 2017 00:00:00 +0000 https://dchua.com/posts/2017-04-21-load-env-variables-from-configmaps-and-secrets-upon-pod-boot/ One of the coolest stuff I’ve picked up just today is that you can keep environment variables that you want to be loaded into every deployment pod in a neatly configured ConfigMap or Secret which gets injected back into the Pod during deploys. Lets say you have a Secret that looks like: apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque data: username: YWRtaW4= password: MWYyZDFlMmU2N2Rm And you want username and password to be easily accessible in ENV['username'] and ENV['password'] on your application pods, all you need is a envFrom within your TemplateSpec How to access unexported embedded structs within composite literals in Golang https://dchua.com/posts/2017-04-18-how-to-access-unexported-embedded-structs-within-composite-literals-in-golang/ Tue, 18 Apr 2017 00:00:00 +0000 https://dchua.com/posts/2017-04-18-how-to-access-unexported-embedded-structs-within-composite-literals-in-golang/ Short Answer, you can’t. BUT you can still declare it. If you have two struct types which is embedded within one another but declared as an unexported type, you will not be able to declare it in a composite type. You can only declare it outside of an composite literal Came across this while attempting to make use of Kubernetes' API to declare an Deployment object. type Deployment struct { metav1. Forwarding Vault audit logs to a remote Syslog server (like Graylog) https://dchua.com/posts/2017-03-06-forwarding-vault-audit-logs-to-a-remote-syslog-server/ Mon, 06 Mar 2017 00:00:00 +0000 https://dchua.com/posts/2017-03-06-forwarding-vault-audit-logs-to-a-remote-syslog-server/ Using Vault’s Audit Backend to send logs to a remote Syslog server. Objective Send audit_logs from Hashicorp’s Vault to an Graylog instance Prerequisite Setup a Syslog TCP/UDP Input on Graylog (if you’re using graylog) Has a remote syslog server running Steps Important Notice: Vault has a Syslog Audit Backend as part of its suite but it currently does not allow remote forwarding. In order to do that we will have to make use of rsyslog’s rules forwarding. Alerting with Prometheus and AlertManager https://dchua.com/posts/2017-02-28-alerting-with-prometheus-and-alertmanager/ Tue, 28 Feb 2017 00:00:00 +0000 https://dchua.com/posts/2017-02-28-alerting-with-prometheus-and-alertmanager/ How to setup Prometheus AlertManager and get a whole alerting pipeline setup. Objectives and Goals Write and Deploy Prometheus Alert Rules Configure Prometheus to send Alerts to Alert Manager Setup AlertManager to receive Prometheus Alert Send a Slack message on Alert Prerequisite Prometheus is already setup and running Alert Rules To begin writing and deploying alerts, you’ll need to modify your prometheus config file. Usually, its located at /etc/prometheus/prometheus. Deleting files and getting your diskspace back without rebooting https://dchua.com/posts/2017-02-03-deleting-files-and-getting-your-diskspace-back-without-rebooting/ Fri, 03 Feb 2017 00:00:00 +0000 https://dchua.com/posts/2017-02-03-deleting-files-and-getting-your-diskspace-back-without-rebooting/ When you run out of diskspaces and need to delete files quickly to recover them, most of the time, your deleted files will not free up the diskspace until the process that is using it is restarted or deleted. To force the filesystem to free up the lock to the file so that the files can be cleared up, you’d need to find the process that is using the file and truncate it. Using Multiple Buildpacks in Deis https://dchua.com/posts/2017-01-22-using-multiple-buildpacks-in-deis/ Sun, 22 Jan 2017 00:00:00 +0000 https://dchua.com/posts/2017-01-22-using-multiple-buildpacks-in-deis/ To use multiple buildpacks with your Application Deployment on Deis Workflow, is pretty straight forward. As heroku-buildpack-multi is already built into deis' slugbuilder, all you need to do is to create a .buildpacks file in your repository, insert your required buildpacks, commit and push away. An example .buildpack could look like: # .buildpacks https://github.com/cloudfoundry/heroku-buildpack-ruby.git https://github.com/gaumire/heroku-buildpack-mysql Enjoy. References Github - multi-buildpacks Fixing OmniAuth Redirecting to a URI with Port https://dchua.com/posts/2017-01-16-fixing-omniauth-redirecting-to-a-uri-with-port/ Mon, 16 Jan 2017 00:00:00 +0000 https://dchua.com/posts/2017-01-16-fixing-omniauth-redirecting-to-a-uri-with-port/ Came across this problem the other day when deploying a Rails application that is sitting behind Kubernetes and Deis With my Rails application using OmniAuth and its Facebook strategy, after a user logins, hits Facebook and returns a redirection, the URL returned has its port 80 embedded in the return URI like: https://domain.com:80. In most cases, this wouldn’t be a problem but Facebook would return an error as the return URI (with the port 80) is not whitelisted hence creating a bad flow for your user. Supercharge your Nginx with Openresty and Lua https://dchua.com/posts/2017-01-01-supercharge-your-nginx-with-openresty-and-lua/ Sun, 01 Jan 2017 00:00:00 +0000 https://dchua.com/posts/2017-01-01-supercharge-your-nginx-with-openresty-and-lua/ Turn your Nginx into an API Gateway. Modify requests and responses from Nginx itself using Openresty and Lua. Objectives To deploy Openresty on a development/production environment As a reverse proxy, to modify incoming request params and pass it on to another server As a reverse proxy, to modify response from proxied server before returning to user Openresty Openresty is a open-sourced NGINX bundle that combines NGINX and various 3rd-party NGINX modules and Lua Libraries. Monitor a Directory for File Changes with inotifywait https://dchua.com/posts/2016-12-27-monitor-a-directory-for-file-changes-with-inotifywait/ Tue, 27 Dec 2016 00:00:00 +0000 https://dchua.com/posts/2016-12-27-monitor-a-directory-for-file-changes-with-inotifywait/ Sometimes there may be a need to monitor and be alerted when any new files have been added in a particular directory. One of the quickest way to do that is to use inotifywait. You can find it in the inotify-tools package. If you don’t have it yet, you can install it with apt-get install inotify-tools if you’re on Debian/Ubuntu. Create a nice looking bash script. Maybe something called watcher.sh, copy the following script and fire away! Using Kubectl proxy to access your Kubernetes services https://dchua.com/posts/2016-11-20-using-kubectl-proxy-to-access-your-kubernetes-services/ Sun, 20 Nov 2016 00:00:00 +0000 https://dchua.com/posts/2016-11-20-using-kubectl-proxy-to-access-your-kubernetes-services/ Sometimes it might be quite difficult to access your kubernetes services if your infrastructure isn’t able to properly support both LoadBalancer and NodePort service types. Or maybe you just want to quickly access your services securely without having it be exposed to the public internet. kubectl proxy is a great tool for you to do just that. First, run kubectl proxy on your local machine. This exposes and proxies port 8081 to your Kubernetes Master. Running Deis on Digitalocean - A Nginx/Haproxy LB Setup https://dchua.com/posts/2016-10-25-running-deis-on-digitalocean/ Tue, 25 Oct 2016 00:00:00 +0000 https://dchua.com/posts/2016-10-25-running-deis-on-digitalocean/ DigitalOcean users generally tend to get the short end of the stick when it comes to convenience. When building your Deis cluster on DigitalOcean, you’ll realize you don’t get the benefit of being able to provision cloud-provider specific load balancers like (AWS ELB, GCE Load Balancers) to route inbound traffic into the kubernetes cluster and into your deis-router. In this article, I’ll quickly go through setting up a (nginx + haproxy) load balancer that sits infront of deis. How to get Kubernetes and Deis onto Azure https://dchua.com/posts/2016-10-17-how-to-get-kubernetes-and-deis-onto-azure/ Mon, 17 Oct 2016 00:00:00 +0000 https://dchua.com/posts/2016-10-17-how-to-get-kubernetes-and-deis-onto-azure/ With Kubernetes Anywhere, setting up a Kubernetes cluster on Microsoft’s Azure isn’t as painful as it used to be. Prerequisite Docker is setup gcc is setup kubectl is setup Azure Subscription ID is prepared Kubernetes Prepare your environment Deploy $ git clone https://github.com/kubernetes/kubernetes-anywhere $ cd kubernetes-anywhere Starting the development process $ make docker-dev $ make deploy You’ll be prompted to fill up the required fields. Please skip the following fields by leaving them blank. Removing DNS caching on NGINX proxy pass https://dchua.com/posts/2016-09-29-removing-dns-caching-on-nginx-proxy-pass/ Thu, 29 Sep 2016 00:00:00 +0000 https://dchua.com/posts/2016-09-29-removing-dns-caching-on-nginx-proxy-pass/ If you’re passing hostnames into your nginx’s proxy_pass and made a change to the hostname’s ip address, you might experience times when nginx is still referencing the old ip address despite your DNS TTL having already been expired. This is because nginx caches the ip of all hostnames resolved in its configuration. In order to ensure that any IP change is quickly resolved, you should specify a valid period so that nginx will force a cache refresh on its ip storage. Secure your Server - Send Slack Alert on SSH Login https://dchua.com/posts/2016-08-17-secure-your-server-send-slack-alert-on-ssh-login/ Wed, 17 Aug 2016 00:00:00 +0000 https://dchua.com/posts/2016-08-17-secure-your-server-send-slack-alert-on-ssh-login/ In my line of work, besides architecting and building infrastructure, part of the scope includes building safeguards to ensure that systems that were setup are properly secured. Here’s a quick trick for folks looking for an easy first-line defence that they can implement easily across their servers right now. In order to quickly respond to a server compromise, it is a good idea to setup some form of IDS to alert you when a potential headache is about to happen. Building a Kubernetes PetSet Cluster for your MongoDB ReplicaSet https://dchua.com/posts/2016-08-08-building-a-kubernetes-petset-cluster-for-your-mongodb-replicaset/ Mon, 08 Aug 2016 00:00:00 +0000 https://dchua.com/posts/2016-08-08-building-a-kubernetes-petset-cluster-for-your-mongodb-replicaset/ Some observations when working with Kubernetes 1.3 introduced PetSets to build MongoDB. Here’s my PetSet YAML: # mypetset.yml apiVersion: v1 kind: Service metadata: annotations: service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" name: a-mongo labels: name: a-mongo tier: database type: mongo spec: ports: - port: 27017 name: mongo type: NodePort selector: tier: database name: a-mongo --- apiVersion: apps/v1alpha1 kind: PetSet metadata: name: a-mongo spec: serviceName: "a-mongo" replicas: 3 template: metadata: labels: name: a-mongo tier: database annotations: pod. How to change instance size of an existing Kubernetes Cluster https://dchua.com/posts/2016-07-27-how-to-change-instance-size-of-an-existing-kubernetes-cluster/ Wed, 27 Jul 2016 00:00:00 +0000 https://dchua.com/posts/2016-07-27-how-to-change-instance-size-of-an-existing-kubernetes-cluster/ Just recently, I was trying to reverse my poor decision of starting my Kubernetes Cluster on AWS with a very simple t2.micro nodes. If you’re on AWS, your nodes are probably created via an Auto-scaling group. To change your instance size, you’ll need to: Copy the launch configuration that Kubernetes is probably running. Change the instance size of the launch configuration Update the Kubernetes Auto Scaling Group to use the new launch configuration ? How to set current_user in Tornado asynchronously https://dchua.com/posts/2016-07-23-how-to-set-current_user-in-tornado-asynchronously/ Sat, 23 Jul 2016 00:00:00 +0000 https://dchua.com/posts/2016-07-23-how-to-set-current_user-in-tornado-asynchronously/ Something that I’ve missed when working on setting the current_user upon @tornado.web.authenticated. Normally, you’d set get_current_user(self) in the RequestHandler which is used by @tornado.web.authenticated to pull/query the current_user that is logged on. It is fine until you start to utilize asynchronous functions within the get_current_user(self). Quick notes: get_current_user(self) cannot be a coroutine. So no @tornado.gen.coroutine decorator before it. This also means that it cannot perform yield functions from elsewhere. when required to use a async function to pull user information, use prepare(self) instead. Setting Up Amazon VPC to VPC Peer Connection https://dchua.com/posts/2016-07-21-setting-up-amazon-vpc-to-vpc-peer-connection/ Thu, 21 Jul 2016 00:00:00 +0000 https://dchua.com/posts/2016-07-21-setting-up-amazon-vpc-to-vpc-peer-connection/ Need to communicate between 2 Amazon VPCs securely and privately? Here’s some of my notes on setting up a VPC Peer Connection. Prerequisite Ensure that your VPCs do not share the same CIDR block and can are in different subnets Link up Note, VPC Peering is one-way only so if you create a VPC peer connection from one VPC (lets call this A) and another VPC (B), only A will be able to communicate and access B. Logging Tornado Logs To Graylog (with JSON) https://dchua.com/posts/2016-07-19-logging-tornado-logs-to-graylog/ Tue, 19 Jul 2016 00:00:00 +0000 https://dchua.com/posts/2016-07-19-logging-tornado-logs-to-graylog/ If you’re building your microservices on Tornado and your chosen deployment strategy is to treat it like cattle instead of pets, logging is going to have to play a very important part of your architecture. To ensure that all your information is logged and stored in a centralized location, I built a full HA cluster of graylog nodes and elasticsearch nodes which I will use to keep track of my application. Painless ECR authentication for Docker https://dchua.com/posts/2016-06-17-painless-ecr-authentication-for-docker/ Fri, 17 Jun 2016 15:24:00 +0800 https://dchua.com/posts/2016-06-17-painless-ecr-authentication-for-docker/ I’ve been playing around with Jenkins lately and one of the problems I’ve been facing is that it gets tedious manually doing a aws ecr get-login every 12 hours on the Jenkins instances so that my builds can push docker images into ECR. You may be thinking, why not just add the authentication into the build step before the push. Tried that but for some reason, I’m never able to get it to eval correctly. Speed up NPM install in Docker Containers https://dchua.com/posts/2016-06-17-speed-up-npm-install-in-docker-containers/ Fri, 17 Jun 2016 00:00:00 +0000 https://dchua.com/posts/2016-06-17-speed-up-npm-install-in-docker-containers/ NPM installs in your docker containers getting slow, make sure you set registry! # Dockerfile RUN npm config set registry http://registry.npmjs.org/ && npm install --no-optional --verbose Try this the next time your NPM install feel like its taking forever. Getting Started with Jenkins using Docker https://dchua.com/posts/2016-06-10-getting-started-with-jenkins-using-docker/ Fri, 10 Jun 2016 00:00:00 +0000 https://dchua.com/posts/2016-06-10-getting-started-with-jenkins-using-docker/ $ docker run --user root --privileged -p 8080:8080 -p 50000:50000 -v /path/to/host/volume:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins Lets break it down. -p 8080:8080 What we’re doing here is to expose the Jenkins port on the container out to the host. Jenkins uses 8080 by default so you may want to change the host port to a port that you find comfortable. -p 50000:50000 Jenkins uses port 50000 for its REST API. Expose this too. Installing Jenkins Plugins with their Dependencies in One Step https://dchua.com/posts/2016-06-02-installing-jenkins-plugins-with-their-dependencies-in-one-step/ Thu, 02 Jun 2016 00:00:00 +0000 https://dchua.com/posts/2016-06-02-installing-jenkins-plugins-with-their-dependencies-in-one-step/ Assuming that your Jenkins server is listening at port 8080, all you need to do is to run the following curl command: $ curl -X POST -d '<jenkins><install plugin="[email protected]" /></jenkins>' --header 'Content-Type: text/xml' http://localhost:8080/pluginManager/installNecessaryPlugins Replace [email protected] with the name of your plugin you’re trying to install by looking for the plugin-id in the plugin’s page. ie for Docker Build Step, the Plugin ID listed on the page is docker-build-step So your curl script should look something like: Hooking up Jekyll to Facebook Instant Articles https://dchua.com/posts/2016-04-24-hooking-up-jekyll-to-facebook-instant-articles/ Sun, 24 Apr 2016 10:57:56 +0800 https://dchua.com/posts/2016-04-24-hooking-up-jekyll-to-facebook-instant-articles/ Run a Jekyll site? Want to get into the whole Facebook Instant Articles Action? Prerequisite Install Jekyll Last Modified Plugin Plugin Install jekyll-last-modified-at gem by adding it onto your Gemfile or installing it into your system-wide gem. $ gem install jekyll-last-modified-at In your Jekyll site’s _config.yml file: # Add this under gems gems: - jekyll-last-modified-at Write your special RSS feed In /path/to/jekyll/feed/instant-articles.xml, copy the following snippet into it. Enjoy! Now when you access http://jekyll-site/feed/instant-articles. Playing around with Facebook's Instant Articles https://dchua.com/posts/2016-04-21-playing-around-with-facebooks-instant-articles/ Thu, 21 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-21-playing-around-with-facebooks-instant-articles/ Over lunch yesterday, Lester introduced me to how quick and easy setting up the newly available Facebook Instant Articles was. After playing around with it a little bit by first generating an IA compatible RSS feed using Jekyll, (Jekyll Feed Layout), I managed to get it ingested into the system. But looks like there’s still quite a fair bit of caveats. Custom CSS will not work. Which means, code highlighting syntaxes will not render. A Python RethinkDB walkthrough https://dchua.com/posts/2016-04-13-a-rethinkdb-python-walkthrough/ Wed, 13 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-13-a-rethinkdb-python-walkthrough/ RethinkDB is a modern NoSQL document-based database that baked in push/pull features into its database core which allows developers to build realtime applications without having to add a websocket/messaging layer ontop of their existing stack. If you’re familar with MongoDB and its schemaless document structure, RethinkDB works in a similar manner with flexible schemas. With RethinkDB, you can write a client in any language (Python, JS, Ruby etc…) to listen to specific database queries and perform operations when there’s a change in the database. Event Loop Tasks https://dchua.com/posts/2016-04-11-event-loop-tasks/ Mon, 11 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-11-event-loop-tasks/ A pictorial to understanding async loopings Source: Asyncio Task - Python Documentation Packaging your Python Packages into PyPI (pip) https://dchua.com/posts/2016-04-09-packaging-your-python-packages-into-pypi-pip/ Sat, 09 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-09-packaging-your-python-packages-into-pypi-pip/ This is a quick walkthrough on submiting your module for a package. Most of it has been distiled from Peter Down’s article so any credits please send it his way. I’ve modified the walkthrough to use setuptools instead of disutils For a more verbose account, please read his article. First-time PyPI packagers Make sure you register an account at both https://pypi.python.org and https://testpypi.python.org Create a .pypirc file in your ~/. Installing GRPC, Protobuf and its dependencies for Python Development https://dchua.com/posts/2016-04-08-installing-grpc-protobuf-and-its-dependencies-for-python-development/ Fri, 08 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-08-installing-grpc-protobuf-and-its-dependencies-for-python-development/ Been having some trouble getting my local machine setup for GRPC development lately. Hopefully this quick walkthrough will help someone out. Prerequisites You’re on the Python 2.X branch. Python 3.X is currently not supported for the official grpc library Setup Install Protobuf $ pip install "protobuf>=3.0.0a2" Note: In order to support protobuf syntax 3, you’ll need to ensure your version of protobuf is at least 3. Debugging Websockets with wscat (Websocket CLI Terminal) https://dchua.com/posts/2016-04-01-debugging-websockets-with-wscat/ Fri, 01 Apr 2016 22:32:09 +0800 https://dchua.com/posts/2016-04-01-debugging-websockets-with-wscat/ So I’m currently working on a Websocket implementation from almost-scratch (Tornado/Websockets) and I’m working on implementing some form of rooms/channel functionality. This is mostly just for my learning experience to dive deeper into Python. A cool tool that I’ve just discovered is helping me shave off the time needed to do debugging to a bare minimum. Introducing wscat, a nodejs cli application that allows you to connect directly to a websocket server and receive/send data in a terminal like environment. Converting KML into GeoJSON for Mongoimport & Querying https://dchua.com/posts/2016-03-31-converting-kml-into-geojson-for-mongoimport/ Thu, 31 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-31-converting-kml-into-geojson-for-mongoimport/ First, you need to install kml2geojson. Luckily, there’s pip. $ pip install kml2geojson Once that’s done, you can take a .kml file and convert it into a GeoJSON format. Why GeoJSON? Because its gaining in support (MongoDB, RethinkDB) and has lots of smart people working on its specification. $ kml2geojson <.kml> <output_path> Now if you’re looking to import the geoJSON data into MongoDB so that you can do geospatial queries, you’ll need to prepare the GeoJSON file for import so that MongoDB will create multiple documents rather than a single document of your geo-objects. Debugging in Python https://dchua.com/posts/2016-03-30-debugging-in-python/ Wed, 30 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-30-debugging-in-python/ Miss being able to inspect, debug and run expressions in between your code? I’ve heard so much about iPython’s debugging functionality and it turns out there’s a way to make use of it without having to use iPython itself. Meet ipdb. With just a couple of simple steps: import ipdb print("before break") ipdb.set_trace() print("after break") You’ll drop into a nice interactive shell for your debugging need. Most syntaxes are similar to ruby-debug, so if you’re coming from ruby, you shouldn’t have any issue. Understanding Python Decorators https://dchua.com/posts/2016-03-29-understanding-python-decorators/ Tue, 29 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-29-understanding-python-decorators/ Decorators are functions that replaces an existing function with another function with the original function as a parameter. A decorator function would have the following structure: def decorator_name(func): def func_wrapper(*args) return "any content" + str(*args) return func_wrapper Sometimes, you might see functions that are defined like: @some_function def do_stuff(*args): return str(*args) @some_function in this case is a decorator. What the above actually translate to in pseudo-code is: Whenever do_stuff() is called, return the output of some_function() which takes in the output of do_stuff(). Running virtualenvwrapper with pyenv https://dchua.com/posts/2016-03-27-running-virtualenvwrapper-with-pyenv/ Sun, 27 Mar 2016 13:10:46 +0800 https://dchua.com/posts/2016-03-27-running-virtualenvwrapper-with-pyenv/ Virtualenvwrapper is a great way to manage your different python package environment. How would you then manage the different package environment ontop of your different python environments? Pyenv-virtualenvwrapper attempts to answer that question by allowing your virtualenvwrapper to be used in conjunction with your pyenv. If you don’t have virtualenvwrapper or virtualenv installed, don’t fret, you can still follow the tutorial, pyenv-virtualenvwrapper would install all the necessary dependencies. Prerequisite: Have Pyenv installed Installation Installation is pretty quick, just do a simple: Installing pyenv - RVM for Python https://dchua.com/posts/2016-03-25-installing-pyenv-rvm-for-python/ Fri, 25 Mar 2016 11:02:06 +0800 https://dchua.com/posts/2016-03-25-installing-pyenv-rvm-for-python/ One of the things I’ve missed while learning python, is the ability to manage and switch between python versions. In Ruby, we often use tools like RVM or rbenv to handle the different ruby versions. This is particularly useful when you have projects that have to rely on an older version of ruby and you don’t want to mess up your local environment. In python, there’s pyenv. To set it up, its really quite simple. Writing a Serverless Python Microservice with AWS Lambda and AWS API Gateway https://dchua.com/posts/2016-03-22-writing-a-serverless-python-microservice-with-aws-lambda-and-aws-api-gateway/ Tue, 22 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-22-writing-a-serverless-python-microservice-with-aws-lambda-and-aws-api-gateway/ Serverless Architectures are the rage nowadays. In this article, I’ll attempt to walkthrough the process of writing a Serverless Microservice with AWS Lambda and Amazon API Gateway. For people who know me, know that I’m not much of a Pythonista, but doing this blog post helped me to better appreciate Python and its awesome standard libraries and while I could have done this in NodeJS, this was a perfect example to pick up Python while doing something new. Disable wordwrapping for easier markdown composing in vim https://dchua.com/posts/2016-03-21-disable-wordwrapping-for-easier-markdown-composing-in-vim/ Mon, 21 Mar 2016 23:32:45 +0800 https://dchua.com/posts/2016-03-21-disable-wordwrapping-for-easier-markdown-composing-in-vim/ If you’re writting a really long prose in markdown, you probably have experienced the annoying thing that vim does when dealing with a long line; it creates linebreaks when your textwidth value is met. This gets absolutely annoying especially since markdown reads linebreaks as linebreaks and displays it as such when rendered. Fret not, a quick way is to set your autocmd in your .vimrc to disable said wrapping for markdown files or any file extension that you’d like. Getting npm packages to be installed with docker-compose https://dchua.com/posts/2016-02-07-getting-npm-packages-to-be-installed-with-docker-compose/ Sun, 07 Feb 2016 00:00:00 +0000 https://dchua.com/posts/2016-02-07-getting-npm-packages-to-be-installed-with-docker-compose/ If you’re trying to deploy your node app into a docker container and you’re also using docker-compose, here’s something to watch out for. If you are running npm install on your Dockerfile, you might want to make sure that you mount /node_modules as a data volume in your docker-compose. For eg. your Dockerfile might look something like this: # Dockerfile FROM node:5.5.0 ADD ./ /node_app WORKDIR /node_app RUN npm install Normal stuff. 4 Tips to Supercharge your Jekyll Static Site Hosting https://dchua.com/posts/2016-01-21-proper-jekyll-deployment-workflow-to-s3-static-hosting/ Thu, 21 Jan 2016 00:00:00 +0000 https://dchua.com/posts/2016-01-21-proper-jekyll-deployment-workflow-to-s3-static-hosting/ Supercharge your Jekyll site with these 4 tips. Put your Jekyll into a Git Repository One of the best part of being in a command-line environment is that you have the full power of the tools available to you by your OS environment. Why not make sure you never lose track of your blog posts and revisions by using the power and flexibility of Git. By committing your blog posts and changes as individual commits, you can easily revert back or track changes you make easily at your own convenience. SSH Agent Forwarding with your Docker Container https://dchua.com/posts/2016-01-15-ssh-agent-forwarding-with-your-docker-container/ Fri, 15 Jan 2016 00:00:00 +0000 https://dchua.com/posts/2016-01-15-ssh-agent-forwarding-with-your-docker-container/ Quick tip: If you’re building your docker container to deploy an application, you’d most likely need to pull your git repository code into the container. Instead of copying your ssh-keys into the container (please don’t), why not use the tried and tested method of ssh-agent forwarding. To setup ssh-agent forwarding with your docker container, just start your container with this: docker run --rm -t -i -v $SSH_AUTH_SOCK:/ssh-agent -e SSH_AUTH_SOCK=/ssh-agent my_image /bin/bash Now you can use your host system’s ssh-keys to pull from your git repositories! Using Graylog Extractors to Split Logs https://dchua.com/posts/2015-12-25-using-graylog-extractors/ Fri, 25 Dec 2015 00:00:00 +0000 https://dchua.com/posts/2015-12-25-using-graylog-extractors/ If you’re new to Graylog, Graylog Extractors are a great way to pull out information from your logs for easier storing and manipulation. If like me, you sometimes experience a “String fields longer than 32kb” indexing error on one of your fields, a good way to help mitigate it is to use extractors to split your field into two. I wouldn’t recommend this all the time as obviously the way to solve this is to set your particular field to be non_indexable but in certain cases when you need the full data and still want to be able to search for it, this might be the better solution. Getting Multi-threaded Thin to Work in Development (Rails 4) https://dchua.com/posts/2015-12-03-getting-thin-to-work-in-development/ Thu, 03 Dec 2015 00:00:00 +0000 https://dchua.com/posts/2015-12-03-getting-thin-to-work-in-development/ Rails 4 If you’re attempting to run thin to work in threaded mode for the very first time, you might encounter that no matter what you do, your site won’t load. Running $ thin start --threaded Will return the typical startup output >> Thin web server (v1.5.1 codename Straight Razor) >> Maximum connections set to 1024 >> Listening on 0.0.0.0:3005, CTRL+C to stop But you’ll never be able to access the site no matter what you do. Timestamp your bash output https://dchua.com/posts/2015-10-24-timestamp-your-bash-output/ Sat, 24 Oct 2015 00:00:00 +0000 https://dchua.com/posts/2015-10-24-timestamp-your-bash-output/ If you need to find out the time difference between bash outputs, you can use ts from the moreutils package. To get started, you’ll need to install moreutils. A simple apt-get install moreutils should do the trick. Then all you need to do is to pipe your output to ts Like: echo -e "test\ntest\ntest" | ts This will return a nice timestamp on each linebreak. You can even customize the timestamp like: Using Superagent in your Browser https://dchua.com/posts/2015-09-29-using-superagent-in-your-browser/ Tue, 29 Sep 2015 00:00:00 +0000 https://dchua.com/posts/2015-09-29-using-superagent-in-your-browser/ Quick tip. I love superagent. Its a great lightweight JS httpclient that I use in almost all of my node apps that needs one. But what if you want to use it in your browser frontend and you don’t know how? All it takes: Add the Javascript <script src="https://cdnjs.cloudflare.com/ajax/libs/superagent/1.2.0/superagent.min.js"></script> Call it! <script> superagent .get('/') .end(function(err,res){ // do everything you want to res }) </script> Thats it! No need to declare anything. Playing Hearthstone on Linux https://dchua.com/posts/2015-09-23-playing-hearthstone-on-linux/ Wed, 23 Sep 2015 00:00:00 +0000 https://dchua.com/posts/2015-09-23-playing-hearthstone-on-linux/ Today’s tutorial will be on how to install and play the popular Blizzard Online Card Game, Hearthstone, on Linux. Yes, it is entirely possible and not really that difficult to get started. Ready? Lets go. Install PlayOnLinux I’m on Ubuntu upstream, so the simplest way for me is to: wget -q "http://deb.playonlinux.com/public.gpg" -O- | sudo apt-key add - sudo wget http://deb.playonlinux.com/playonlinux_trusty.list -O /etc/apt/sources.list.d/playonlinux.list sudo apt-get update sudo apt-get install playonlinux If you’re on other distributions, don’t worry, the other ways are all well-documented here. Parsing S3 Logs with GoAccess and s3cmd Sync https://dchua.com/posts/2015-09-03-parsing-s3-logs/ Thu, 03 Sep 2015 00:00:00 +0000 https://dchua.com/posts/2015-09-03-parsing-s3-logs/ In this post, I’d like to demostrate a way of analyzing logs gathered through Amazon AWS’s log collection using the awesome tool GoAccess as well as going through a quick walkthrough in setting up S3cmd Sync to pull down your logs quickly from your S3 buckets. Why GoAccess? So far for me, GoAccess has a great suite of analytic tools from Terminal outputs like: To HTML reports like: Lets get started! Change your Vim Colorscheme https://dchua.com/posts/2015-08-24-change-your-vim-colorscheme/ Mon, 24 Aug 2015 00:00:00 +0000 https://dchua.com/posts/2015-08-24-change-your-vim-colorscheme/ Step 1: Install the awesome vim-colorschemes If you’re using vim + vundle like me: " add to .vimrc Plugin 'flazz/vim-colorschemes' :PluginInstall Step 2: Choose your colorscheme There’s a pretty cool site that aggregates most of the available colorschemes on github and previews them for you. You can check them out at vimcolors.com Step 3: Install your colorscheme Most colorschemes are pretty easy to install. To get you started, you can clone flazz/vim-colorschemes and just copy the colors/*. Harden your SSL Security - The site is using outdated security. https://dchua.com/posts/2015-07-10-harden-your-ssl-security/ Fri, 10 Jul 2015 00:00:00 +0000 https://dchua.com/posts/2015-07-10-harden-your-ssl-security/ This article should help you get a A-grade in SSLLabs’s SSL Testing . Backstory As of 26 September 2014, Chrome (and also Firefox) have started the process to sunset SHA1 certification support and will give the above error if you’re using a certificate that is signed with SHA1. If you’ve just installed a newly issued SSL cert from your certification authority, you should be already be issued with a signed certificate in SHA-256 as most CA have already stopped signing them with SHA1. Setting up OpenVPN AS with FreeRADIUS AND stunnel https://dchua.com/posts/2015-07-08-setting-up-openvpn-as-with-freeradius-and-stunnel/ Wed, 08 Jul 2015 00:00:00 +0000 https://dchua.com/posts/2015-07-08-setting-up-openvpn-as-with-freeradius-and-stunnel/ If you’re ever curious as to how do VPN service providers manage all their users and their authentication. I hope this might give you a little peek. This article will serve to setup FreeRADIUS as the authentication mechanism, OpenVPN as the VPN protocol and stunnel as an introduction to obfuscating censorship. With all these setup, you’re one step closer to start providing VPN services. Setup FreeRADIUS Install Freeradius Centos $ yum install freeradius freeradius-mysql Create Freeradius Database Install MySQL Find out which processes is using your swap https://dchua.com/posts/2015-05-27-find-out-which-processes-is-using-your-swap/ Wed, 27 May 2015 00:00:00 +0000 https://dchua.com/posts/2015-05-27-find-out-which-processes-is-using-your-swap/ To find out which process is using your swap memory, use smem $ sudo apt-get install smem # To get a full list of PID and the amount of memory and swap they use $ smem -s swap # To get a summary of memory usage by user $ smem -u References: Find out what is using your swap SMEM memory reporting tool Fix freezing mouse-click issue on Spotify Linux https://dchua.com/posts/2015-05-05-fix-freezing-mouse-click-issue-on-spotify-linux/ Tue, 05 May 2015 00:00:00 +0000 https://dchua.com/posts/2015-05-05-fix-freezing-mouse-click-issue-on-spotify-linux/ If you’re running Linuxmint/Ubuntu and you’re experiencing strange issues with your mouse click being disabled across the entire desktop after running spotify, you might experiencing this issue To fix this, go to your file: # ~/.config/spotify/Users/<username>/prefs # add the following ui.track_notifications_enabled=false Debugging Redis and its Memory https://dchua.com/posts/2015-04-28-debugging-redis-and-its-memory/ Tue, 28 Apr 2015 00:00:00 +0000 https://dchua.com/posts/2015-04-28-debugging-redis-and-its-memory/ To determine the size of a key redis-cli> DEBUG OBJECT <keyname> Value at:0x7ffc8f8eb330 refcount:1 encoding:linkedlist serializedlength:1175841791 lru:416416 lru_seconds_idle:30 serializedlength is in bytes. To print out the size of each key in your redis-database Checkout this script To tweak Redis from overloading your memory Add vm.overcommit_memory = 1 to /etc/sysctl.conf and then reboot or run the command sysctl vm.overcommit_memory=1 for this to take effect immediately. To limit the size of your redis database # /etc/redis/redis. Configuring Chef https://dchua.com/posts/2015-03-24-configuring-chef/ Tue, 24 Mar 2015 00:00:00 +0000 https://dchua.com/posts/2015-03-24-configuring-chef/ Preparing Cookbooks Lets prepare a cookbook. Uploading a cookbook $ knife cookbook upload -a Databags Creating a databag $ knife data_bag create 'users' Uploading a databag $ knife data_bag from file users path/to/data_bag/files Bootstraping Create a new node In order to bootstrap a server to get and use chef, ensure that you have ssh access to your server already. You can define which user to login as using the –ssh-user command below. Setting up Chef https://dchua.com/posts/2015-03-24-setting-up-chef/ Tue, 24 Mar 2015 00:00:00 +0000 https://dchua.com/posts/2015-03-24-setting-up-chef/ Prerequisite: Chef 12.1.x Setting up Chef-Server Download Chef on your Chef-Server $ wget https://web-dl.packagecloud.io/chef/stable/packages/ubuntu/trusty/chef-server-core_12.0.5-1_amd64.deb $ sudo dpkg -i chef-server-core_*.deb $ sudo chef-server-ctl reconfigure Install Opscode-Manage Web interface for Chef management (highly recommended) Note: Opscode-Manage will run on port 80 so keep that free. # On Chef-server $ chef-server-ctl install opscode-manage $ opscode-manage-ctl reconfigure $ chef-server-ctl reconfigure Setup your User and Organization Users are an account that will be used to connect to Chef-server. Ignore files already committed to Git https://dchua.com/posts/2015-01-03-ignore-files-already-committed-to-git/ Sat, 03 Jan 2015 00:00:00 +0000 https://dchua.com/posts/2015-01-03-ignore-files-already-committed-to-git/ Have you ever have a situation where you do not want your changes to a particular file to be tracked but you still need it in the repository for future developers? An example of this would be the new config/secrets.yml in Rails 4.x secrets.yml acts as a replacement for .env files but unlike .env files, you need secrets.yml in your repository in order not to break your app. With secrets.yml already committed into your application by default, how do you untrack future changes to it? How to Test for File Operations with FakeFS and RSpec https://dchua.com/posts/2014-12-24-how-to-test-for-file-operations-with-fakefs-and-rspec/ Wed, 24 Dec 2014 00:00:00 +0000 https://dchua.com/posts/2014-12-24-how-to-test-for-file-operations-with-fakefs-and-rspec/ Background: Say you have controller method that creates and writes into a file and you’d like to be able to run expectations on the behavior and not so much of the implementation. The normal way would be to do something like: def test_creates_directory FileUtils.expects(:mkdir).with("directory").once Library.add "directory" end because this just doesn’t work. def test_creates_directory Library.add "directory" assert File.directory?("directory") end So one way to solve this is with this gem called FakeFS. How to make a S3 bucket publicly accessible https://dchua.com/posts/2014-12-13-how-to-make-a-s3-bucket-publicly-accessible/ Sat, 13 Dec 2014 00:00:00 +0000 https://dchua.com/posts/2014-12-13-how-to-make-a-s3-bucket-publicly-accessible/ In your AWS Console, select the Bucket that you’re trying to make public. Under Permissions, add the following bucket policy: { "Version": "2008-10-17", "Id": "...", "Statement": [ { "Sid": "...", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::<REPLACE_THIS_WITH_YOUR_BUCKET_NAME>/*" } ] } How to setup Fluentd to Log to S3 with Rails https://dchua.com/posts/2014-11-25-how-to-setup-fluentd-to-log-to-s3/ Tue, 25 Nov 2014 00:00:00 +0000 https://dchua.com/posts/2014-11-25-how-to-setup-fluentd-to-log-to-s3/ Logging is important. When you’re running a fully fledge production application server, you do not have the luxury of monitoring your rails console all the time. In this example, I will go through with you the process of setting up Fluentd with your Rails App to log data and to upload them onto S3 for persistent archival purposes. Pre-requsite Running Trusty (14.04) Ruby 2.1.2 installed An AWS Account with your AWS Credentials (key and token) ready Step 1: Download Td-Agent Wait, isn’t this a Fluentd tutorial? How to add a confirmation dialog box in AngularJS https://dchua.com/posts/2014-11-19-how-to-add-a-confirmation-dialog-box-in-angularjs/ Wed, 19 Nov 2014 00:00:00 +0000 https://dchua.com/posts/2014-11-19-how-to-add-a-confirmation-dialog-box-in-angularjs/ Setting up a confirmation box can be quite tricky, here’s how to quickly get a confirmation box setup in AngularJS. Pre-requisite AngularJS is already loaded and running You are using real jQuery instead of jqLite (I’ll explain below) Prepare your HTML First, start off with a blank HTML page. Create a link that you would like to run a function() on when clicked and confirmed. It should look something like this: Fix AngularJS from loading twice error on Rails 4 https://dchua.com/posts/2014-11-16-fix-angularjs-from-loading-twice-error-on-rails-4/ Sun, 16 Nov 2014 00:00:00 +0000 https://dchua.com/posts/2014-11-16-fix-angularjs-from-loading-twice-error-on-rails-4/ If you’re running a Rails 4.x app with AngularJS, and you’re getting increasingly frustrated with receiving “AngularJS loaded twice” errors and you can’t seem to figure out what went wrong, perhaps you might want to make sure you do two things first: First, remove Turbolinks Gem Remove require ‘turbolinks’ from your application.js It is not apparent at first but turbolinks inject its own form of javascript into your views and when run, it will actually overwrite your normal angular behavior. How to Start a Static Blog in 10 minutes with Jekyll and Terminal https://dchua.com/posts/2014-11-13-how-to-start-a-static-blog-in-10-minutes-with-jekyll-and-terminal/ Thu, 13 Nov 2014 00:00:00 +0000 https://dchua.com/posts/2014-11-13-how-to-start-a-static-blog-in-10-minutes-with-jekyll-and-terminal/ Run your own Terminal Blog with Terminal You probably have seen or heard of Medium.com or at least the bunch of awesome articles created by its huge community of writers. The platform has been gaining popularity with beginning writers because of its clean design and easy to use input interface but one of the increasingly frustrating limitations is the lack of support for custom domains. You have a really cool domain name that you don’t want to go to waste, why let Medium get all the traffic? How to fix BigDecimal returning as Strings in Rails https://dchua.com/posts/2014-11-06-activesupport-encodes-bigdecimals-as-strings/ Thu, 06 Nov 2014 00:00:00 +0000 https://dchua.com/posts/2014-11-06-activesupport-encodes-bigdecimals-as-strings/ Something that I’ve just found out recently. Basically, Rails serializes BigDecimal objects as JSON strings because they didn’t want its values to be misintepreted when deserialized. But in Rails 4.0.x, an option to reverse that behavior was introduced in the form of: # config/application.rb ActiveSupport.encode_big_decimal_as_string = false # default = true If you’re on Rails 4.0.x, adding the above will ensure that the values returned will be numbers. If your Rails API is providing response data that contains numbers, you want to make sure that you are not accidentally encoding big decimals as string, which would cause a problem when trying to do arithmetic calculations with it. How to Move your Rubygems from one Ruby version to another in RVM https://dchua.com/posts/2014-11-04-moving-your-rubygems-from-one-ruby-version-to-another-in-rvm/ Tue, 04 Nov 2014 00:00:00 +0000 https://dchua.com/posts/2014-11-04-moving-your-rubygems-from-one-ruby-version-to-another-in-rvm/ Today’s post is going to be a pretty short one. So you know how sometimes when requirements change and your entire project have to change (either to upgrade or to downgrade) from one Ruby version to another, you don’t want to have to reinstall and setup your gems all over again? Here’s a quick little tip to quickly move your gems from one version to another. Assuming that you’re currently using Ruby 2. How to Generate PDFs with HTML templates in 10 minutes with Rails https://dchua.com/posts/2014-10-30-generate-pdfs-with-html-templates-in-rails/ Thu, 30 Oct 2014 00:00:00 +0000 https://dchua.com/posts/2014-10-30-generate-pdfs-with-html-templates-in-rails/ Generating PDF for your webapplication is not that difficult as you think. There are many ways to do this, and there’s another great gem out there that does this functionality quite well too, called Prawn, but for the purpose of todays' tutorial, I’ll be going through another popular gem called wicked_pdf instead. WickedPDF uses wkhtmltopdf which allows you to generate PDFs directly from HTML without having to adhere to a DSL. Run a webrick server to serve static html content in a folder https://dchua.com/posts/2014-09-19-run-a-webrick-server-to-serve-static-html-content-in-a-folder/ Fri, 19 Sep 2014 00:00:00 +0000 https://dchua.com/posts/2014-09-19-run-a-webrick-server-to-serve-static-html-content-in-a-folder/ Need a quick way to serve html files? Want to quickly share your static website to the rest of the world, this snippet might help: /directory/to/static/html$ ruby -run -e httpd . -p 9000 Reference: Tenderlove’s Twitter Figure out which DNS server you are using https://dchua.com/posts/2014-09-17-figure-out-which-dns-server-you-are-using/ Wed, 17 Sep 2014 00:00:00 +0000 https://dchua.com/posts/2014-09-17-figure-out-which-dns-server-you-are-using/ If you need to find out what DNS you are currently using (for troubleshooting purposes, this snippet might help): $ tcpdump udp and src port 53 $ tcpdump udp and dst port 53 $ tcpdump -n -s 1500 -i eth0 udp port 53 These 3 commands will sniff your packets for port 53 interactions and will let you know what is the ip address of the DNS server that you’re interacting with. Link your custom app to your Gnome Applications https://dchua.com/posts/2014-09-12-link-your-custom-app-to-your-gnome-applications/ Fri, 12 Sep 2014 00:00:00 +0000 https://dchua.com/posts/2014-09-12-link-your-custom-app-to-your-gnome-applications/ If you’re running GNOME and you have a custom script/app that you want to be able to run quickly from your applications bar, you can easily add an entry with the following: In your ~/.local/share/applications or /usr/share/applications folder (choose one depending on whether you want to share with all your users or yourself), add the following entry: # whateverfile.desktop [Desktop Entry] Type=Application Encoding=UTF-8 Name=Sample Application Name Comment=A sample application Exec=application Icon=application. Authenticating with Pagekite API using Ruby https://dchua.com/posts/2014-08-31-authenticating-with-pagekite-api-using-ruby/ Sun, 31 Aug 2014 00:00:00 +0000 https://dchua.com/posts/2014-08-31-authenticating-with-pagekite-api-using-ruby/ If you’re using pagekite, you probably have read this document. It tells you how to connect to pagekite’s XMLRPC service to perform actions such as creating a new kite, checking your accounts etc. Their example is in python and if you’re like me, trying to figure out how to get it to work in Ruby, here’s my snippet: # lets do this - test.rb require 'xmlrpc/client' server = XMLRPC::Client.new_from_uri('https://pagekite.net/xmlrpc/') # note that you really need that trailing slash after /xmlrpc. MySQL backup process https://dchua.com/posts/2014-08-28-mysql-backup-process/ Thu, 28 Aug 2014 00:00:00 +0000 https://dchua.com/posts/2014-08-28-mysql-backup-process/ How I usually prepare the backup regime of my mysql/mariadb databases of all my newly provisioned servers is to do the following tasks: Create a DBA user > create user 'dba'@'localhost'; Give the DBA user only the relevant access just for backing > GRANT SELECT, SHOW VIEW, RELOAD, REPLICATION CLIENT, EVENT, TRIGGER ON *.* TO 'dba'@'localhost'; > GRANT LOCK TABLES ON *.* TO 'dba'@'localhost'; > FLUSH PRIVILEGES; Setup a backup script! Configure legacy timestamp attributes in Activerecord https://dchua.com/posts/2014-08-19-configure-legacy-timestamp-attributes-in-activerecord/ Tue, 19 Aug 2014 00:00:00 +0000 https://dchua.com/posts/2014-08-19-configure-legacy-timestamp-attributes-in-activerecord/ In ActiveRecord, when working with legacy database and the existing database uses different fields for last_update and created_at, how do you specify the correct column names for ActiveRecord to properly record timestamps? # in your config/initializers directory, create a new file # `active_record_patch.rb` # overload module ActiveRecord module Timestamp private def timestamp_attributes_for_create #:nodoc: [:created_at, :created_on, :create_date, :post_date] end def timestamp_attributes_for_update #:nodoc: [:modtime, :updated_on, :modified_at, :updated_at, :last_update] end end end Equivalent of try() for hashes? - Ruby https://dchua.com/posts/2014-08-17-equivalent-of-try-for-hashes-ruby/ Sun, 17 Aug 2014 11:41:36 +0800 https://dchua.com/posts/2014-08-17-equivalent-of-try-for-hashes-ruby/ To do a try() on hashes, its as simple as: # to get data['something_value'] @item = @model.try(:data).try(:[], 'something_value'] 2021 Update From ruby 2.3.0 you can now do the following shorthand @model&.data&.[](:something_value) Reference: Safe navigation equivalent to Rails try for hashes References: Equivalent of .try() for a hash? Migrating from utf8 to utf8mb4 in MySQL https://dchua.com/posts/2014-08-15-migrating-from-utf8-to-utf8mb4-in-mysql/ Fri, 15 Aug 2014 00:00:00 +0000 https://dchua.com/posts/2014-08-15-migrating-from-utf8-to-utf8mb4-in-mysql/ If you ever get this error message: Incorrect string value: '\xF0\x90\x8D\x83\xF0\x90...' for column 'content' at row You probably have experienced at one point, the horrors that is mysql’s utf8 4-byte encoding. If you’re using MySQL 5.4 and below, there’s no way to solve this without a proper upgrade to at least MySQL 5.5. My steps: Ensure I’m using the latest version (MySQL > 5.5) Setup your /etc/mysql/my.cnf # in /etc/mysql/my. DRY your ActionMailer::Base.deliveries.clear in RSpec tests https://dchua.com/posts/2014-08-12-dry-your-actionmailer-base.deliveries.clear-in-rspec-tests/ Tue, 12 Aug 2014 12:22:03 +0800 https://dchua.com/posts/2014-08-12-dry-your-actionmailer-base.deliveries.clear-in-rspec-tests/ When testing for mailers, you might need to add the following quite frequently: describe "test mailer" do it "should work" do ActionMailer::Base.deliveries.clear # bunch of other # codes here end end You probably would add them at the start of every test block that tests for ActionMailer. The neater way would be to do this: describe "test mailer", type: :mailer do it "should work" do # bunch of # codes here end end By assigning a mailer_type to the test describe, RSpec will automatically clear the actionmailer deliveries at the start. Testing with Rack-test custom headers https://dchua.com/posts/2014-08-12-testing-with-rack-test-custom-headers/ Tue, 12 Aug 2014 00:00:00 +0000 https://dchua.com/posts/2014-08-12-testing-with-rack-test-custom-headers/ When writing a method that requires a custom request header, ie. “X-Api-Key”, use the following: key = request.headers['HTTP_X_API_KEY'] Instead of key = request.headers['X-Api-Key'] This is because Rack converts all custom headers into the HTTP_ format. When writing your tests, remember to specify like the following get '/test', {}, {"HTTP_X_API_KEY" => "key"} Even though when in your actual query, you’re sending the header as X-Api-Key Things to do with a new Ubuntu Installation https://dchua.com/posts/2014-07-21-things-to-do-with-a-new-ubuntu-installation/ Mon, 21 Jul 2014 00:00:00 +0000 https://dchua.com/posts/2014-07-21-things-to-do-with-a-new-ubuntu-installation/ This post will be constantly updating for new information Step 1: Setup Swap space sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=2048 # count is in MB sudo /sbin/mkswap /var/swap.1 sudo /sbin/swapon /var/swap.1 Setup 2:Setup IPv4 Precedence To ensure that your apt-gets are not slowed down due to your system grabbing repository files over IPv6 # in /etc/gai.conf # uncomment this line prescedence ::ffff:0:0/96 100 X11 forwarding over remote SSH tmux https://dchua.com/posts/2014-07-15-x11-forwarding-over-remote-ssh-tmux/ Tue, 15 Jul 2014 00:00:00 +0000 https://dchua.com/posts/2014-07-15-x11-forwarding-over-remote-ssh-tmux/ I’m starting to do all my development remotely on a development server that I’m hosting at home over a VPN. This means that all my devices are basically thin-clients. Pre-requisite Make sure that you’ve already allowed X11 forwarding over your remote user account. If not, do this (in your remote server logged in as your remote user account) # in ~/.ssh/config (create if its not there) # add the following ForwardX11 yes One of the problems I had encountered was attempting to do X11 forwarding over TMUX. Creating a custom jekyll-markdown snippet https://dchua.com/posts/2014-06-19-creating-a-custom-jekyll-markdown-snippet/ Thu, 19 Jun 2014 00:00:00 +0000 https://dchua.com/posts/2014-06-19-creating-a-custom-jekyll-markdown-snippet/ In the time spent writing the previous blog post, I come to realize that I really need a snippet to help me with the highlight tags. I’m using Jekyll to run this journal and with Jekyll comes a really cool liquid template that helps make pasting code snippets easy. For example, to write a ruby codeblock, I’d need to: To do this multiple times, it is quite a mouthful of keystrokes. Creating my first Rack Middleware with Rack::ContentLength https://dchua.com/posts/2014-06-19-creating-my-first-rack-middleware/ Thu, 19 Jun 2014 00:00:00 +0000 https://dchua.com/posts/2014-06-19-creating-my-first-rack-middleware/ Yeah, I’m probably pretty slow in the game but I think I’ve finally wrapped my head around all the countless Rack Middlewares out there. The goal of this was to create a response_logger that intercepts the response object on the middleware level and send out a log with the size of the JSON object that it returns for future reference. I first setup Rack::ContentLength, which is a great Middleware that’s available out of the box if you’re on Rails. Stub a Rails Environment in RSpec https://dchua.com/posts/2014-06-11-stub-a-rails-environment-in-rspec/ Wed, 11 Jun 2014 00:00:00 +0000 https://dchua.com/posts/2014-06-11-stub-a-rails-environment-in-rspec/ When writing tests to test for rails.env specific functions, in order to temporarily change the rails environment during testing, use this: Rails.stub(env: ActiveSupport::StringInquirer.new("production")) This will then pass when doing a Rails.env.production? Add leading zeroes in Ruby https://dchua.com/posts/2014-06-07-add-leading-zeroes-in-ruby/ Sat, 07 Jun 2014 00:00:00 +0000 https://dchua.com/posts/2014-06-07-add-leading-zeroes-in-ruby/ To add leading zeroes infront of a number in ruby, try this: # with 5 being the number you want to have the leading zeroes added to irb> "%03d" % 5 => 005 # 3 leading 0 digits with number 5 . Source: How can I output leading zeroes in ruby - Stackoverflow Getting Passenger_pre_start to load Rails Apps on load https://dchua.com/posts/2014-06-07-getting-passenger_pre_start-to-load-rails-apps-on-load/ Sat, 07 Jun 2014 00:00:00 +0000 https://dchua.com/posts/2014-06-07-getting-passenger_pre_start-to-load-rails-apps-on-load/ If you’re like me and you’ve already set: passenger_pre_start http://somesite_to_load/; But yet your application doesn’t load up on restart. Fret not, its probably because your pathing is screwed. To troubleshoot, take a look at your nginx error.log. You’ll probably see something like this even after you restart: # /opt/nginx/logs/error.log /usr/bin/env: ruby: No such file or directory This is because nginx is trying to load your site using ruby and you don’t have ruby in your path. Ruby Multithreading with Threadwait https://dchua.com/posts/2014-05-22-ruby-multithreading-with-threadwait/ Thu, 22 May 2014 00:00:00 +0000 https://dchua.com/posts/2014-05-22-ruby-multithreading-with-threadwait/ Multithreading is nothing new in Ruby but its only in recent times that I’ve found a use for it. Unlike background processes which runs your task on its own instance independently from the main rails app, Multithreading also runs each threads separately but needs to join back with the main thread in order to continue. An example use: @groups.each do |t| threads << Thread.new do t.process_something end end # Group def process_something # calls external resources and wait for a reply. User Agent Testing in Rack/Test https://dchua.com/posts/2014-05-06-user-agent-testing-in-rack-test/ Tue, 06 May 2014 00:00:00 +0000 https://dchua.com/posts/2014-05-06-user-agent-testing-in-rack-test/ When using Rack::Test, to mock out the user-agent of the request, do something like: get "/activate/", {}, {'HTTP_USER_AGENT' => "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A501 Twitter for iPhone"} Enable MySQL Slow Log Query without restarting https://dchua.com/posts/2014-04-10-enable-mysql-slow-log-query-without-restarting/ Thu, 10 Apr 2014 00:00:00 +0000 https://dchua.com/posts/2014-04-10-enable-mysql-slow-log-query-without-restarting/ So sometimes in the midst of trying to find out what’s went wrong with our mysql query and why is it so slow, we enable mysql slow query log. Without having to restart your mysql daemon, here’s what you can do (if you’re running MYSQL 5.1 and above): # In your mysql console > SET GLOBAL slow_query_log = 'ON'; > FLUSH LOGS; Now head on over to /var/log/mysql/ and find your logs! Include Rack/Test to perform API Testing https://dchua.com/posts/2014-03-30-include-rack-test-to-perform-api-testing/ Sun, 30 Mar 2014 00:00:00 +0000 https://dchua.com/posts/2014-03-30-include-rack-test-to-perform-api-testing/ Start testing your REST API in RSpec. In other to get the awesome functionality of writing restful routes like '/users.json' instead of the usual :update for example: # spec/controller/whatever.rb describe ... do patch '/users.json' {key: 'value} expect(response).to be_success end You’ll need to use Rack/Test First, in your Gemfile: # Gemfile ... gem "rack-test", require: "rack/test" Then, in your spec/spec_helper.rb, Add the following in the outer block: # spec/spec_helper.rb module ApiHelper require 'rack/test' include Rack::Test::Methods def app Rails. Access your Raspberry Pi/UDOO via Serial https://dchua.com/posts/2014-03-24-tip-access-your-raspberry-pi-udoo-via-serial/ Mon, 24 Mar 2014 00:00:00 +0000 https://dchua.com/posts/2014-03-24-tip-access-your-raspberry-pi-udoo-via-serial/ Learnt today that it is possible to have a direct output from most minipc boards w/o having to ssh in and dmesg, or hook up another monitor to it, just to find out what’s going on. This is especially useful if you only have one monitor and don’t want to go through the trouble of switching both your keyboard/mouse combo and the monitor everytime you want to troubleshoot it (without sshing; if the internet connection is what you’re trying to troubleshoot). In OSX, SD Card transfers can be painfully slow https://dchua.com/posts/2014-03-24-in-osx/ Mon, 24 Mar 2014 00:00:00 +0000 https://dchua.com/posts/2014-03-24-in-osx/ Over the weekend, I was trying to experiment with some SD card images for the UDOO Board. When copying a new image into my SD Card, I realized that I was getting a really ridiculous slow writing speed of 1MB/s. This means that an 7GB image will take 100+ minutes just to copy. It didn’t feel right with me and doing some quick googling, I’ve found out that if I’m doing a dd directly to /dev/diskX which is where the sdcard is normally mounted, I’ll be subjected to this slow speed. Format strings dynamically in ruby https://dchua.com/posts/2014-03-16-format-strings-dynamically-in-ruby/ Sun, 16 Mar 2014 00:00:00 +0000 https://dchua.com/posts/2014-03-16-format-strings-dynamically-in-ruby/ #TIL you can format strings dynamically and neatly with: puts "%s is at %s" % [ 'John', 'Singapore' ] => John is at Singapore 2021 Update The above is similar to how we do it in go person := "John" country := "Singapore" fmt.Printf("%s is at %s", person, country) References: Stackoverflow Big Ruby 2014 Video - Throw some keys in it Got the Adafruit PiTFT Working https://dchua.com/posts/2014-03-15-got-the-adafruit-pitft-working/ Sat, 15 Mar 2014 00:00:00 +0000 https://dchua.com/posts/2014-03-15-got-the-adafruit-pitft-working/ Woohoo, got the Adafruit PiTFT working on our Raspberry Pis. What would be the best way to display content on the screen? Develop a simple python GUI app? Is there a way to render a fullscreen HTML page directly to the screen, or is there an easier way to do this? SSH Portforwarding https://dchua.com/posts/2014-03-08-ssh-portforwarding/ Sat, 08 Mar 2014 00:00:00 +0000 https://dchua.com/posts/2014-03-08-ssh-portforwarding/ When having a remote computer (A) in which you’re trying to access the port :3000, you can setup a reverse portforwarding with the following command: $ ssh -L <port that you want to access locally>:localhost:<port that you want to connect to> <user>@<ip> $ ssh -L 3000:localhost:3000 pi@myipaddress This way, you can access http://localhost:3000 and it is actually communicating with myipaddress:3000. All securely too! Carrierwave Fog - Need a local file? Use Cache! https://dchua.com/posts/2014-02-27-carrierwave-fog-need-a-local-file-use-cache-/ Thu, 27 Feb 2014 20:40:50 +0800 https://dchua.com/posts/2014-02-27-carrierwave-fog-need-a-local-file-use-cache-/ If you’re like me, using carrierwave + fog to hook up your Model witih Amazon S3, you may encounter this problem where you need to reprocess an image and you can’t do so without downloading the file locally. In my case, I need a to_file() method done on one of my assets but using Carrierwave/Fog(S3) and in order for that to happen the file needs to be a Carrierwave::SanitizedFile. So what can I do? Sum up array contents with a simple method https://dchua.com/posts/2014-02-23-sum-up-array-contents-with-a-simple-method/ Sun, 23 Feb 2014 00:00:00 +0000 https://dchua.com/posts/2014-02-23-sum-up-array-contents-with-a-simple-method/ With just: array.inject(:+) It will take an array and sum it up. >> [0,2,3,1] >> [0,2,3,1].inject(:+) => 6 Rack Multipart HTTP upload https://dchua.com/posts/2014-01-29-rack-multipart-http-upload/ Wed, 29 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-29-rack-multipart-http-upload/ TIL file uploads over Rack (multipart form upload) stores the files with a tmp filename. While the uploaded file retains its original filename in a special attribute ‘original_filename’, if you’re planning to process the uploaded file through a script that validates the file by its file extension, you’re going to have lots of problems. A possible solution is to rename it before continuing on the process. # controller def uploaded file = params[:file] x = file. Add text to end of multiple line - vim https://dchua.com/posts/2014-01-26-add-text-to-end-of-multiple-line-vim/ Sun, 26 Jan 2014 12:21:39 +0800 https://dchua.com/posts/2014-01-26-add-text-to-end-of-multiple-line-vim/ Suppose you have a whole list of text: Movies Travel Fashion (Men) Fashion (Women) Education Technology Clubbing Nightlife Fine Dining Food E-Commerce Beauty Wellness Gaming Performing Arts Sports Books Music Photography Fitness Gadgets And you want to make the whole thing an array in ruby. How do you do it in vim, quickly? First, we need to give everyone quotation marks. Using surround.vim, do: Surround the entire text using Ctrl-V In vim, type: :norm yss " This will give every single line in the selection a "" like: Better Errors not working over Vagrant https://dchua.com/posts/2014-01-22-better-errors-not-working-over-vagrant/ Wed, 22 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-22-better-errors-not-working-over-vagrant/ If you’re running the awesome better_errors over a vagrant environment and you’re wondering why on earth isn’t better_errors intercepting your exceptions. You probably need to specify in your environments/development.rb to allow for your remote IP. if defined? BetterErrors BetterErrors::Middleware.allow_ip! '192.168.178.0/24' end Source: Better_Errors Issue #210 Dotenv - Secure config in environment variables https://dchua.com/posts/2014-01-17-dotenv/ Fri, 17 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-17-dotenv/ Dotenv is one of those few gems which provide an easy way for dev. teams to properly and (at least semi)-securely manage their application environments. If you’re still storing your passwords and secret keys in a config file that is chucked in with the rest of your repository, DONT. Its always better to store everything in ENV. You don’t want your pesky co-workers to find out that your password to everything is “ilovewatchingdonkeys” right? Facebook OAuth doesnt support local testing https://dchua.com/posts/2014-01-17-facebook-oauth-doesnt-support-local-testing/ Fri, 17 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-17-facebook-oauth-doesnt-support-local-testing/ If you’re like me, trying to setup a Facebook App for a Website Oauth and having the redirect_uri set to “http://localhost:3000”, don’t bother. Facebook has regressively disallow localhost testing (or at least in non port-80). Read the bug report. Horrible, horrible move. Edit: One of FB’s support engineer in the same bug report above had indicated that non-common port access should be working and that it is actually a bug rather than a regression. UPNP IGP https://dchua.com/posts/2014-01-15-upnp-igp/ Wed, 15 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-15-upnp-igp/ #TIL in more detail how networked devices auto expose their listening ports to the external world without any user interaction on their routers. Key: Router UPnP allowing NAT traversal using the IGD Protocol. There’s even a ruby lib that helps you talk to your router to forward a port to a device behind NAT. Reference: Security Stackexchange 3D Printing and my Alma Mater - Singapore Polytechnic https://dchua.com/posts/2014-01-12-3d-printing-and-my-alma-mater-singapore-polytechnic/ Sun, 12 Jan 2014 18:32:25 +0800 https://dchua.com/posts/2014-01-12-3d-printing-and-my-alma-mater-singapore-polytechnic/ Last week, I had the opportunity to drop by my Alma Mater where I’ve spent 3 years of my post-secondary school education picking up Electronics, Computer and Communication Engineering. Its been a good 9 years since graduating and so much have changed. The buildings that I remember are mostly renovated (and is still renovating) and the entire campus is full of little construction sites, both demolition and construction. I was in Singapore Polytechnic, not out of randomness but to exhibit Fabbox a cloud-based total 3D Printing solution that my cousin and his friends started. Chrome Android and background-size: cover not playing nice https://dchua.com/posts/2014-01-09-chrome-android-and-background-size-cover-not-playing-nice/ Thu, 09 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-09-chrome-android-and-background-size-cover-not-playing-nice/ Quick note that I’ve discovered when working with full-screen background images not sizing up itself properly in portrait mode on Android’s Chrome. html { background: asset-url('supercoolbackground.jpg', image) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } Normally, that’s all we need to do. Works perfectly on most modern browsers on the desktop. But when we get to the mobile web, it ends up looking more like this. Fix VB Guest Addition update error in Vagrant https://dchua.com/posts/2014-01-03-fix-vb-guest-addition-update-error-in-vagrant/ Fri, 03 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-03-fix-vb-guest-addition-update-error-in-vagrant/ Ever get problems with your Vagrant informing you that your Virtualbox Guest Additions is of a different version than your Virtualbox version? [default] The guest additions on this VM do not match the install version of VirtualBox! This may cause things such as forwarded ports, shared folders, and more to not work properly. If any of those things fail on this machine, please update the guest additions and repackage the box. Getting Singapore Mobile Broadband to give you an public ip address https://dchua.com/posts/2014-01-02-getting-singapore-mobile-broadband-to-give-you-an-public-ip-address/ Thu, 02 Jan 2014 00:00:00 +0000 https://dchua.com/posts/2014-01-02-getting-singapore-mobile-broadband-to-give-you-an-public-ip-address/ If your computers are behind a 3G/4G mobile broadband router, you may be finding yourself without a publicly accessible external IP address to call back to. By default, most broadband plans would put you behind a private network which means you cannot access your devices w/o being in the same network. To solve this and get your provider to provide you with a public ip address, in your 3G/4G routers, you should have an option to manually specify an APN. Monkey HTTP for Raspbian https://dchua.com/posts/2013-12-30-monkey-http-for-raspbian/ Mon, 30 Dec 2013 00:00:00 +0000 https://dchua.com/posts/2013-12-30-monkey-http-for-raspbian/ Quick tip: If you’re planning to run Monkey httpd on Raspbian, don’t use the official Raspbian repository to pull the package. Why Monkey? Apparently its 16% faster than Nginx. Follow the instructions here as the official repository packages are really really old. I originally had problems with Symlinks not working even though someone on the bug tracker said it does work. Turns out, my monkey version is 0.9.3, while the current stable (at this point of writing) is 1. Wifi Configuration - Ncurses style https://dchua.com/posts/2013-12-30-wifi-configuration-ncurses-style/ Mon, 30 Dec 2013 00:00:00 +0000 https://dchua.com/posts/2013-12-30-wifi-configuration-ncurses-style/ So I’ve been having this irritating problem of configuring wifi on my small mini-computers in command-line. Getting a little bit frustrated with the tedious work of configuring the wpa-supplement and /etc/network/interfaces just to get my system configured with my home network, I found this nifty little tool called wicd. WICD is a network manager that comes in many flavors mostly in GUI ontop of a X Window. Running a headless Raspberry Pi, I don’t want to boot up and X11 forward a display just to configure it. Exclude individual css from assets pipeline https://dchua.com/posts/2013-12-22-exclude-individual-css-from-assets-pipeline/ Sun, 22 Dec 2013 00:00:00 +0000 https://dchua.com/posts/2013-12-22-exclude-individual-css-from-assets-pipeline/ When in the need to remove certain CSS files from your application.css’s require_tree: # application.css /* *= require_self *= require_tree . */ Use sprocket’s stub directive: # application.css /* *= require_self *= require_tree . *= stub whatevercssyoudontwanttoload[.css] */ Note: Best practice of course is to add individual stylesheets into your application.css rather than a full on require_tree .. But in the event that you require to still use require_tree, the above may help you. Mongodb caveats; you need swap space https://dchua.com/posts/2013-12-10-mongodb-caveats-you-need-swap-space/ Tue, 10 Dec 2013 00:00:00 +0000 https://dchua.com/posts/2013-12-10-mongodb-caveats-you-need-swap-space/ So I was helping out a friend figure out why his server keeps crashing and timing out. Having moved his mongodb query code out into its own controller and setup an onload AJAX call, I subsequently started to look deep into the mongodb logs. I noticed this appearing: Tue Dec 10 15:20:14.043 [conn3] getmore bleah.data query: { $where: " Doing a google, I came across this article. Apparently, mongodb dies when it runs out of memory. Vagrant for Ruby and ROR developers https://dchua.com/posts/2013-11-18-vagrant-for-ruby-and-ror-developers/ Mon, 18 Nov 2013 00:00:00 +0000 https://dchua.com/posts/2013-11-18-vagrant-for-ruby-and-ror-developers/ If you’re using Vagrant, this is the Vagrant Box I’ve been using that has been really awesome. RWprecise64 Discover Wireless Devices with Avahi https://dchua.com/posts/2013-11-16-discover-wireless-devices-with-avahi/ Sat, 16 Nov 2013 00:00:00 +0000 https://dchua.com/posts/2013-11-16-discover-wireless-devices-with-avahi/ Quick protip: If you’re having trouble finding the ip addresses of your devices that is connected to your local area network. Use Avahi $ avahi-browse -alr This returns you a list of all the devices connected to your network with the corresponding ip address. September Book Read https://dchua.com/posts/2013-09-25-sept-book-read/ Wed, 25 Sep 2013 00:00:00 +0000 https://dchua.com/posts/2013-09-25-sept-book-read/ Borrowed a couple of books the other day at the library after work. Been so long since I’ve been to the library. Miss the days just hanging around, reading, getting inspired with new ideas and learning from the giants. Here’s hoping I can complete all 4 books before the due date. Big Data - A Revolution That Will Transform How We Live, Work and Think The Essentials of Financial Analysis - A Manager’s Guide To How to read your installed gem's rdoc https://dchua.com/posts/2013-09-12-how-to-read-your-installed-gem-s-rdoc/ Thu, 12 Sep 2013 00:00:00 +0000 https://dchua.com/posts/2013-09-12-how-to-read-your-installed-gem-s-rdoc/ One of the problems I usually have is that rubygem’s rdoc generator is so slow. Everytime I want to take a look at the rdoc of a gem via rubygems, it would take ages to load up that I would give up halfway. But did you know you already have the rdocs installed on your local machine and that there is an easy way to view them locally? You know how usually when you install a gem, and you get a *Installing rdoc …… prompt? The Mystery Behind Ruby Require and Load Paths https://dchua.com/posts/2013-09-12-the-mystery-behind-ruby-require-and-load-paths/ Thu, 12 Sep 2013 00:00:00 +0000 https://dchua.com/posts/2013-09-12-the-mystery-behind-ruby-require-and-load-paths/ Ruby require used to annoy the hell out of me. Because back then I didn’t know how to use it, or what exactly it does. I knew that I need to ‘require’ in order to use custom Classes I’ve created. But what to put in the ‘require …’, has always been trial and error for me. Basically, ‘require’ takes in a param which contains the path to your custom class(es) that is stored in a . Properly using SSH Agent Forwarding in Capistrano https://dchua.com/posts/2013-08-29-properly-using-ssh-agent-forwarding-in-capistrano/ Thu, 29 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-29-properly-using-ssh-agent-forwarding-in-capistrano/ You’ve probably seen this in your capistrano deploy script. # /config/deploy.rb set :ssh_options, { :forward_agent => true } SSH Agent Forwarding is a great way to keep SSH keys manageable as it allows the deployment server to use your own local private key to authenticate to the git repository, instead of having to give your deployment server access to your git repository. Github has an awesome article explaining this. My tl;dr version: Some Technical Management Readings https://dchua.com/posts/2013-08-28-some-technical-management-readings/ Wed, 28 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-28-some-technical-management-readings/ Been doing quite a bit of spring cleaning on my MBP lately and found a couple of articles hidden away in the corner in one of my bookmark folders that I thought I should share. Been reading them over the week and found it helpful in gaining some perspective in the technical management shape of things. The product design sprint: a five-day recipe for startups Show How, Don't Tell What - A Management Style Your team should work like an open source project Ruby Rescuing Precedence https://dchua.com/posts/2013-08-25-ruby-rescuing-precedence/ Sun, 25 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-25-ruby-rescuing-precedence/ When doing a rescue_from in your controller, note that: # controller rescue_from Exception, :with => :do_this rescue_from SomeOtherException, :with => :do_that def ... Exceptions always run in precedence, with the later rescue taking in a higher priority. Therefore, if you ever want SomeOtherException to be rescued, it has to be later than the catch-all Exception. Copying in Tmux into your OSX Clipboard https://dchua.com/posts/2013-08-22-copying-in-tmux-into-your-osx-clipboard/ Thu, 22 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-22-copying-in-tmux-into-your-osx-clipboard/ One of the common gripes I have with using Tmux (even though its super awesome) is that its so difficult to copy text out of your tmux session into your system clipboard. Recently I’ve came across a helpful snippet which would make it so much easier to do that in OSX. This doesn’t work on Linux unfortunately, but we’ll get to that in a bit! Pre-requisite: Have homebrew. In OSX: Tail Rails Production Logs from Capistrano https://dchua.com/posts/2013-08-21-tail-rails-production-logs-from-capistrano/ Wed, 21 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-21-tail-rails-production-logs-from-capistrano/ Found this nifty snippet today which would allow you to tail your production logs without leaving your local terminal. # capistrano desc "tail production log files" task :tail_logs, :roles => :app do trap("INT") { puts 'Interupted'; exit 0; } run "tail -f #{shared_path}/log/production.log" do |channel, stream, data| puts # for an extra line break before the host name puts "#{channel[:host]}: #{data}" break if stream == :err end end I put mine in the ‘rails’ namespace, so all I have to do is a nice: Fix OpenSSL Read Server Certificate B: Certificate Verify Failed on OSX with RVM https://dchua.com/posts/2013-08-20-fix-openssl-read-server-certificate-b-certificate-verify-failed-on-osx-with-rvm/ Tue, 20 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-20-fix-openssl-read-server-certificate-b-certificate-verify-failed-on-osx-with-rvm/ openssl::ssl::sslerror: ssl_connect returned=1 errno=0 state=sslv3 read server certificate b: certificate verify failed Getting the above error when running some gems? Your SSL certificates may be out of date. According to this, there’s actually a way to fix this without much work. Pre-requisite: You should be using RVM. Verify that your certs are out of date: $ rvm osx-ssl-certs status all Certificates for /usr/local/etc/openssl/cert.pem: Old. Certificates for /etc/openssl/cert.pem: Old. Testing external libraries in RSpec https://dchua.com/posts/2013-08-20-testing-external-libraries-in-rspec/ Tue, 20 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-20-testing-external-libraries-in-rspec/ Note to self: Creating subdirectories under the spec/ directories in Rails, is alright. But ensure that all test files have the _spec suffix in their filename else RSpec will ignore the file when you’re doing an: webapp/$ rspec . DRY up your Devise Logins in RSpec https://dchua.com/posts/2013-08-19-dry-up-your-devise-logins-in-rspec/ Mon, 19 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-19-dry-up-your-devise-logins-in-rspec/ Tired of instantiating a def setup that logins a user for ever rspec controller that will require devise logins? In order to DRY your login mechanisms in Devise: Create a external library that contains your various login strategies. I chose to put mine in /spec/support/controller_macros.rb. You can name the file anything you want. Make sure you modify the module name as well if you do so. module ControllerMacros def login_admin @request. Migrate all DNS Zones from Zerigo over to AWS Route 53 https://dchua.com/posts/2013-08-18-migrate-all-dns-zones-from-zerigo-over-to-aws-route-53/ Sun, 18 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-18-migrate-all-dns-zones-from-zerigo-over-to-aws-route-53/ So yesterday there was another major downtime on Zerigo and its about the last straw I had with the service. Not only the service was down for most of the day, causing some of my domains to fail updating, not a single e-mail or communication was made on the part of the management team to inform us about what has happened. If I didn’t check zerigostatus.com, I wouldn’t have realized that the entire service was down. Capistrano - Setup log directories and database.yml https://dchua.com/posts/2013-08-17-capistrano-setup-log-directories-and-database-yml/ Sat, 17 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-17-capistrano-setup-log-directories-and-database-yml/ Updated my deploy.rb gist. Created a new setup_config task that should be run the first time you setup your server. This fixes the missing database.yml file, and setups the missing log directories in your Rails app. Also creates a helper method that checks if a remote_file exists. # config/deploy.rb before 'deploy:db:symlink_db', 'deploy:setup_config' def remote_file_exists?(full_path) 'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip end namespace :deploy do task :setup_config do run " Postman Chrome Extension - Test your APIs via a simple GUI https://dchua.com/posts/2013-08-11-postman-chrome-extension-test-your-apis-via-a-simple-gui/ Sun, 11 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-11-postman-chrome-extension-test-your-apis-via-a-simple-gui/ If you’re always building APIs and often need to test your outputs, why not use the Chrome extension POSTMAN. Instead of running ‘curl’ and having to remember how to set the params values, Postman allows you to send http requests via all the available methods (GET/POST/PUT/DELETE…) through an easy to use GUI interface. I’ve been using it for quite sometime now and it has been really valuable in my webdev toolkit. Rspec Generator Files https://dchua.com/posts/2013-08-10-rspec-generator-files/ Sat, 10 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-10-rspec-generator-files/ If RSpec for some reason doesn’t generate your spec files when generating models and controllers, add this to your config/application.rb. config.generators do |g| g.test_framework :rspec, :fixtures = true, :view_specs = false, :helper_specs = false, :routing_specs = false, :controller_specs = true, :request_specs = true g.fixture_replacement :factory_girl, :dir = "spec/factories" end Store your SSH private keys in a different file https://dchua.com/posts/2013-08-07-store-your-ssh-private-keys-in-a-different-file/ Wed, 07 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-07-store-your-ssh-private-keys-in-a-different-file/ SSH Keys have been helping developers and system administrators quickly manage their servers without having to remember pesky passwords. I’d like to re-use my SSH key as I have multiple machines that I’d like to easily access my resources with. Normally, I would store my personal priv/pub SSH Keys on Dropbox (with encryption of course) and then copy it over to my new machine. But because I also keep different sets of keys around, I often found myself getting confused if this particular set of id_rsa/id_rsa. SimpleCov for Spork/RSpec (Code Test Coverage) https://dchua.com/posts/2013-08-06-simplecov-for-sporkrspec-code-test-coverage/ Tue, 06 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-06-simplecov-for-sporkrspec-code-test-coverage/ If you’re into testing, you know about test coverages. Some developers aim to strive 100% coverage, but personally, I’d avoid being dogmatic about test coverage. After all test coverage is doesn’t ensure that your code is well tested. But if you’re into test coverage, or just want a quick and easy way to view your team’s test progress, this gem, simplecov, maybe what you’re looking for. SimpleCov generates a HTML report in coverage/ located in your Rails. Git pull request for Git https://dchua.com/posts/2013-08-01-git-pull-request-for-git/ Thu, 01 Aug 2013 00:00:00 +0000 https://dchua.com/posts/2013-08-01-git-pull-request-for-git/ One of the nice features of a forking workflow is that your repository is always clean and you can pick and choose features from other forked developers' repository to merge with yours. When we usually think ‘fork’ and ‘git’, I’m sure to most of us, Github’s forking/pulling workflow comes in mind. But did you know you can actually do a git-pull-request on a non-github repository and also on a centralized feature branch workflow? Resque your RSpec https://dchua.com/posts/2013-07-31-resque-your-rspec/ Wed, 31 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-31-resque-your-rspec/ To start testing your Resque: Use the awesome ResqueSpec # Gemfile group :test do ... gem 'resque_spec' ... end In your spec testing, play around with “have_queue_size_of” and “have_queued”. Example, if you have a worker called SoldPropertyMessenger with its perform action taking in a param of an ‘id’ # app/workers/sold_property_messenger.rb class SoldPropertyMessenger @queue = :notification def self.perform(id) # magic end end Your spec/controller test could do something like: Unstage your last git commit - Temporary commits for workstation switches https://dchua.com/posts/2013-07-31-unstage-your-last-git-commit-temporary-commits-for-workstation-switches/ Wed, 31 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-31-unstage-your-last-git-commit-temporary-commits-for-workstation-switches/ Undo a git commit. Sometimes when I’m switching between workstations while working halfway onto code, I’d need to find a way to transfer WIP code over. There are a couple of ways to do this. Some would create patches, copy it into dropbox, and pull and apply it on the new workstation. For me, I would just create a new branch, commit the changes as “WIP”, push it to the remote repository and pull the whole branch over from the other workstation. Howto: Multiple Redis Database for Different Rails Environment https://dchua.com/posts/2013-07-30-howto-multiple-redis-database-for-environment/ Tue, 30 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-30-howto-multiple-redis-database-for-environment/ This is a personal note to self. Referenced from a post on Stackoverflow. One problem I had with running Redis on my Rails app was that on the default configuration, Redis can’t tell if you’re on development or production and hence when doing tests, you may overwrite your development data. To solve this, lets setup redis to use the correct database for the correct environment: Add config/redis.yml #config/redis.yml default: host: localhost port: 6379 development: db: 0 # namespace: appname_dev test: db: 1 # namespace: appname_test production: db: 2 host: localhost # namespace: appname_prod Update the config/initializer/redis. Redis::InheritedError during RSpec/Spork Testing https://dchua.com/posts/2013-07-30-redisinheritederror-during-rspecspork-testing/ Tue, 30 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-30-redisinheritederror-during-rspecspork-testing/ When testing an action that performs a redis-rb command on RSpec, you may run into an error: Redis::InheritedError (Tried to use a connection from a child process without reconnecting ... Solve it by adding the following into your spec_helper. # spec/spec_helper.rb RSpec.configure do |config| # ... config.before :all do $redis.client.reconnect end # ... end Saves me ton of time. Reference: Stackoverflow Network Connectivity Broke Twilio's Billing System https://dchua.com/posts/2013-07-25-network-connectivity-broke-twilios-billing-system/ Thu, 25 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-25-network-connectivity-broke-twilios-billing-system/ Security and Infrastructure issues often intrique me. This morning, an already old (in internet terms) news story broke on HN, pointing to a discussion on antirez about the recent billing incident where Twilio’s customers were billed multiple times and subsequently, some of whom had their accounts suspended. I’m not going to repeat the brief of the story because you probably would already have clicked on the above two links and read anyway but two points to take away from this: How to Redis from Source to Daemon https://dchua.com/posts/2013-07-23-how-to-redis-from-source-to-daemon/ Tue, 23 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-23-how-to-redis-from-source-to-daemon/ Make sure you have a server. I’d recommend spinning up an Amazon EC2 instance. Grab the latest redis. At this point of writing, it is: $ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz $ tar xzf redis-2.6.14.tar.gz $ cd redis-2.6.14 You can do this in any directory, I recommend making a tmp/ directory in your home directory. Run a make and make test! $ make && make test Copy the redis/* content into a accessible folder. Whenever - Easy Cronnable Rake Tasks! With Capistrano! https://dchua.com/posts/2013-07-17-whenever-easy-cronnable-rake-tasks-with-capistrano/ Wed, 17 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-17-whenever-easy-cronnable-rake-tasks-with-capistrano/ We all have this problem before. Attempting to write a crontab that runs a rake task is almost a painful endeavor. Especially if you’re running a ruby version manager like RVM. Luckily, this crontab DSL gem, “Whenever”, created by Javan, helps make things so much easier. To get started, make sure you have whenever in your Gemfile: #Gemfile gem 'whenever' Then, run wheneverize: /home/myapp$ wheneverize . This will create your config/scheduler. Taps. An easy way to quickly migrate data to remote databases https://dchua.com/posts/2013-07-16-taps-an-easy-way-to-quickly-migrate-data-to-remote-databases/ Tue, 16 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-16-taps-an-easy-way-to-quickly-migrate-data-to-remote-databases/ If you’ve been using Heroku, you probably have already seen taps at work. $ heroku db:pull $ heroku db:push Looks familar? Well that’s basically runs on taps. An open-source implementation for database migration that Heroku released to the open-source community. 8 May 2014 Update: Theres an updated gem called taps-taps which fixes a couple of bugs migrating from MySQL to SQLite3 after the original gem was abandoned by the developers. Loading 'deploy/assets' in Capistrano, automatically precompiles:assets https://dchua.com/posts/2013-07-14-loading-deployassets-in-capistrano-automatically-precompilesassets/ Sun, 14 Jul 2013 00:00:00 +0000 https://dchua.com/posts/2013-07-14-loading-deployassets-in-capistrano-automatically-precompilesassets/ Something that I’ve just figured out after tinkering for a couple of hours. Hope this will save someone else some trouble. So I’m running my own custom Capistrano script that on fresh deploy setup a symlink of database.yml on the app instance, so that you don’t need to check in your database.yml file. So in my config/deploy.rb, I have something like: after 'deploy:update_code', 'deploy:symlink_db', 'deploy:assets:precompile' . . . . desc " Git commit individual line changes / View staged changes' diff https://dchua.com/posts/2013-06-30-git-commit-individual-line-changes-view-staged-changes-diff/ Sun, 30 Jun 2013 00:00:00 +0000 https://dchua.com/posts/2013-06-30-git-commit-individual-line-changes-view-staged-changes-diff/ When working on files where you want to commit specific line changes into separate commits, there is a way to individually select lines of the changes that you want to commit. Use: git add -p Git diff on its own lets you view changes between your HEAD and the current unstaged state. To view changes of staged changes (but not committed), use: git diff --cached In order to save time, in my personal ~/. Testing PaypalExpress Gateway on ActiveMerchant https://dchua.com/posts/2013-06-24-testing-paypalexpress-gateway-on-activemerchant/ Mon, 24 Jun 2013 00:00:00 +0000 https://dchua.com/posts/2013-06-24-testing-paypalexpress-gateway-on-activemerchant/ Background: ActiveMerchant’s BogusGateway class which is usually used in a Test environment, does not have setup_purchase/setup_authorization method which is used in Paypal’s Express Checkout. This gives an: undefined method `setup_purchase' for # When testing for controller actions that interact with ActiveMerchant’s Paypal Express Gateway. To fix this, according to Skud, you will need to download and unpack the active_merchant and active_utils gems into your local vendor directory. /webapps/myapp/$ gem unpack /path/to/activemerchant-1. Getting OSX to load .bashrc on Terminal start https://dchua.com/posts/2013-06-21-getting-osx-to-load-bashrc-on-terminal-start/ Fri, 21 Jun 2013 00:00:00 +0000 https://dchua.com/posts/2013-06-21-getting-osx-to-load-bashrc-on-terminal-start/ OSX doesn’t load bashrc when you open up Terminal. Add the following to your ~/.bash_profile to get your bashrc scripts working. [[ -s ~/.bashrc ]] && source ~/.bashrc Quick Spork/RSpec Setup https://dchua.com/posts/2013-06-12-quick-sporkrspec-setup/ Wed, 12 Jun 2013 00:00:00 +0000 https://dchua.com/posts/2013-06-12-quick-sporkrspec-setup/ Make sure RSpec is already running. Add this in your Gemfile # Gemfile gem 'spork' In your project application root, create a .rspec file if its not already created and add –drb in the first line. # .rspec --drb Add Spork.prefork block into spec_helper. # spec/spec_helper.rb require 'spork' Spork.prefork do ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' end Reload Spork Deploying Node.JS on Nginx with Capistrano/Capper https://dchua.com/posts/2013-05-28-deploying-node-js-on-nginx-with-capistranocapper/ Tue, 28 May 2013 00:00:00 +0000 https://dchua.com/posts/2013-05-28-deploying-node-js-on-nginx-with-capistranocapper/ The other day on a single-day internal hackathon, together with Ernest and the rest of the One Cent Movement guys, we decided to do up an internal check-in mobile webapp where we can see where each of us were at anytime of the day. This is a simple guide to deploy your node.js app onto a production server. Which in my case is an Amazon EC2 m1.small instance. Pre-requsite: NPM is assumed to be the defacto package manager. RVM MRI 2.0.0-rc1 installs giving you trouble? https://dchua.com/posts/2013-05-08-rvm-mri-2-0-0-rc1-installs-giving-you-trouble/ Wed, 08 May 2013 00:00:00 +0000 https://dchua.com/posts/2013-05-08-rvm-mri-2-0-0-rc1-installs-giving-you-trouble/ RVM Ruby 2.0.0-rc1 installs giving you problem? Getting this Error? Error running 'make', please read /Users/katz/.rvm/log/ruby-2.0.0-rc1/make.log OSX brew install openssl rvm get head CC=clang rvm install 2.0.0 -C --enable-shared, --with-openssl-dir=/usr/local Ubuntu sudo apt-get install openssl rvm get head rvm install 2.0.0 -C --enable-shared, --with-openssl-dir=/usr/local Source: http://www.blog.bridgeutopiaweb.com/post/install-ruby-2-dot-0-0-with-rvm Ubuntu 12.10 Nginx/Passenger/Rails https://dchua.com/posts/2013-03-23-ubuntu-12-10-nginxpassengerrails/ Sat, 23 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-23-ubuntu-12-10-nginxpassengerrails/ Created a new AMI image. Click away. It runs: RVM Ruby 1.9.2 Nginx (/opt/nginx) Passenger Its comes with an 8GB Elastic Block Storage (EBS) instance. So if you’re planning to run a ruby/rails webserver, I hope this saves you time! Note Please ensure your Kernel ID is set to aki-fe1354ac Non-Rails Capistrano Deploy Recipe https://dchua.com/posts/2013-03-21-non-rails-capistrano-deploy-recipe/ Thu, 21 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-21-non-rails-capistrano-deploy-recipe/ If you’re deploying non-Rails apps/websites, this snippet may be useful to you as it was to me. I can’t remember when was the last time I actually used FTP to deploy websites. Then again, I don’t do much non-Rails apps nowadays… set :application, "" set :repository, "" set :deploy_to, "/path/to/name" set :user, 'deployer' set :use_sudo, false set :rvm_type, :system set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none` role :web, " Google Reader is Dead. Why Not Just Drink To It https://dchua.com/posts/2013-03-19-google-reader-is-dead-why-not-just-drink-to-it/ Tue, 19 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-19-google-reader-is-dead-why-not-just-drink-to-it/ Google Reader is dead. As an avid Reader user who has at last count, 222 feeds pumping in news all day all the time, the news is nothing less than devastating. As we mourn the loss of an essential tool that feeds us our information diet, we at the office thought that, instead of developing an in-house RSS reader like the rest of the world, why not just drink to it and be done with it. LTA - first government agency to engage developers? https://dchua.com/posts/2013-03-18-lta-first-government-agency-to-engage-developers/ Mon, 18 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-18-lta-first-government-agency-to-engage-developers/ LTA seems to be sincere in trying to engage the development community to find out how best they can help to empower developers to build more mobile apps around their dataset. Last week I was asked to come down for a meeting with LTA apparently to discuss about my old android app, Traffic@SG. Today, I met two of the engineers and we had a short chat about how the app work and what problems I’ve encountered. Using s3cmd with Non-US S3 Buckets https://dchua.com/posts/2013-03-15-using-s3cmd-with-non-us-s3-buckets/ Fri, 15 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-15-using-s3cmd-with-non-us-s3-buckets/ Note to self: After installing s3cmd, and configuring it, if you’re getting “[Errno 32] Broken Pipe” errors when putting a file into your non-US S3 buckets, modify your ~/.s3cfg: # in ~/.s3cfg # modify host_base and host_bucket to the proper endpoints host_base = s3-ap-southeast-1.amazonaws.com host_bucket = %(bucket)s.s3-ap-southeast-1.amazonaws.com JFDIN - We Made it onto HN Frontpage #1 https://dchua.com/posts/2013-03-14-jfdin-we-made-it-onto-hn-frontpage-1/ Thu, 14 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-14-jfdin-we-made-it-onto-hn-frontpage-1/ So yesterday, Sung and I decided to ‘launch’ our one-day hackathon Pomodoro Timer project onto HN. To be honest, I was pretty skeptical that we’d even more than 10 points, and perhaps 1 or 2 comments. To my surprise, not only we managed to go past the 50 points mark, we also hit the frontpage and was hovering around #1 for a really short period of time. What is the project about? Custom Exception Handling https://dchua.com/posts/2013-03-10-custom-exception-handling/ Sun, 10 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-10-custom-exception-handling/ Note to self: For some better practice, handle custom exceptions by: Declaring a custom exception module. # In /lib/exceptions/app_exception.rb module AppException class AnyExceptionName < StandardError end end Do try and keep to the relevant exception subclasses (ie. StandardError v Error). Refer to this. # In /config/application.rb # ensure that autoload path is un-commented to allow rails to load your custom exception library config.autoload_paths += %W(#{config.root}/lib/exceptions) Now, you can raise AppException::AnyExceptionName anytime you want! Speed up assets:precompile in Capistrano https://dchua.com/posts/2013-03-02-assets-precompile-passenger/ Sat, 02 Mar 2013 00:00:00 +0000 https://dchua.com/posts/2013-03-02-assets-precompile-passenger/ Note to self. In order to speed up assets:precompile on passenger production, add the following to config/deploy.rb: namespace :deploy do namespace :assets do task :precompile, :roles => :web, :except => { :no_release => true } do from = source.next_revision(current_revision) if capture("cd #{latest_release}&& #{source.local.log(from)}vendor/assets/ app/assets/ | wc -l").to_i > 0 run %Q{cd #{latest_release}&& #{rake}RAILS_ENV=#{rails_env}#{asset_env}assets:precompile} else logger.info "Skipping asset pre-compilation because there were no asset changes" end end end end Alt: set :max_asset_age, 2 ## Set asset age in minutes to test modified date against. JQuery address picker rails gem breaks with JQuery 1.9.x https://dchua.com/posts/2013-02-12-jquery-address-picker-rails-gem-breaks-with-jquery-1-9-x/ Tue, 12 Feb 2013 00:00:00 +0000 https://dchua.com/posts/2013-02-12-jquery-address-picker-rails-gem-breaks-with-jquery-1-9-x/ Note to self: If you're going to use address_picker_rails (0.2.1), make sure your jquery-rails gem is set to Apparently Jquery-UI 1.9.1 breaks address_picker_rails and jquery-rails 2.2.0, uses the 1.9.x branch. I hope this will save you some time debugging address_picker_rails if you’ve accidentally updated your jquery-rails gem to the latest version, only to find out your address autocomplete function no longer allowing you to select the results. Many thanks to this StackOverflow question which led me to suspect that it wasn’t a address_picker_rails bug but rather a version compatibility issue with jquery-ui. Pusher + Slanger - Publish away! https://dchua.com/posts/2013-01-29-pusher-slanger-publish-away/ Tue, 29 Jan 2013 00:00:00 +0000 https://dchua.com/posts/2013-01-29-pusher-slanger-publish-away/ Played around with Pusher today. Surprisingly, it was an easy push. If you haven’t heard about Pusher, it is a hosted API that allows you to quickly build publish-subscribe messaging functionality into your web application. Like most hosted APIs, they work on a freemium model and charge only when you use up to 100 concurrent connections at a time. Pusher provides a pusher-gem which lets your Rails controllers send out publish messages. LeanUX Week 2013 Day 3 https://dchua.com/posts/2013-01-27-leanux-week-2013-day-3/ Sun, 27 Jan 2013 00:00:00 +0000 https://dchua.com/posts/2013-01-27-leanux-week-2013-day-3/ Attended Lean UX Week 2013 Day 3 held at Microsoft’s offices today. The following are just my notes for personal reference and are written as it is. Quotes: Talking about your startup/pitch to others, is not customer development. It doesn't validate anything. "Relationships are important than being right" "Lean startup is not cheap startup" "If u can develop a metric, and if u can meet that metric, then you have developed an important muscle in your company. Paypal Express and Authorization Holds https://dchua.com/posts/2012-12-23-paypal-express-and-authorization-holds/ Sun, 23 Dec 2012 00:00:00 +0000 https://dchua.com/posts/2012-12-23-paypal-express-and-authorization-holds/ Source: ActiveMerchant and Recurring Payments Paypal Express Checkout Payment Actions Paypal Express Integration Guide Paypal Website Pro Integration Guide ActiveMerchant rdoc Bypassing mass-assignment protection in rake db:seed https://dchua.com/posts/2012-12-19-bypassing-mass-assignment-protection-in-rake-dbseed/ Wed, 19 Dec 2012 00:00:00 +0000 https://dchua.com/posts/2012-12-19-bypassing-mass-assignment-protection-in-rake-dbseed/ Reference: GilesBowkett So I ran this problem the other day on a project where the db/seed.rb file couldn’t run due to mass-assignment protection. Instead of adding the attribute in attr_accessible on the model side and then removing it after running the seed data, there’s actually another way to do it. Giles suggested adding attr_accessible directly into the seed file itself, such that it will temporarily disable mass-assignment protection for the duration of the rake db:seed. The Splat(*) Operator https://dchua.com/posts/2012-05-18-the-splat-operator/ Fri, 18 May 2012 00:00:00 +0000 https://dchua.com/posts/2012-05-18-the-splat-operator/ Learnt something new today. When trying to split a multi-dimensional array, one could use the *(splat) operator. a = [[1,2],[3,4]] b = [[5,6],[7,8]] In order to include b into a without breaking the array, you could do the following: a = [[1,2],[3,4], *b] # a = [[1,2],[3,4],[5,6],[7,8]] And that’s the magic of *splat. Update: Amazon S3, Paperclip and a Curious Case of Singapore Buckets https://dchua.com/posts/2012-04-25-update-amazon-s3-paperclip-and-a-curious-case-of-singapore-buckets/ Wed, 25 Apr 2012 00:00:00 +0000 https://dchua.com/posts/2012-04-25-update-amazon-s3-paperclip-and-a-curious-case-of-singapore-buckets/ This is an update of my previous blog post, “How to get Paperclip and AWS S3 Singapore and European Buckets Working”. Since the post last year in December, Amazon had released its official AWS SDK for ruby which is now available as the ‘aws-sdk’ rubygem. Paperclip had also made an update in its core to directly support AWS-SDK over marcel’s AWS-S3 gem. This little guide is supposed to help you get quickly started uploading your images into S3 via Paperclip and the new gem. The Perils of Freelancing https://dchua.com/posts/2012-04-20-the-perils-of-freelancing/ Fri, 20 Apr 2012 00:00:00 +0000 https://dchua.com/posts/2012-04-20-the-perils-of-freelancing/ Being a freelancer for the past 8 months had been one of the most exciting and simultaneously scary thing that have happened to me in a long time. Even though I have been running my own small businesses since 2003, they have always been done under the safety and comfort of being a student and having some form of backup safety net that is my parents. I always knew that if something went wrong with the business, I’d still have enough money to eat and could always start again in the future. The New Internet Bubble and the Rise of the Countless Entrepreneurs https://dchua.com/posts/2012-04-11-the-new-internet-bubble-and-the-rise-of-the-countless-entrepreneurs/ Wed, 11 Apr 2012 00:00:00 +0000 https://dchua.com/posts/2012-04-11-the-new-internet-bubble-and-the-rise-of-the-countless-entrepreneurs/ The recent announcement that Facebook is acquiring Instagram for US$1 Bn have made shockwaves all across the globe. This comes hot after the heels of the well publicized IPO of Groupon and the pending Facebook IPO. Now, to me, Instagram is no where worth the $1bn that Facebook is willing to pay for it. For one, its only a small team of less than 10 men. Secondly, Instagram has no business revenue. Don't estimate software https://dchua.com/posts/2012-04-04-dont-estimate-software/ Wed, 04 Apr 2012 00:00:00 +0000 https://dchua.com/posts/2012-04-04-dont-estimate-software/ Just read a great post by Dan Shipper which was posted on Hacker News. In his article, Dan writes about how as humans we tend to be unable to estimate complexity and hence we make really bad estimation of how long it will take to do something. Now let’s talk about software. When a non-technical person attempts to estimate software development time they come armed with their two basic heuristics: complexity based on size and complexity based on speed. A traffic light to keep stock https://dchua.com/posts/2012-03-15-a-traffic-light-to-keep-stock/ Thu, 15 Mar 2012 00:00:00 +0000 https://dchua.com/posts/2012-03-15-a-traffic-light-to-keep-stock/ Just got home from a thereputic walk around the private estate near my block. Ashamely, its been quite sometime since I’ve done something like this, but I’m glad that I did it. Nothing helps to clear the mind like some jazz and soul music (from Lush 99.5fm) while taking a walk in a dead quiet neighbourhood on a chilly evening. The past few months have been quite a challenge; Having to get more involved politically as well as juggling various client projects on my freelance side of things. Big Ideas are Scary https://dchua.com/posts/2012-03-13-big-ideas-are-scary/ Tue, 13 Mar 2012 00:00:00 +0000 https://dchua.com/posts/2012-03-13-big-ideas-are-scary/ Big ideas are scary. That's the gist of what I've got from Paul Graham's keynote speech at PyCon US 2012 and "Frighteningly Ambitious Startup Ideas" Don't be afraid of big ideas, instead attack it from different angles in smaller bites. Start small and eventually you'll get there. Oh and check out the rest of the videos from PyCon US 2012 Ubuntu Oneiric 64bit RVM/Ruby1.9x/Passenger/Nginx EC2 AMI https://dchua.com/posts/2012-02-28-ubuntu-oneiric-64bit-rvmruby1-9xpassengernginx-ec2-ami/ Tue, 28 Feb 2012 00:00:00 +0000 https://dchua.com/posts/2012-02-28-ubuntu-oneiric-64bit-rvmruby1-9xpassengernginx-ec2-ami/ Create a new 64bit Ubuntu Oneiric AMI image this morning Click away It runs: RVM Ruby 1.8.7 Ruby 1.9.3-head (default) Nginx (/opt/nginx) Passenger Its comes with an 8GB Elastic Block Storage (EBS) instance. So if you’re planning to run a ruby/rails webserver, I hope this saves you time! :) Create Sequence Diagrams with just text https://dchua.com/posts/2012-01-31-create-sequence-diagrams-with-just-text/ Tue, 31 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-31-create-sequence-diagrams-with-just-text/ http://www.websequencediagrams.com/ Alice-Bob: Authentication Request note right of Bob: Bob thinks about it Bob-Alice: Authentication Response Alice-Bob: Do something Alice-John: Yo note left of John: Hey alt text1 A-B: text else text2 A-B: text end A--B: text A-A: text Just came across this little gem while trying to look for a cloud-based tool to create UML diagrams. Pretty darn cool! You can create a Sequence diagram just by typing the relationships between the entities and it gets rendered automatically. AWS to include Windows instances into its Free Usage Tier https://dchua.com/posts/2012-01-16-aws-to-include-windows-instances-into-its-free-usage-tier/ Mon, 16 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-16-aws-to-include-windows-instances-into-its-free-usage-tier/ This is on-top of the existing 750 micro-instance hours a month for linux instances! Which means you can now run 2 micro-instances of Windows and Linux each under the Free Usage Tier! From AWS Blog The AWS Free Usage Tier now allows you to run Microsoft Windows Server 2008 R2 on an EC2 t1.micro instance for up to 750 hours per month. This benefit is open to new AWS customers and to those who are already participating in the Free Usage Tier, and is available in all AWS Regions with the exception of GovCloud. Handling foreign currency exchanges for Freelancers https://dchua.com/posts/2012-01-16-handling-foreign-currency-exchanges-for-freelancers/ Mon, 16 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-16-handling-foreign-currency-exchanges-for-freelancers/ As freelancers here in Singapore, we often have to purchase goods/services online which are often not priced in Singapore Dollars. So how then do we account for it in our yearly tax declaration to IRAS? According to the folks: These amounts [price] in Singapore dollars may be shown separately beside their respective amount in foreign currency on your tax invoice. In your GST return, you should report the amounts in Singapore dollars shown in your tax invoice for “value of standard-rated supplies” and “output tax due”. LEMP Ubuntu Oneiric x64 EC2 AMI Image https://dchua.com/posts/2012-01-13-lemp-ubuntu-oneiric-x64-ec2-ami-image/ Fri, 13 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-13-lemp-ubuntu-oneiric-x64-ec2-ami-image/ If you’re looking for a quick way to start hosting your PHP5 applications, here’s an EC2 AMI image I’ve created to help make it easier. It is a x64 Ubuntu Oneiric EBS AMI Image running a LEMP (Linux, Nginx, MySQL, PHP) stack which is suitable for those who wants to make use of Amazon’s free-tier micro instances. Click away Its currently only available for the AP-Southeast region (Singapore). Tip: Remember to create a swap partition. Using Amazon S3 as your private git repository https://dchua.com/posts/2012-01-12-using-amazon-s3-as-your-private-git-repository/ Thu, 12 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-12-using-amazon-s3-as-your-private-git-repository/ Been trying to move my git repositories and apps from my old (and expensive) VPS into Amazon’s EC2 when I asked myself whether it is possible to push use Amazon S3 as a git repository. Some searching later, I found out the answer. YES! It is not only possible to push your repositories into your S3 bucket, but its also possible to do so even when your bucket is in a non-US region. Note to self: Swap and EC2 https://dchua.com/posts/2012-01-11-note-to-self-swap-and-ec2/ Wed, 11 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-11-note-to-self-swap-and-ec2/ Note to self: Create a swap partition after launching a micro-instance. sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 sudo /sbin/mkswap /var/swap.1 sudo /sbin/swapon /var/swap.1 You’ll need it. Some tips to deploying Rubber-powered EC2 Rails Apps https://dchua.com/posts/2012-01-07-some-tips-to-deploying-rubber-powered-ec2-rails-apps/ Sat, 07 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-07-some-tips-to-deploying-rubber-powered-ec2-rails-apps/ I’ve been playing around with Rubber, the EC2 deployment script for Rails lately and I’ve spent a huge amount of time trying to get it to work. Here are some of my notes: To setup: # Put this into Gemfile gem rubber Vulcanize! (Create the deployment scripts according to your specifications) On Rails 3.x, in your Rails application’s root directory davidc@davidc:~/my/awesome/app> vulcanize <option> # with option being any one of the following minimal_nodb, redis, passenger_nginx, complete_passenger_nginx, cruise, postgresql, resque, apache, sphinx, monit, minimal_passenger_nginx, mysql_cluster, complete_passenger_postgresql, cassandra, mongodb, complete_mongrel_mysql, complete_passenger_nginx_postgresql, base, memcached, minimal_mysql, complete_passenger_mysql, complete_passenger_nginx_mysql, munin, mysql_proxy, complete_passenger, mysql, nginx, jetty, passenger, haproxy, mongrel Edit /config/rubber/rubber. Video Selection from 28c3 https://dchua.com/posts/2012-01-02-video-selection-from-28c3/ Mon, 02 Jan 2012 00:00:00 +0000 https://dchua.com/posts/2012-01-02-video-selection-from-28c3/ 28c3 (The 28th Chaos Communication Congress) My video picks from 28c3: These are some of my picks which are selected to be as comprehensive and interesting without being overly technical. If you have a slight interest in computing and politics, these should be very interesting to you. The coming war on general computation (must watch) Cory Doctorow: The coming war on general computation The copyright war was just the beginning Go on, watch videos at work. https://dchua.com/posts/2011-12-28-go-on-watch-videos-at-work/ Wed, 28 Dec 2011 00:00:00 +0000 https://dchua.com/posts/2011-12-28-go-on-watch-videos-at-work/ All developers should spend at least an hour a day reading tech blogs, watching tech/conference videos or writing blogs. Even if you’re at work. There’s no substitute for being aware of what’s happening in your community. If you want to be a decent developer you have to know what is out there and what your peers are working on. At the very least you need to have an opinion on technology issues. Free the Taxi industry; Liberalize the market https://dchua.com/posts/2011-12-21-free-the-taxi-industry-liberalize-the-market/ Wed, 21 Dec 2011 00:00:00 +0000 https://dchua.com/posts/2011-12-21-free-the-taxi-industry-liberalize-the-market/ With 4 out of 5 of Taxi Operators here in Singapore, announcing that they will be raising their prices in tandem with ComfortDelgro, perhaps it is time for us to start thinking about how we can liberalize the market again to bring some form of real competition into the picture. Bring back the Yellow-top taxis. Allow individuals to own their own vehicles again. Let them set the price. Let them decide how to conduct their business and then we’ll have a real free market. Rails 3.2 RC1 released! https://dchua.com/posts/2011-12-20-rails-3-2-rc1-released/ Tue, 20 Dec 2011 00:00:00 +0000 https://dchua.com/posts/2011-12-20-rails-3-2-rc1-released/ Woke up this morning to a tweet by DHH: Rails 3.2 (Release Candidate 1) has been released! And it has pretty exciting features to boot like: Faster dev mode & routing Explain queries Tagged logger ActiveRecord Store I’m in particularly excited about tagged logging. For far too long, developers have to struggle with trying to figure out whose Logger.info belonged to who. Can’t wait to try them out. Steve Jobs on the iconic "Think Different" Ad https://dchua.com/posts/2011-12-15-steve-jobs-on-the-iconic-think-different-ad/ Thu, 15 Dec 2011 00:00:00 +0000 https://dchua.com/posts/2011-12-15-steve-jobs-on-the-iconic-think-different-ad/ Rob Siltanen then Creative Director and Partner at advertisement agency TBWA/Chiat/Day: Steve was highly involved with the advertising and every facet of Apple’s business. But he was far from the mastermind behind the renowned launch spot. In fact, he was blatantly harsh on the commercial that would eventually play a pivotal role in helping Apple achieve one of the greatest corporate turnarounds in business history. As you’ll learn later in my account, the soul of the original “The crazy ones” script I presented to Jobs, as well as the original beginning and ending of the celebrated script, all ultimately stayed in place, even though Jobs initially called the script “shit. 1980 Steve Jobs on Apple https://dchua.com/posts/2011-12-14-1980-steve-jobs-on-apple/ Wed, 14 Dec 2011 00:00:00 +0000 https://dchua.com/posts/2011-12-14-1980-steve-jobs-on-apple/ If you’re into computing history, this is quite interesting look into the mind of Jobs at a time when Apple was just getting pretty famous. (Apple II) Notice how the Steve Jobs of 1980 contradicts the Steve Jobs of the iPod-era in philosophy. Rails 3.1x Migration Change https://dchua.com/posts/2011-12-13-rails-3-1x-migration-change/ Tue, 13 Dec 2011 00:00:00 +0000 https://dchua.com/posts/2011-12-13-rails-3-1x-migration-change/ If you’re on Rails 3.1, you’ll probably notice that the methods in your migration files have changed from ‘up’ and its corresponding ‘down’ to just a simple ‘change’. So don’t be the surprised the next time you handle migrations. Rails 3.1 makes migrations smarter by providing a new change method. This method is preferred for writing constructive migrations (adding columns or tables). The migration knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method. Instagram and their scaling strategy https://dchua.com/posts/2011-12-03-instagram-and-their-scalling-strategy/ Sat, 03 Dec 2011 00:00:00 +0000 https://dchua.com/posts/2011-12-03-instagram-and-their-scalling-strategy/ Instagram Engineering Blog Sharding & IDs at Instagram If you’re wondering how they are scaling up so fast… Linus Torvalds: Please write good git commit messages https://dchua.com/posts/2011-11-28-linus-torvalds-please-write-good-git-commit-messages/ Mon, 28 Nov 2011 00:00:00 +0000 https://dchua.com/posts/2011-11-28-linus-torvalds-please-write-good-git-commit-messages/ That’s right. Header line: explaining the commit in one line Body of commit message is a few lines of text, explaining things in more detail, possibly giving some background about the issue being fixed, etc etc. The body of the commit message can be several paragraphs, and please do proper word-wrap and keep columns shorter than about 74 characters or so. That way “git log” will show things nicely even when it’s indented. Colorthis! - Create Colored Boxes via a Web API https://dchua.com/posts/2011-10-11-colorthis-create-colored-boxes-via-a-web-api/ Tue, 11 Oct 2011 00:00:00 +0000 https://dchua.com/posts/2011-10-11-colorthis-create-colored-boxes-via-a-web-api/ Just launched a quick and dirty API for web developers and designers looking to add simple colored boxes on their work. Called, colorthis and deployed on heroku, you can create blank images directly from your webpage just by adding the following example code: If you do find it useful please let me know. I’d love to hear what you think. RIP Steve https://dchua.com/posts/2011-10-06-rip-steve/ Thu, 06 Oct 2011 00:00:00 +0000 https://dchua.com/posts/2011-10-06-rip-steve/ Today is quite a momentous day in the world of Technology. Steve Jobs, co-founder of Apple Computers (now Apple Inc), passed away at the age 56. He was a legend. A misfit. A person who dared to think Different. Together with Steve Wozniak, they gave the modern Personal Computer to the world. They inspired an entire generation and spawned an entirely new industry. Today he will mostly be remembered as the person who brought iPod and iPhone to the world. Sendgrid with Heroku https://dchua.com/posts/2011-09-11-sendgrid-with-heroku/ Sun, 11 Sep 2011 00:00:00 +0000 https://dchua.com/posts/2011-09-11-sendgrid-with-heroku/ Sending out emails from your Rails app never felt so easy. Gave Heroku’s free sendgrid addon a try today since I have an app already hosted on the cloud platform that requires a Mailer functionality. Sendgrid has generously partnered with Heroku to provide its users 200 emails/messages a day and the integration is so seamless that all you need to do is just to add the addon, create your Mailer models and you’re done. Backspacing in vim (OSX) https://dchua.com/posts/2011-09-06-backspacing-in-vim-osx/ Tue, 06 Sep 2011 00:00:00 +0000 https://dchua.com/posts/2011-09-06-backspacing-in-vim-osx/ A quick tip for those using vim on their OSX terminal. If you’re trying to figure out why you can’t backspace and remove characters from vim, you’d need to add the following in your ~/.vimrc set backspace=indent,eol,start That should allow you to now delete previous characters with your ‘backspace’ (delete) key :) References: http://stackoverflow.com/questions/3534028/mac-terminal-vim-will-only-use-backspace-when-at-the-end-of-a-line http://blog.edogg.com/index.php?/archives/19-How-to-fix-the-backspace-keep-in-vim-on-OS-X.html Amazon launches ElastiCache https://dchua.com/posts/2011-08-23-amazon-launches-elasticache/ Tue, 23 Aug 2011 00:00:00 +0000 https://dchua.com/posts/2011-08-23-amazon-launches-elasticache/ Amazon today launches a new service onto its AWS platform. Called, Amazon ElasticCache, a webservice that deploys, scales and operate an in-memory cache in the cloud. Think of it as running Memcached in the cloud. Would RSS eventually be replaced by Facebook Pages? https://dchua.com/posts/2011-06-29-would-rss-eventually-be-replaced-by-facebook-pages/ Wed, 29 Jun 2011 00:00:00 +0000 https://dchua.com/posts/2011-06-29-would-rss-eventually-be-replaced-by-facebook-pages/ Nowadays, most blogs and communities would have their own Facebook Page in which they would push updates to their ‘fans’ just like how RSS would had done to RSS readers of subscribers. The only difference is there’s a larger community of users on Facebook that blogs can reach out to without subscribers having to have the technical know-how to add RSS feeds into their readers. Not to mention the social interactions of the updates would also provide blog owners an additional feedback channel that RSS on its own doesn’t have. Omniauth - Easy Authentication for your Rails Projects https://dchua.com/posts/2011-02-18-omniauth-easy-authentication-for-your-rails-projects/ Fri, 18 Feb 2011 00:00:00 +0000 https://dchua.com/posts/2011-02-18-omniauth-easy-authentication-for-your-rails-projects/ I just came across this wonderful little gem that makes authentication on your rails application a breeze. This is exactly the gem that I wished was around a year ago when I was developing Baboonza.com (now defunct) and my various mini project sites. Usually when I start a new rails project, I would use Authlogic and tweaked it to allow openid authentication. If you’ve used authlogic before, you would know that the entire process of just installing the gem and getting all your user models and controllers setup is god awful tedious. Changing default colorscheme in gvim/mvim https://dchua.com/posts/2011-02-09-changing-default-colorscheme-in-gvimmvim/ Wed, 09 Feb 2011 00:00:00 +0000 https://dchua.com/posts/2011-02-09-changing-default-colorscheme-in-gvimmvim/ A common mistake I’ve often fall into is forgetting how to set a default colorscheme for GUI vim editors. If you’re using gvim, MacVim, to set your default colorscheme, you’ll need to edit (or create if it doesn’t exist) ~/.gvimrc instead of your normal ~/.vimrc. For example in my ~/.gvimrc: colorscheme darkblue Create authlogic apps on Rails 3.x easily https://dchua.com/posts/2011-02-07-create-authlogic-apps-on-rails-3-x-easily/ Mon, 07 Feb 2011 00:00:00 +0000 https://dchua.com/posts/2011-02-07-create-authlogic-apps-on-rails-3-x-easily/ Sometime ago, I’ve created a authlogic rails application template to help me create from scratch a rails 3.x app with authentication. This template would generate the necessary models and migration files to get you running in just a few steps. I’ve created this because of an itch. I hate the process of creating a rails application, installing the authlogic gem, editing the various files. While its relatively simple, it gets annoying when you have to do the same steps many times manually. Why we never seem to finish our great ideas https://dchua.com/posts/2011-02-06-why-we-never-seem-to-finish-our-great-ideas/ Sun, 06 Feb 2011 00:00:00 +0000 https://dchua.com/posts/2011-02-06-why-we-never-seem-to-finish-our-great-ideas/ Some guy on HN asked recently about a problem that most of us have. You know that brilliant web-app that you came up with in the shower? The one that you know is going to change the world when its released? After coming out of the shower, you rush to your room, usually still dripping wet, to take out a piece of paper and scribble the idea down before you forget. Watch Al-Jazeera English live with just one terminal command https://dchua.com/posts/2011-01-30-watch-al-jazeera-english-live-with-just-one-command/ Sun, 30 Jan 2011 00:00:00 +0000 https://dchua.com/posts/2011-01-30-watch-al-jazeera-english-live-with-just-one-command/ I’ve been watching Al-Jazeera for sometime now, mostly via their on-demand Youtube Channel or their stream provided at http://english.aljazeera.net/watch_now. They are clunky and obstructive and as far as I noticed, the flash player affects the stream performance. r11t from HN posted this snipplet that will grab a full-HD stream from AJE and pipe it into MPlayer, avoiding the horrible and slow flash player. rtmpdump -v -r rtmp://livestfslivefs.fplive.net/livestfslive-live/ -y “aljazeera_en_veryhigh?videoId=747084146001&lineUpId=&pubId=665003303001&playerId=751182905001&affiliateId=” -W “http://admin. How to Consume Project Nimbus Dataservices via REST in Java/Android https://dchua.com/posts/2010-12-21-how-to-consume-project-nimbus-dataservices-via-rest-in-javaandroid/ Tue, 21 Dec 2010 00:00:00 +0000 https://dchua.com/posts/2010-12-21-how-to-consume-project-nimbus-dataservices-via-rest-in-javaandroid/ In my previous post, I’ve wrote about consuming Project Nimbus Datasets in Ruby through their REST API interface. Right now, on Project Nimbus’s guides to consuming their datasets in Java/Android currently introduces two method. One is to use the Restlet framework to connect, and another is through a manual webservice method. I particulaly found the two tutorials lacking, especially for a beginning Java guy, or a Rusty java guy. The webservice tutorial have lots of holes in between and might throw off new users, like how it threw me off. Singapore Traffic Android Application (Alpha) Released! https://dchua.com/posts/2010-11-22-singapore-traffic-android-application-alpha-released/ Mon, 22 Nov 2010 00:00:00 +0000 https://dchua.com/posts/2010-11-22-singapore-traffic-android-application-alpha-released/ After hacking around the Android SDK for sometime to work on a project for class, I’ve decided to try and do something relatively quick and easy that made use of the Project Nimbus dataset just to go through the whole development cycle from development to publishing on the Google Android Market. I present to you, Singapore Traffic. A simple one Activity Android application that tells you where are the road hotspots around the island as well as where the road congestions are. How to consume Project Nimbus dataservices in Ruby https://dchua.com/posts/2010-10-13-how-to-consume-project-nimbus-dataservices-in-ruby/ Wed, 13 Oct 2010 00:00:00 +0000 https://dchua.com/posts/2010-10-13-how-to-consume-project-nimbus-dataservices-in-ruby/ Project Nimbus is a community initiative by developers working in key corporations and government agencies in Singapore that aims to open up data and services for local innovators who wants to create applications with localized data. The project is current supported by a mix of government agencies like LTA, NEA as well as private businesses/startups like Chlkboard and Shownearby. The list is no where exhaustive and the dataset curators are constantly updating their site with new data providers, so please refer to the Project Nimbus page for a more up to date list. Search and Replace multiple files https://dchua.com/posts/2010-07-28-search-and-replace-multiple-files/ Wed, 28 Jul 2010 00:00:00 +0000 https://dchua.com/posts/2010-07-28-search-and-replace-multiple-files/ Just a little note to self on replacing text within multiple files. find ./ -type f | xargs sed -i 's/string1/string2/g' This recursively searches for all the files with ‘string1’ and replace it with ‘string2’ from your current path. The joys of engineering leadership https://dchua.com/posts/2010-06-24-the-joys-of-engineering-leadership/ Thu, 24 Jun 2010 00:00:00 +0000 https://dchua.com/posts/2010-06-24-the-joys-of-engineering-leadership/ Its always great to watch both Brian Fitzpatrick and Ben Collins-Sussman speak at Google’s IO conferences. This year at the recent Google IO 2010 conference, they gave a talk on ‘The joys of engineering leadership’ which I’d recommend every technical manager to watch. Their trademark humor and banter makes the video not only entertaining to watch but absolutely insightful too. Well worth the 56 minutes. JQuery wildcard selection https://dchua.com/posts/2010-06-11-jquery-wildcard-selection/ Fri, 11 Jun 2010 00:00:00 +0000 https://dchua.com/posts/2010-06-11-jquery-wildcard-selection/ To select a wildcard selection on jquery, use the selector: $("[id^=pnl]") with ‘pnl’ replaced by the name of your selector. For example, when trying to select all elements with the id ‘sub_’ prefix, just do a $("[id^=sub]") Breaking tests after renaming tables https://dchua.com/posts/2010-06-09-breaking-tests-after-renaming-tables/ Wed, 09 Jun 2010 00:00:00 +0000 https://dchua.com/posts/2010-06-09-breaking-tests-after-renaming-tables/ Tip: When renaming your tables via rake migration, you might notice that your tests break thereafter with errors referencing your previous tables. To fix that, in your test/fixtures, remember to rename the fixtures from the old table name to the new. Ubuntu - Setting locale failed? Reinstall packages then! https://dchua.com/posts/2010-05-21-ubuntu-setting-locale-failed-reinstall-packages-then/ Fri, 21 May 2010 00:00:00 +0000 https://dchua.com/posts/2010-05-21-ubuntu-setting-locale-failed-reinstall-packages-then/ A quick note to self reminder. If your newly minted Ubuntu box gives you the following error: perl: warning: Falling back to the standard locale ("C").</div> <div> <div>perl: warning: Setting locale failed.</div> <div>perl: warning: Please check that your locale settings:</div> <div> <span style="white-space: pre;"> </span>LANGUAGE = (unset),</div> <div> <span style="white-space: pre;"> </span>LC_ALL = (unset),</div> <div> <span style="white-space: pre;"> </span>LANG = "en_SG.UTF-8"</div> <div> are supported and installed on your system.</div> <div> Just reinstall the packages by:</div> <div> <div>apt-get install --reinstall language-pack-en  It should fix the problem Getting Integrity up and running w/ email notification and test database raking https://dchua.com/posts/2010-05-04-getting-integrity-up-and-running-w-email-notification-and-test-database-raking/ Tue, 04 May 2010 00:00:00 +0000 https://dchua.com/posts/2010-05-04-getting-integrity-up-and-running-w-email-notification-and-test-database-raking/ After spending a couple of hours trying to get integrity setup, I thought I’ll share and document my process for future reference. So what is Integrity? From their website: “As soon as you push your commits, Integrity builds your code, run your tests and makes sure everything works fine. It then reports the build status using various notifiers back to you and your team so everyone is on the same page and problems can be fixed right away. Getting your git repository to send out e-mails upon receiving updates https://dchua.com/posts/2010-04-29-getting-your-git-repository-to-send-out-e-mails-upon-receiving-updates/ Thu, 29 Apr 2010 00:00:00 +0000 https://dchua.com/posts/2010-04-29-getting-your-git-repository-to-send-out-e-mails-upon-receiving-updates/ Here’s a useful tip for those who wants to be updated everytime someone pushes code into a shared git repository. There’s a hook that comes with git that sends out an email to a defined e-mail address with information about a commit that has recently been pushed. It tracks the author, the commit message, the commit log as well as displays the changes made. Lets get started. Prepare your remote git repository In your remote server where your remote git repository resides, you should find the following file in remote_repository_path/. Configuring Gmail SMTP with Actionmailer https://dchua.com/posts/2010-03-30-configuring-gmail-smtp-with-actionmailer/ Tue, 30 Mar 2010 00:00:00 +0000 https://dchua.com/posts/2010-03-30-configuring-gmail-smtp-with-actionmailer/ Just a couple of reminders for myself, in case I spend wasted time trying to get gmail smtp working on actionmailer again.To get your ActionMailer configured for gmail: # in config/environments.rb # add the following lines AFTER your Rails::Initializer.run block  email_settings = YAML::load(File.open("#{RAILS_ROOT}/config/email.yml"))  ActionMailer::Base.raise_delivery_errors = true ActionMailer::Base.perform_deliveries = true ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = email_settings[RAILS_ENV] unless email_settings[RAILS_ENV].nil? in config/email.yml (if its not there, create it) add the following for each rails environment development: :enable_starttls_auto: true :address: smtp. Comparing elements of two arrays for similarity https://dchua.com/posts/2010-03-29-comparing-elements-of-two-arrays-for-similarity/ Mon, 29 Mar 2010 00:00:00 +0000 https://dchua.com/posts/2010-03-29-comparing-elements-of-two-arrays-for-similarity/ When checking two arrays for similar elements, use: (a & b).any? or (a & b).empty? * �or� in the snipplet above, is not an operator, but a geniune �or�From The Momoro Hoax Getting rid of the ^Ms ... https://dchua.com/posts/2010-03-23-getting-rid-of-the-ms/ Tue, 23 Mar 2010 00:00:00 +0000 https://dchua.com/posts/2010-03-23-getting-rid-of-the-ms/ So you�ve opened your text files using a visual editor like gedit and now when you open the same file in vim, you see those pesky "^M" attached to the end of each line.Being a smart little vim�er you try to strip it off with a :%s/^M/ But nothing happens! Instead you get a Pattern Not Found warning.What now?Don�t worry, all you need to do is to do a: :%s/<ctrl-v><enter>/ Hirb - irb in tables form https://dchua.com/posts/2010-03-22-hirb-irb-in-tables-form/ Mon, 22 Mar 2010 00:00:00 +0000 https://dchua.com/posts/2010-03-22-hirb-irb-in-tables-form/ I was randomly searching for documentation on writing tests for ActiveMerchant when I ended up on this article introducing Hirb, an irb addon which lets you view your objects in tables instead of a Hash form.What's really neat is that Nicholas (the author of the aforementioned article) added a neat little tip to log SQL outputs onto your screen:Simply by typing the following in your irb. ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT) Easy deployment to Heroku in a few simple steps https://dchua.com/posts/2010-03-16-easy-deployment-to-heroku-in-a-few-simple-steps/ Tue, 16 Mar 2010 00:00:00 +0000 https://dchua.com/posts/2010-03-16-easy-deployment-to-heroku-in-a-few-simple-steps/ Finally gave Heroku a try this evening.I�ve first heard of them many months ago, but never really tried to use it, probably because I wasn�t sure how it worked, and it looks expensive.After countless advice from friends and mentors to consider putting my next rails app on the platform, I decided to try and deploy an opensource project I�m starting to work on as a testbed.I was pleasantly surprised.All I needed was to install the heroku gem, create an account, add a remote git repository to the heroku git repository, and finally do a push, and I�m done. Use Range to your advantage in conditionals (ruby/rails) https://dchua.com/posts/2010-03-15-use-range-to-your-advantage-in-conditionals-rubyrails/ Mon, 15 Mar 2010 00:00:00 +0000 https://dchua.com/posts/2010-03-15-use-range-to-your-advantage-in-conditionals-rubyrails/ Handy tip, when trying to find all entries from a model that correspondences to a range, you can use a range in your conditionals.an example: date = Time.now Expense.all.sum(:cost, :conditions => {(date.beginning_of_week..date.end_of_week)}) to find out the total cost of all expenses for the whole of this week.Side note: 1) beginning_of_week, end_of_week are ActiveSupport Extensions for time calculations that can be found http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Time/Calculations.html Viewing custom/default error pages on development mode https://dchua.com/posts/2010-03-10-viewing-customdefault-error-pages-on-development-mode/ Wed, 10 Mar 2010 00:00:00 +0000 https://dchua.com/posts/2010-03-10-viewing-customdefault-error-pages-on-development-mode/ Working in development environment, we tend to miss out seeing production-level 404, 500 etc. error codes.By changing the following line to false in your config/environments/development.rb config.action_controller.consider_all_requests_local = true We get to display your custom/default error pages instead of an all so information stack trace. Perfectline Blog explains By default, Rails displays the error pages only in production mode. In development mode you get the all-so-informational exception descriptions and stacktraces. This behavior boils down to Rails either considering your request local (and displaying the full debug message) or “remote” (which means the application is probably in production and/or the debug messages should not be displayed). GSM Cracked - 26th Chaos Communications Congress https://dchua.com/posts/2009-12-29-gsm-cracked-26th-chaos-communications-congress/ Tue, 29 Dec 2009 00:00:00 +0000 https://dchua.com/posts/2009-12-29-gsm-cracked-26th-chaos-communications-congress/ The annual Chaos Communications Congress is barely into day 2 of its 26th meeting and the folks have released published open source instructions for cracking the A5/1 mobile telephony encryption algorithm and for building an IMSI catcher that intercepts mobile phone communication. GSM, you�re no longer safe!The Global System for Mobile Communications (GSM) standard for digital mobile phone networks, which is used by around four billion people in 200 countries, is quite insecure, explained 10 Steps to getting role authorization working on your rails project https://dchua.com/posts/2009-12-03-10-steps-to-getting-role-authorization-working-on-your-rails-project/ Thu, 03 Dec 2009 00:00:00 +0000 https://dchua.com/posts/2009-12-03-10-steps-to-getting-role-authorization-working-on-your-rails-project/ How to get declarative_authorization working on a brand new rails project in 10 steps1) In your config/environment.rb, Add the line config.gem "declarative_authorization", :source => "http://gemcutter.org"2) Do a rake gems:install3) Create a Role model ./script/generate model Role title:name 4) In your migration file that comes with your model, reference the role table with your user model (replace the name of the model as necessary) t.references :user 5) In your User model, create a has_many :roles association6) In your Role model, create a belongs_to association7) Create a authorization file that will contain your authorization file. Holding current rails location for future redirection https://dchua.com/posts/2009-11-15-holding-current-rails-location-for-future-redirection/ Sun, 15 Nov 2009 00:00:00 +0000 https://dchua.com/posts/2009-11-15-holding-current-rails-location-for-future-redirection/ I always have this problem of redirects not going back to the exact page that I want it to. After chatting with some of the rails developers at #rubyonrails on freenode, they’ve provided me with this handy code snipplet to save the current page location temporary and after redirecting it, destroy that temporary location. We can return to this location by calling #redirect_back_or_default. def hold_location session[:return_to] = request.request_uri end # Redirect to the URI stored by the most recent hold_location call or # to the passed default. Howto put your Windows 7 installer on a bootable USB thumbdrive https://dchua.com/posts/2009-11-14-howto-put-your-windows-7-installer-on-a-bootable-usb-thumbdrive/ Sat, 14 Nov 2009 00:00:00 +0000 https://dchua.com/posts/2009-11-14-howto-put-your-windows-7-installer-on-a-bootable-usb-thumbdrive/ With market and technology shifting from notebooks to its light yet powerful cousins, the netbooks, we're gonna see alot more people asking about how to get their favourite Operating System (I'm using the word 'favourite' liberally here) onto it. Netbooks typically do not come with any optical drive as they take up space, but this doesn't mean that we're going to be locked by our vendor's install OS. While researching, I found that Windows 7 has a pretty decent tool, called the Windows 7 USB/DVD Download Tool, which helps you copy your Windows 7 ISO onto your flashdrive. Howto Setup Gmail SMTP on Rails https://dchua.com/posts/2009-11-09-howto-setup-gmail-smtp-on-rails/ Mon, 09 Nov 2009 00:00:00 +0000 https://dchua.com/posts/2009-11-09-howto-setup-gmail-smtp-on-rails/ So now that I've configured this domain on Google Apps, how can I make use of the free e-mail service that Google is providing on my rails application? Gmail SMTP server works with TLS and on a different port number, which threw me off at first, until I found this quick snipplet of code that does the trick. Now this only works on Rails 2.2 and above as well as Ruby 1. Morning Business Observations https://dchua.com/posts/2009-11-09-morning-business-observations/ Mon, 09 Nov 2009 00:00:00 +0000 https://dchua.com/posts/2009-11-09-morning-business-observations/ What started off as a typical Monday morning, ended up being a real-life class in how to conduct business. I was out with my dad for breakfast (which by itself is pretty rare considering that he works, and we seldom eat out for breakfast) at our nearby ‘kopitiam’ (coffee shop), when we decided to replace our aging water heater that no longer works. We stopped by a local hardware store that seems to be running for as long (or more) as the number of years I’ve been breathing. Hello World https://dchua.com/posts/2009-11-03-hello-world-2/ Tue, 03 Nov 2009 00:00:00 +0000 https://dchua.com/posts/2009-11-03-hello-world-2/ Hey there! This is the first post on posterous after letting my old personal domain (frozened.com) of 7 years depreciate. What a journey it has been. With everything going onto the cloud, I thought, why not try and live on the cloud, using tools readily available (and free) like this platform for publishing and Google Apps for my e-mails. This blog will serve as a repository of my thoughts and ideas that I experience as I traverse the new frontier of cyber-communication. How to get Paperclip and AWS-S3 Singapore (and European) buckets working https://dchua.com/posts/2011-12-27-how-to-get-paperclip-and-aws-s3-singapore-and-european-buckets-working/ Mon, 01 Jan 0001 00:00:00 +0000 https://dchua.com/posts/2011-12-27-how-to-get-paperclip-and-aws-s3-singapore-and-european-buckets-working/ Getting the AWS-S3 and Paperclip plugins to work with Singapore (and European) Buckets in S3 is not as straightforward as it seems. In fact, if you’ve followed most of the guides out there, you’ll probably get an error like: {% highlight ruby %} The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint {% endhighlight %} Right now, there’s currently a bug in which non-US bucket URL is not being probably embedded with the aws/s3 plugin and in order to get your Singapore/European buckets to play nice, you’re going to need to make changes to your code.