Friday 20 September 2013

Rails 4: Patch JSON encoding to support emojis

Add this to file under `config/initializers/`

module ActiveSupport::JSON::Encoding
  class << self
    def escape(string)
      if string.ascii_only?
        string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
      end
      json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
      json = %("#{json}")
      json.force_encoding(::Encoding::UTF_8) if json.ascii_only?
      json
    end
  end
end

Friday 5 July 2013

List all tasks from your custom rake file

namespace :my_application do |ns|
  desc "Lists all tasks available"
  task :tasklist do
    ns.tasks.each do |t|
      name   = t.name_with_args
      spaces = name.length >= 25 ? "\t" : "\t\t" # hack for spacing
      puts name + spaces + t.comment.to_s
    end
  end
end

Thursday 4 July 2013

Thursday 8 November 2012

Ruby script to send windows toast notification

A simple ruby script to send MSDN toast notification for windows phones:

      require 'uri'
      require 'net/http'
 
      u = URI.parse('your-device-url')
      req = Net::HTTP::Post.new(u.path)

      req_xml = '<?xml version="1.0" encoding="utf-8"?>'
      req_xml << '<wp:Notification xmlns:wp="WPNotification">'
      req_xml << '<wp:Toast>'
      req_xml << '<wp:Text1>Test title</wp:Text1>'
      req_xml << "<wp:Text2>My First notification</wp:Text2>"
      req_xml << "<wp:Param>/Page1.xaml?id=123</wp:Param>"
      req_xml << '</wp:Toast>'
      req_xml << '</wp:Notification>'

      req.content_type = 'text/xml'
      req['X-WindowsPhone-Target'] = 'toast'
      req['X-NotificationClass'] = '2'
      req.body = req_xml

      resp = Net::HTTP.start(u.host, u.port) do |http|
        http.request(req)
      end

Wednesday 19 September 2012

Syncing Amazon S3 buckets

I would recommended two tools to sync data to s3 bucket or between two buckets.

First comes is s3cmd sync which is a command line.
For those who are not comfortable with the command line can checkout - DragonDisk

Wednesday 6 June 2012

rsync - simple usage

rsync -Avrzh --ignore-existing src/ dest/

This command syncs the new files or dirs from /src to /dest recursively.

Wednesday 30 May 2012

Linux: Delete old log files

Here's a simple shell command to delete old log(or any) files.

Syntax:

ls -t <list of files> | tail -n <count> | xargs rm -f

Usage:

 ls -t /data/www/myapp/shared/log/*.log.gz | tail -n +6 | xargs rm -f

This will remove all compressed log files leaving recent 5 backups.