Send private tweet to Anyone

Have you ever need to send a private tweet to some one that is not following you and you do not have any other way to contact them?

There is a really easy way to achieve it: Twitgram. With Twitgram you can send private messages, of any length, to anyone and only the recipient(s) will be able to read them.

Posted in Uncategorized | Tagged , | Leave a comment

Asynchronous loop with jQuery Deferred

On my previous experience with Flex I faced several times the necessity of delegate heavy loops to asynchronous processes; that was because either, the loop was too big or because each item required some heavy data or UI processing.

I was reading today about jQuery’s Deferred object (used mainly on $.when) and I decided to read more about it, understand it and use it when I required it. Deferred is a really simple but powerful object for some situations and one of those turns to be the asynchronous loops.

After some reading, brain overheat, and testing I came with this async loop that receive an options object with 4 properties:

  • total: The number of iterations. By default 0.
  • context: your this context to be sent to the callbacks. By default the loop this object.
  • limit: How many iterations before doing a pause. By default 100.
  • pause: A pause in milliseconds between each chunk. By default 10.

The more useful options are total and context. With those you can create an asynchronous loop that, upon each callback for each iteration will contain the proper this object.

var asyncFor = function(params) {
    var defaults = {
      total: 0,
      limit: 100,
      pause: 10,
      context: this
    },
      options = $.extend(defaults, params),
      def = $.Deferred(),
      step = 0,
      done = 0;

    this.loop = function() {
      if (done < options.total) {
        step = 0;
        for (; step < options.limit; step += 1, done += 1) {
          def.notifyWith(options.context, [done]);
        }
        setTimeout.apply(this, [this.loop, options.pause]);
      } else {
        def.resolveWith(options.context);
      }
    };

    setTimeout.apply(this, [this.loop, options.pause]);
    return def;
  };

 

And a simple example on how to use it:

asyncFor({
  total: 10,
  context: this
}).progress(function(step) {
  console.log(step);
}).done(function() {
  console.log('complete!')
});

 

As a plus, a more complete example demonstrates the big difference while looping 10,000 times synchronously and asynchronously.

Posted in Uncategorized | Tagged , , , , , , | 2 Comments

Make Chrome work a bit more for you (JIRA)

At the office we use JIRA to manage all the Tickets and Tasks we are supposed to do, and sometimes it might be a little more complicated than expected. For some reason I woke up early today and decided to search for JIRA extensions on Chrome Store and found 2 really nice:

JIRA Notifier

This one is a small popup on your toolbar that opens your selected saved filter and also includes a small number with how many tickets are in that list. It also (as the name says) allows you to get desktop notifications with updates on such tickets.

 

Goto Bug

This one is a much simpler but I will love it. How many times someone asks you about a ticket and only gives you the number? Then you need to open JIRA, search for one of your tickets and change the URL with the ID or search the ID using the search options. No more! Just select your favorite(s) project(s) and you will only need a number to get into each and every ticket!

 

I do not know why I didn’t search for these before 8) .

Posted in Uncategorized | Tagged , , , | Leave a comment

Creating a fake file with Node.js

I know, Node.js is not the best tool to access and create files, but it was what I had at hand, and I do like a lot JavaScript.

Today I needed to create some fake files to test an upload service. Turns out that I had no test files and for more than 1 MB files it is not worth copy, paste and hang the editors, I also tested some fake file services online but after 12 MB the download was failing.

So, long story short, I created a really simple (and ugly) script to create a file of N bytes, and here is the code 8)

var fs = require("fs"),
  params = process.argv.splice(2, process.argv.length - 2),
  size = 0,
  filename = __dirname + '/fake.txt',
  content = '',
  i = 0;

if (params.length === 0) {
  console.log('Specify a file size in bytes');
  return;
}

size = params[0];
if (isNaN(parseInt(size, 10))) {
  console.log('Size must be a number');
  return;
}

for (; i < size; i = i + 1) {
  content = content + '.';
}

fs.writeFile(filename, content, 'utf8');

 

And to execute it, I just execute node with the js file and the size in bytes:

node fakeFile.js 1024

 

Happy file trolling!

Posted in Uncategorized | Tagged , , , | Leave a comment

Simple OOP in JavaScript

I just finish reading JavaScript, The Good Parts, It was a good reading, but not as good as I expected. Some of the parts I do not like is extending built-in objects. Anyway, this is just a simple OOP example based on what now I understand correctly: prototype.

var Class = function(params) {

		// Everything not in "this" will be available to the
		// object but hidden to subclasses.
		var defaults = {
			name: 'Anonymous'
		};

		// Everything in "this" will be available to the object
		// and subclasses.
		this.initialize = function(params) {
			this.options = $.extend({}, defaults, params);
		};

		this.sayHello = function() {
			console.log('Hello ' + this.options.name + ' !');
		};

		// And this is the "constructor" who takes the invocation
		// params and something with them.
		this.initialize(params);
	};

var SubClass = function(params) {

		// Everything in "this" will be available to the object
		// and subclasses.
		this.sayGoodbye = function() {
			console.log('Bye ' + this.options.name + '!');
		};

		// And this is the "constructor" who takes the invocation
		// params and something with them. Note that this class
		// contains no "initialize" method but due to prototype
		// inheritance it is available.
		// Also, we need to explicitly call it, otherwise it wont
		// be executed on the super class.
		this.initialize(params);
	};

// This is where the magic happens. By overriding our SubClass
// prototype we are then inheriting from the new instance.
SubClass.prototype = new Class();

// An here is the actual use if the SubClass
var subClass = new SubClass({
	name: 'John'
});

subClass.sayHello();
subClass.sayGoodbye();

 

And once executed on my console it outputs:

Hello John!
Bye John!

 

As usual, I am open to answer questions and comments :)

Happy coding!

Posted in Uncategorized | Tagged , | 2 Comments