Wednesday, January 29, 2014

From Zero to App Store with HTML5, APIs and PhoneGap- Making my first phone app

Thanks Richard (friend and fellow student from General Assembly) for letting me know about an awesome meeting @ Apigee to build an API driven mobile app. I've been looking for something new and fun and this is it!

I'll post the juicy details of the one day bootcamp afterwards... should be fun... 

Friday, January 17, 2014

Benchmarking, what a difference a line makes

What a difference a line of code makes
32 little lines
...
Ok, now that we have Renee Olstead's song in our heads lets get what it is I'm trying to say:

Code that works may very well not perform well. I wanted to see about writing some code to figure out the first n prime numbers. Pretty easy stuff:


I got to thinking that the downside of this is that I am running the modulo operator on each and every number. This clunkiness isn't so apparent on testing the first 100 numbers, but, what if we are talking about 100,000?!?

More to the point, how to even see what is the time spent?

require 'benchmark' => from ruby docs we can see that this module lets us see what the time factor really is.



From the docs we understand that the time breakdowns are: CPU time, system CPU time, sum of user and system CPU time and, arguably, most importantly, elapsed real time (the on in quotes: (0.000100) )

Lets change array_test(100) to array_test(100000). This will run our benchmark on our method to show us the prime numbers 1 - 100,000.


Yeah, a big time difference. Before coding I wouldda thought there would have been a linear difference, but here we see instead of it taking x1,000 more, it is x100,000. Here is our first taste of Big O, and I'll go over that later. But lets instead focus on making this faster.


Nice... ~x6 faster

what does adding (2..Math::sqrt(max)).each do |i| help us achieve?

Essentially we have to recognize that x * y =  z. If z is not a prime then there has to be two numbers x * y = z. If both x and y were are greater than the square root of n then x * y would be greater than n; This then means that x or y must be less than (or equal to) the square root of n (thanks to Edward Guiness's book for really helping me understand this).

So, instead of checking all the numbers 1-100,000 (as the iterated array using i), we can test a much smaller set of numbers. 

Sunday, January 12, 2014

Deleted a whole lotta files, now what about git?

I'm sure we've all run into this problem: Want to cleanup a directory since it has a lot of old files we are no longer using. But when we want to commit our changes using git we see something like this:


I can think of very few other things than manually doing "git rm blahh blahh blahh" x many times which would annoy me more. Maybe TMNT on the NES ... ;)

So, I figured that "git add . " adds all the modified/new/renamed files, so why not try "git rm .". Well that didn't work, but there was a temping thought in my mind to try "git rm . -r". This recursively deletes files, and I thought it would only do the ones in question. Alas, it went about deleting everything... recursively ... duhh.

Fortunately, I've become git obsessed so with a "git reset --hard" I went back to the previous commit and all is ok.

After some sleuthing, it turns out there is a one trick pony to be able to do what I want,
git rm $(git ls-files --deleted)

With a git commit we're good to go!

Now, looking at the way I removed the files it appears to narrowly select the deleted files, I wonder if this will work on other types too (modified, new, etc)?

I'll try to remember to try when I come across this next. 

Friday, January 10, 2014

A link in an ALT that is being referenced to another image? Wait whaaa...??

I am in the midst of putting up some of the final touches to my personal/portfolio website (tejas-mehta.com) and one thing I really wanted to do was to include a link to my projects.

Sounds easy, right??

BUT what if Isotope is used to dynamically show the full pic after clicking on a thumbnail, that full pic being referenced via another link AND the text is shown within an ALT tag? Now are you squirming?? (only the cronies will get that joke)

After a few tries I used the most important lesson I have ever learned: Keep at it, the answer is there, you'll get it done and search around. Yes, I consider that ONE lesson... they flow together.

Here's my code before tinkering:






And after lots of testing... sweetness









A small lesson: links can indeed be included within alt tag.... here's the result:



That nice orange link works... nicely ;)


Wednesday, January 8, 2014

Why ask for twitter when a blog is way better?

I was filling out a "info-form" for General Assembly's employer meet and greet and was surprised that while there is a space to put in my twitter account, there wasn't anything for my blog!?!

While I am not against twitter, I feel that having a space for a blog is better. Granted, I dont blog so often and that a great thing about twitter is that you can splurt out your thoughts real fast.

Anyway, long story short I should blog more often ;)

Thursday, January 2, 2014

Changed Database... now gotta rake task for Heroku, but how?

Issue: Ok, so for Hacker Corral I never setup a "forgotten password" field in case, well, if someone forgets their password. To do this I need to setup an Authorization Token for each user so that when an email is sent, that specific token, which will be assigned to a user some random token (SecureRandom.urlsafe_base64) is sent with the email, rather than just the user id number.

Here's the problem: the database on my Heroku deployed version has actual users, not fake ones. On my "local" version it was ok for me to just rake db:reset my database... but I dont want to do that on a live site. Doing so would delete my users.

The solution: RAKE TASK it baby!! ;)

What this means: Essentially, it will go thru my database and generate the auth_token as needed for those users who dont have one. Pretty cool, no?


lib/tasks/rebuild_token_auth.rake

+namespace :user do
+  desc "Rebuild Auth-Tokens"
+  task :rebuild_auth_token => :environment do
+    User.transaction do
+      User.all.each { |u|
+        u.generate_token(:auth_token)
+        u.save!
+      }
+    end
+  end
+  
+end

The only "downside" of this particular implementation is that it the auth_token that will be sent to the user will be the same for any future request to update a password. I'll probably do something like running this particular task once a week/month/whatever to keep those auth_tokens changing, thus preventing someone who may get a user's token from being able to reset the password. 

Anyway, next steps are simple. 

1. git add .
2. git commit -m "rake take to generate auth token"

This is the part where one has to think about what is going on with heroku.

3. heroku run rake user:rebuild_auth_token
       this then populates the auth_token as needed
4. for good measure, heroku restart

Now we have populated what is needed, and the users who registered before I created the reset password option can do so as well, yay!