February 17, 2010

Everytime I look at ruby I just have to say “Wow”

Filed under: Uncategorized — Tags: , , , — weatheredwatcher @ 12:56 pm

So here is the background: At our last Mac User’s Group gathering, my good friend and Ruby on Rails Jedi, Jed Schneider in the process of his talk showed a pretty nifty little ruby script that created a single file that repeated the same line over and over again a set number of times. It was pretty sweet. Fast forward to almost a week later and I am having to create a massive amount of test data for some final testing. I need to create a grand total of 61 csv files, all with different names but that contain the same five email address (as well as some other information) So I decided to try and implement a solution in Ruby, drawing on the concepts of Jed’s script. Ian and I sat down for a spare second and knocked out a little script that does just that.

Surprisingly enough (or un-surprisingly enough if you are a ruby programmer) that’s all she wrote.

The line 1.upto(61) relies on the fact that in Ruby everything is an object. We want to output the numbers 1 through 61. We use |i| to catch the output into a variable i then use File.open to create a file called test1.csv through test61.csv. For each file we want to write some lines. Each line can be written with file.puts. Notice that we pass the name of our file to a variable called file with |file| just like we did for i above.

So after I created 61 csv files with different names but the same data, I decided to check and see if ruby could help me with any of the other test data generation. Of course it could!

The next little bit of code is really just based on the code above. Instead of writing the same line of data to 61 different files, I wanted to write 61 different email address to one file, using the same base. (client101@domain.com, client102@domain.com ect.)

Considering that this is pretty much the same as the previous script, only inside out, I’m sure you can figure it out without much effort.

This Ruby Wow Moment was brought to you by the Letter R and the Number Awesome

Questions? Comments? Any ruby Jedi’s out there that can see of a better way of doing this? I await your comments with much anticipation!

February 1, 2010

Custom Error Handling with PHP

Filed under: programming, work — Tags: , , , , — weatheredwatcher @ 3:06 pm

One major headache in any major project is how you are going to handle the errors. For starters, it is not a good idea to show errors on a live, production site. But on the other hand, you don’t want your system to blow-up and you not know what has happened. There are lots of ways that you could handle this situation. This is just the way that I am handling it on my current project. Background: My current project is a behind-the-firewall-intranet application. It is not accessible for out side the office. So it’s safe to leave error messages on…except for the fact that no one wants to see the error messages except for me. I was given the task of making errors as terse and minimum as possible in their output, while at the same time recording more detailed information in some kind of log file that could be seen my anyone who wanted to know more information.

set_error_handler

The first bit of magic that I used to accomplish my goals is the set_error_handler tag. Essentially, we create a function, when passed the error codes, out puts user friendly message and can even keep the program running after a near fatal error.

We pass the function the error number, string, file and line number. This allows us to create useful output. We can even branch out further, displaying different messages depending on the error number.

The only thing we have to do to make this work is to place the function set_error_handler(“myErrorHandler”); on every page that you want this custom error handling to occur.

The next step was to figure out a way to store the data without actually displaying it. To do this, I wrote a simple class file that writes data to a database. You can just as well write the data to file. The principle is the same.

The new function for handling the errors looks like this:

This way, I can use simple checks to see if processes for successful or not, writing any messages to a log to check later. I also dump the contents of the log in a table so that if a user throws an error, they could potentially see what that error was.

Feel free to use this code in your own projects. You can always change it to write to a flat file, csv or xml file.

January 30, 2010

Thoughts on the new iPad

Filed under: personal, programming — Tags: , , , , , , , , , , , , — weatheredwatcher @ 7:56 pm

Ever since the iPad was announced the other day, I have been giving a lot of thought to it. I have to admit, I’ve been thinking a lot about cloud computing the past few weeks, and this just gave those thoughts a whole new avenue. The biggest thought that I have is this: Could I, a developer, use the iPad as my primary computing device?

Well, for starters, what do I use my Mac Book for that I couldn’t use the iPad for. The answer is not that much. I develop my own personal website, do a lot of stuff with JavaScript, PHP and Rails. I write, play around in Photoshop, email, twitter, blog, listen to music on iTunes and surf the web.

Ever since I got a full time job, I’ve not been consulting much. Most of the heavy coding I do at work on my Mac desktop.

So, in order to use the iPad there are some “products”/functionality that must be replaced/replicated.

Email: I use Mail for my email, so what difference does it make to me if it is on the mac or iPad? I use my iPhone for about half my email now anyway.

Browsing: For this one I might miss Firefox some, but I still use Safari about 25% of the time anyway. I also browse a lot on my iPhone. The larger screen would be nice.

Tweeting: I use Twitterific both on the mac and the iPhone, so no issues there

Blogging: I usually use the web interface, but I also have used the WordPress app. I think that on the iPad, this would be a far better experience than on the iPhone

Writing: With iWorks AND Google docs, I think it is safe to say that this is a no-brainer. I would more than likely want the keyboard for the iPad for any major writting…but doable.

Photos: I have been working to get all my photos on PicasaWeb anyway. Photos are an easy issue. I also have a Photoshop.com account and the iPhone app for Photoshop.com rocks. GIve Adobe a sec to get it updated for the iPad and I am in business.

This leaves me to the two hardest aspects: Music and Coding. For music I would have to cheat just the tiniest bit. I would have to store my music on my wife’s pc and use iTunes to keep the iPad and iPhone with music…but I can also use LastFM whenever I have a WiFi connection (or always on the iPhone) to listen to my library as well as LastFM’s picks for me :) . Also, if Apple brings iTunes into the cloud, then there is a major issue fixed.

This now leaves coding. I have been playing around with Bespin since it first started in the Mozilla Labs. It’s a great place to store and edit code in the cloud. That being said, until they get Git support, I will only be able to test HTML and JavaScript in Bespin. In walks http://www.kodingen.com. This service is built around a Bespin core, gives you testing servers for PHP, Rails as well as Perl and Python support. The three open source Database players are there: MySQL Postgres and SQLite. It is a developers dream land. Slick and stylish. With this final piece of the puzzle, I really could live for sometime developing and living in the cloud on an iPad.

What do you think guys? Should I do it? Leave me comments and let me know!

January 19, 2010

My new Mac Desktop (at work)

Filed under: Uncategorized — Tags: , , , — weatheredwatcher @ 6:24 am

I got a new mac at my job. I opted for a Mac Mini, fully upgraded to 4gigs of ram and a single 500gb harddrive. I also got two 20″ HD LCD monitors. It’s a sweet deal. Here is a side-by-side screen shot of both on the desktop.

left_screenright_screen

Since I use the machine mainly for web-development, I have a slew of browsers installed, including IE8 via Windows 7, Bootcamp and Parallels. I created aliases for each browser and placed them in a stack on the dock for easy access

stacks_browsers
Uploaded with plasq’s Skitch!

Another thing I like to do is use the Secret plugin to hide the desktop and then load the desktop via a stack on the dock as well.

stacks_desktop
Uploaded with plasq’s Skitch!

December 25, 2009

Classless Object Oriented Programming (lesson 2 prototype)

Filed under: Uncategorized — weatheredwatcher @ 7:10 pm

Merry Christmas! Today is also my daughter’s fourth birthday. She sat with me and helped me prepare for this lesson.. We created a Girl class and a Girl object called Luba. Then we made her sing! It was alot of fun. I don’t think she understood Object Oriented Programming, but it was a great start.

I’ve decided to do a little bit more on prototype and why this is a pretty cool concept. For starters, lets take a look at our previous class, Car. What if I took a look at my class and decided that it was not enough.

function Car (make, model, carColor){
this.make = make;
this.model = model;
this carColor = carColor;
}

What if I needed to add another property, say engineSize, I could add a line in my class and hae engineSize, but what if I only need a property if a certain condition occurs. This is where the prototype property is a great thing. I highly recommend you use firebug and give this code a try. It might not be programmatically practical, but it is a great exercise.

Go ahead, open firebug and switch to the console. I highly recommend that you switch the command line to Larger Command Line – this gives you a large command block on the right hand side. Now, enter the following code into the command line and run it.

function Car(make, model, carColor){
this.make = make;
this.model = model;
this.carColor = carColor;
alert('Smell that new car smell!');
}

Once you see the results in the console, clear the command line and enter this code

var myCar = new Car('Ford','Mustang','Red');

You should have seen the prompt that new car was created. So now we have a new object that we can play with.

Now lets define a new property and call it carsMPH.

Since we are running this is the console, we really can’t re-do the class, so we will use the prototype property.

Car.prototype.carsMPH = 'How Fast';

If you type alert(myCar.carsMPH); you will get ‘How Fast’. This is because we have not set a unique value for this variable and so the default is used. We can define the cars speed with

myCar.carsMPH = '30';

Now we follow up with the previous alert and we now get 30. Here we come on another issue. Having to enter in the alert phrase over and over again is a bit annoying. So lets create a method that will display the cars speed. Once again, since we aren’t able to change our class after it’s run, we will use prototype.

Car.prototype.speed = function(){
alert('You are going ' + this.carsMPH + ' miles per hour');
}

Now all you have to do is type myCar.speed(); and a dialog box pops up and shows you how fast our car is going.

So there you go! That was a bit more indepth overview of prototype and classless object oriented programming in JavaScript. With that background out of the way I think that we can move on to some more complex concepts.

December 23, 2009

Classless Object Oriented JavaScript Programming (Lesson 1)

Filed under: programming — Tags: , , , , — weatheredwatcher @ 10:31 pm

**Made a few changes. Added the word Classless to the title, since this is not the exclusive method of oo programming in javascript, but rather this is a classless method of oo programming in javascript. **

I’ve been exploring Object Oriented Programming with JavaScript. It’s been an interesting experience because JavaScript is not really a traditional Object Oriented Language but rather a prototype language. Prototype is a type of object oriented language that is classless. In a prototype language, inheritance is achieved by reusing prototypes of objects. It is a fascinating model.

So let’s get started on our exploration of class-less object oriented programming.

function Car(){
}

In the code above, we actually created a function called Car(). In JavaScript, we don’t actually create a class, we use a function. Now let’s add a constructor. We will add an alert box for our constructor.

function Car(){
alert("Smell that New Car Smell!!");
}

Now, when ever we create a new Car prototype, we get this alert. It’s a great way to make sure it’s working!

Now lets add some variables to our class. For a car we will need a make a model and a color.

function Car(make, model, carColor){
this.make = make;
this.model = model;
this.carColor = carColor;

alert("Smell That New Car Smell!");
}

Now, after the function, we need to do a little bit more.

Car.prototype.make = 'Car Make';
Car.prototype.model = 'Car Model';
Car.prototype.carColor = 'Car Color';

This sets the default value of the make, model and color. It also introduces us to the prototype property. Later, we will use this prototype property to create our sports car class that inherits the Car class.

Now we are all set to create some cars!

var myCar = new Car('Ford','Mustang','Red');

The cool thing about JavaScript is how methods are handled. We actually define a function with the prototype property. Just remember that we need to use ‘this’ to access the variable in the class.

function Car(make, model, carColor){
this.make = make;
this.model = model;
this.carColor = carColor;

alert("Smell That New Car Smell!");
}
Car.prototype.make = 'Car Make';
Car.prototype.model = 'Car Model';
Car.prototype.carColor = 'Car Color';
Car.prototype.goFast() = function {
alert('Let\'s go fast in my ' + this.make + ' ' + this.model);
}

var myCar = new Car('Ford','Mustang','Red');
myCar.goFast();

Go ahead and play around a bit with this. Net, we will talk about how you can use prototype to create another class that inherits another class.

December 17, 2009

Syncing with Google Calendar (all of them)

Filed under: Uncategorized — weatheredwatcher @ 8:07 am

I have been using Google “products” for years now. When every something new comes out, I just have to give it a try. I love Google Notebook, but it seems that not enough people shared my love. Of course Gmail can be called life changing. Another great product that has gotten me through a lot is Calendar. Being able to sync work and classes and family events in a place that is accessible anywhere is a very cool thing indeed.

I also have to say that I love my Mac and my iPhone. Syncing the three is fairly easy. You subscribe to your calendars in iCal, and sync iCal with the iPhone. The only thing is, my iPhone is always with me, the Mac and iCal not always.

So it became necessary to find a way to create a two way sync between Google Calendar and my iPhone. Oh yeah, and I have more than one calendar and I want them all to sync.

Using the Exchange server on the iPhone to connect was easy, but I didn’t like have the mail was handled. IMAP works just fine thank you very much. Also, it only syncs your primary calendar. If I created other calendars, or subscribe to others, they don’t even show up.

I found some articles on how to sync using CalDav and it seemed to be just right for my needs.

To sync your primary calendar, add a new CalDav to the iPHone and enter this in as your url: https://www.google.com/calendar/dav/you@gmail.com/user/

Add your google username (username@gmail.com) and password and you should be good to go.

Now, for the other calendars, you need to goto your calendars in google and take a look at the ical link. In the string, you should see a gianormous string @group.calendar.google.com. Write that down. Then, add another caldav calendar and in the url enter this: https://www.google.com/calendar/dav/[string of numbers and letters]@group.calendar.google.com/user/

Fill in the username and password same as above. And there you go! A few things to note:

  • You must use https
  • The url will disapear in both instances and be repalced my https://www.google.com It’s suppose to

On another note, I have been playing around a bit with Thunderbird 3 and I am impressed with it as a mail client. I found a Lifehacker article that talked about hacking a tab to display Google Wave permanently and that’s made Thunderbird very useful. I used Thunderbird for my email in Linux so I am comfortable with it. The only thing that is a bit cumbersome is the calendar extension, Lightning. Sure I installed a Nightly Build for 3 and it works fine, but Google Calendar Syncing is a bit of an issue. Once again, I can subscribe as caldav to Google Calendar (use the aboce strings, but change /user to /events) The only issue is that I can’t add or change secondary calendars in Lightning. It has some permissions issues with Google that havn’t been worked out yet. So I applied the same hack that let me have Google Wave in my Thunderbird to give me Google Calendar in my Thunderbird. The result is a great , quick fix to the Calendar syncing issue!

November 12, 2009

Object Oriented PHP Lesson 3: Coding a mailer Class

Filed under: programming — Tags: , , , — weatheredwatcher @ 10:30 am

Today we are going to create a mailer class that will assist us in emailing site visitors. Lets start out buy creating the class and the variables we will need.

class Mailer {

}

Okay, let see. We will need a sender, a recipient, a subject and a body. Well, if you know anything about email, the sender is set in the header. So…

class Mailer {
   var $headers;
   var $recipient;
   var $subject;
   var $message;
}

Now comes the fun part! Lets build the email!

First we have a from setter:

function from($email, $name){
         $this->headers[] = 'From: '.$email.' <'.$name.'>';
     }

What we have done, is we have appended the required format for the Header. In php the ‘.’ is what we use to concatenate strings and variable data together. Also, note that the header is an array. We could add more than just “From” to the header if we needed to.

Now we add rest of the functions.

 function add_recipient($email){

        $this->recipient = $email;
     }

     function subject($subject){  

        $this->subject = $subject;
     }

     function message($message) {

        $this->message = $message;
     }

Notice that we don’t have any getter methods defined. Although it’s a good practice to have them in your class for a program, it would just add extra stuff to a class that’s sole purpose in life is to run a contact me page. Now, if you wanted to use this in a larger site. Say a cms or a social portal, you would want to add a bunch more functions to it. That way you could get all of your email functionality out of a single class file.

But for now, this is enough. Let’s create a utillity function to send the mail.

function send()
     {  //sends the email

        $headers = "";
            foreach($this->headers as $header)
            {
               $headers .= $header.';';
            }
            return mail($this->recipient, $this->subject, $this->message, $headers);
     }

Essentially, we add all the header information, then the rest of the data is pulled into the mail function.

And how do we use this? By now I hope that you understand the layout of my driver files. So it’s your turn. Create a contact form and use this class to make it work.

November 11, 2009

Object Oriented PHP Lesson 2

Filed under: programming — Tags: , , , — weatheredwatcher @ 7:32 am

Yesterday we talked about some basics in oo php programming. To day I am going to show you how to extend a class and also how to be utillity methods (functions) in your class.

Yesterday we created a car class called Car. To today we will extend that class with a sportsCarClass. Why, you might ask, do we want to do this? It’s simple. I might want to create several different types of cars. Maybe I want a SUV car object, or a truck object. All of these objects will have certain properties that are the same (make, model, color, ect) Why wouldn’t I reuse the code that I already have? I could copy-paste, but that would only serve to make my program a whole lot larger than it needs to be. So, instead, I’ll extend my base class to make other classes. The one that I’ll be demonstrating is the sportsCarClass. We start out by declaring the class and extending the base class, followed by declaring any variables that this new class will be using.

class sportsCar extends Car  {
   var $engine;

In case you are wondering, this is in the same class file as our original carClass.

Next we will define some setter and getter functions for the sports car class.

	function add_engine($type){ 

        $this->engine = $type;
     }
	function get_engine(){  

         return $this->engine;
     }

I don’t need to show you how to use this in your driver program. Give it a go! You will notice that you can create a new sportsCar and set make, model, color and engine and it will work, even though you don’t have any setter functions for make, model or color in your class. Also, if you try and set the engine type in a Car, it will fail because Car cannot see sportsCar.

At this point, it might be a good time to point out a few items of importance. Did you notice that in our driver program we call the functions using the class name followed by -> and the function? We refer to the variables in our class in the same way, but we use $this instead of the class name. This is important, because we can also use this to pull variables into other function in our class in this same way.

Lets make a utillity function that will display our object in an easy to read format.

	function show_car(){
		echo('Make: '.$this->make.'');
		echo('Model: '.$this->model.'');
		echo('Color: '.$this->color.'');
		echo('Engine: '.$this->engine.'');
	}

Next time we will use oo programming to do something infinitely more useful. We will create a mailer class that can be used in a contact form.

November 10, 2009

A lesson in Object Oriented PHP

Filed under: Uncategorized, programming — Tags: , , , — weatheredwatcher @ 2:22 pm

I’ve casually referenced oo php recently. So here I am going to give a more formal tutorial on object oriented php for anyone out there who is unaware of OO programming in general. If you are familiar with OO programming, you will understand everything immediately.

The first thing that we are going to create is a class. This class will allow us to make an object based on that class.

Think of a class as a cookie cutter. We make objects from this class. Each object should be the same as all the others, except for the data that we place into the object.

class Car
{
   var $make;
   var $model;
 }

Here we have defined our class “Car” as well as two variables, “make” and “model.” Now we have to make some setter methods for our class

 function add_make($name)
     {
        $this->make = $name;
     }

 function add_model($name)
     {
        $this->model = $name;
     }

These two functions set the variables for the make and model. The next functions are called getter methods, because they ‘get’ the data from the object.

function get_make()
     {
        return $this->make;
     }

function get_model()
     {
         return $this->model;
     }

Next we will build a driver file to test out our class.

require_once('carClass.php');

$myCar = new Car;
$myCar->add_make("Ford");
$myCar->add_model("Explorer");

echo $myCar->get_make();
echo $myCar->get_model();

Basically, we first create a new object “Car” called myCar. Then we add a make and a model to the Car object. Finally, we echo off the variables to see what we have set them as.

Here is the complete class file for you to study. For homework, make a getter and setter method for color and then add it to the driver file.

carClass.php:

class Car
{
   var $make;
   var $model;
   var $color;

     function add_make($name)
     {
        $this->make = $name;
     }

     function add_model($name)
     {
        $this->model = $name;
     }

     function get_make()
     {
        return $this->make;
     }

     function get_model()
     {
         return $this->model;
     }

};
Older Posts »

Powered by WordPress