Github, Heroku and Sensitive Data

Daily Learning:

Don’t be intimidated by new environments. Old concepts usually still hold true.

I am fairly new to Ruby/Rails as well as Git, Github and the whole cloud thing.

I created a Rails application and discovered that I could host it in the cloud, for free, on Heroku (heroku.com).  As an aside…Heroku is AWESOME! :)  Of course if your data or traffic become large enough you will eventually need to pay, but that’s more than reasonable, yes?

At any rate, I had hosted this app out there for a few months when (being new to Rails) I realized that I had been publishing my “secret_token” that Rails uses for session authenticity as well as my email address and email account password, as my app sent email via SMTP.

Horrified, I faced a confusing array of decisions:

1) I could remove the files from my repo altogether, but then the app would no longer work upon my next push to Heroku.

2) I could add these files to my .gitignore file. In fact I did this, but I still ended up altering my .gitignore file every time I wanted to push to Heroku and then push to Github.

3) I could have also upgraded my free Github account to paid (micro) account which would then be private and this would no longer be an issue. Actually, I DO intend on doing this, but not just yet. I still needed a better answer for now.

Finally, I stumbled across a page on the Heroku Dev Center that addresses this. (https://devcenter.heroku.com/articles/config-vars)

In a nutshell, I changed my Rails “mail_setup.rb” file in config/initializers to look like this:

ActionMailer::Base.smtp_settings = {
:address => ENV['SMTP_ADDRESS'],
:port => ENV['SMTP_PORT'],
:domain => ENV['SMTP_DOMAIN'],
:user_name => ENV['SMTP_USER_NAME'],
:password => ENV['SMTP_PASSWORD'],
:authentication => ENV['SMTP_AUTHENTICATION'],
:enable_starttls_auto => true
}

Also in config/initializers I changed the “secret_token.rb” file to look like this:

MyApp::Application.config.secret_token = ENV['SECRET_TOKEN']

and I used this handy line of code in irb to generate my secret token:

rand(36**100).to_s(36)

Thanks to Thibault Barrère for that trick:  http://blog.logeek.fr/2009/7/2/creating-small-unique-tokens-in-ruby

Problem solved. I hope this helped.

Do you have any configuration tips you’d like to share? Please leave a comment!

iOS…again

Daily Reflection

Today’s time is being spent reading through the docs @ developer.apple.com, specifically the iOS docs.

I took a stab at iOS about a year ago. In retrospect, I was trying to learn a platform to achieve an end (make money!) without having developed a clear idea of the means (a specific app). Not having a specific purpose probably was my undoing. Funny how app dev can mirror real life (have identified goals!)

Now, I have a goal in mind. It’s simple game and I’ve already POC’d the whole damn thing in JavaScript. Tt runs in a browser, so I know that the concept is viable. In fact, I’ve had others preview it and give me feedback to improve it even more.

Of course, now, I think that I can learn ANY development language without having a specific app in mind. This is because, rather late in life I’m afraid, I’ve developed a “checklist” to learn the fundamentals of any programming language, even if not the nuances that make that language special.

Example: In ANY language you will have the following notions:

  • data types (or not)
  • collections
  • conditionals
  • operators
  • looping/iterators
  • memory management
  • modularity
  • user interface options
  • asynch operations
  • IO
  • level of object-orientation supported (classes, interfaces, polymorphism, mix-ins, operator overloading)
  • implementations: Are there multiple implementations of the language? If so, why? (Example: Ruby (MRI, Rubinius, JRuby, mRuby, etc)

If I sit down with this checklist and a language, I can at least get two feet firmly planted on the ground.

This new, daily compendium format serves multiple other purposes: consistency/habit, a reference for myself and proof that I really did learn something new that day. I think the first part of each post will consist of a bit of reflection and the second, the specific items learned.

Daily Learnings

  1. in X-Code, you can Ctrl-Click on most anything to get Help for it.
  2. As I gear up for learning Objective C to write iOS apps, this very weekend I see that mRuby has made a splash as Matz has released a version of Ruby for mobile devices. Sigh…
  3. And someone has already created a REPL (Read-Eval-Print-Loop) for it (minus ‘print’) http://www.avocadodb.org/2012/04/22/towards-an-interactive-mruby-shell
  4. X-Code has built in support for creating a local git repo for your project.
  5. Objective C clases require two files: a header file (.h) and an implementation file (.m).  The interface file contains the class’s public API. The implementation file, as the name suggests, contains the code that implements the class.
  6. Everything declared in the public API goes inside the class declaration which is denoted by @interface <Class Name> : <inherits from> ( usually from NSObject ) and the class ends with @end

Date validation, display and parsing in Rails

 

 

When working with date formats and date validations in Rails, I have particular set of behaviours (spelling: a shout out to Peter Cooper)  I want to achieve. The code I’ve achieves this behaviour, but it seems to me it should be easier than this.

 

I include code so that 1) if you have been struggling to achieve these same behaviours, this code provides a solution, and 2) if you’re an old hand at Rails and I am writing more code than I need to, I would *really* appreciate any recommendations you can provide. Also, if any of this code is obnoxiously counter to “idiomatic Ruby/Rails”, meaning it works, but it’s “just not the Ruby/Rails way”, please let me know that as well.

 

Here is the behaviour I want in validating and formatting dates in Rails:

 

1) Validate that something has been entered.

2) I want to use the “chronic” gem because I love its ability to parse input such as “today” and “next Tuesday” and return the correct ruby Time object.

3) When editing an record (already has a valid date) and the user enters an invalid date value, or enters blank, I want the application to keep displaying the invalid value so they can see what they keyed and determine why they’re getting an error message. The default Rails behaviour is to redisplay the original, valid value.

4) Finally, I want the dates to be fetched from the database and displayed to the screen using the North American format of “mm/dd/YYYY”.

 

 

I created a micro application with one model, “Person” (table = “people”) and only three attributes:  first_name, last_name and date_of_birth.  In order to achieve all of the behaviour I listed above, I had to create a “virtual attribute” called “date_of_birth_display” which works with the view and use this field for shuttling date values back and forth between the view and the database as well as formatting purposes.  

 

 

Here’s the important part of the view code:

 

<table cellspacing=”0″>

<%= form_for(@person) do |f| %>

        #… Other fields omitted for brevity…

        <td><%= f.label :date_of_birth, “Date Of Birth” %></td>

        <td><%= f.text_field :date_of_birth_display %></td>

    </tr>

<% end %>

</table>

 

 

And now the model code:

 

require ‘chronic’

class Person < ActiveRecord::Base

  validate :date_of_birth_display_is_parseable? 

 

  def date_of_birth_display

    date_of_birth.strftime(“%m/%d/%Y”) unless date_of_birth.blank?

  end

 

  def date_of_birth_display=(val)

    @date_of_birth_display = val

  end

  

  def date_of_birth_display_is_parseable?

    self[:date_of_birth_display] = @date_of_birth_display

    

    if Chronic.parse(@date_of_birth_display)

      return self[:date_of_birth] =  Chronic.parse(@date_of_birth_display)

    end

    if @date_of_birth_display.blank?

       return !errors.add(:date_of_birth, “cannot be blank.”)

    else

      return !errors.add(:date_of_birth, “not a valid date.”)

    end

  end

end

 

Foundational Git feedback

I have just begun writing a book covering the open-source version control software called “git”.

The book is titled “Foundational Git” and can be read online, as I write it, at http://www.joemeirow.com/writing/books/foundationalgit/index.html. I am writing this post on 11/2/2011 and I have just basically started the book today.

Much about the book will change as I go along, but if you have any comments, please leave them here in the comments for this blog post. Thanks!

 

Joe Meirow

 

The best way to learn something…

While there are many positive aspects to working in a small development group, the downside is that we’re not exposed to many different points of view in the workplace. Unless we proactively seek outside influence through user-groups or actively monitor what’s new via the web, it is easy to unwittingly weave a cocoon around one’s self.

Over the past couple of months, I’ve been on a mission to sharpen my developer chops. Ours is a C#/.NET shop and so that was a logical place to start. After reading several blog-posts and articles expressing concern over the future direction of development in the Microsoft space, I began to get a bit nervous and for the first time thought about what development platform we should switch to, if it ever came to that.

For the time being, I am now relatively comfortable with the whole Windows 8/Metro thing, but during that unsettled time, I really began to thing about my life as a developer/architect in a completely different camp. One thing I knew for sure, Java was out.  I’d been there, done that long ago and have no desire to return. One of my co-workers is a huge Python fan and so I began to check out Python.

In the course of researching Python, it didn’t take long at all for Ruby to show up in many of the same web searches. And then, I was hooked.  I still haven’t written much more than a “Hello, World!” program in Ruby (been pretty busy with the C# stuff), but the Ruby language seems positively amazing and the  community absolutely electric. There is so much material available from the community that it’s almost overwhelming. I especially enjoy The Ruby Show with Jason Seifer and Peter Cooper. It is actually as entertaining as it is informative – a real “win/win”. Also extremely worthwhile is Ryan Bates’ excellent screencast series RailsCasts – Ruby on Rails Screencasts and Michael Hartl’s excellent, comprehensive Rail’s Tutorial.

The level of sophistication and the technologies in the web-client seem light-years ahead of the MS camp. (Disclosure: For those aspects of an organization that are externally-facing, of course web-applications are a must. For those that are exclusively internal, I much prefer a desktop app over a web app every day of the week and twice on Tuesday).

I’ve been listening to several Ruby podcasts during my commute over the past couple of weeks and it’s become abundantly clear that there is so much I have to learn. In fact, when I listen to the 37 Signals podcast, it’s almost if they’re speaking a different language. The slight amount of intimidation I feel about that is far outweighed by the thrill of learning all of this new stuff. I figure if I wrote assembler every day for four years, I can pretty much do anything.

They say there’s no better way to learn something that to learn it like you have to teach it. My plan is to write a series of “micro books” anywhere from 20-50 pages in length. Each will be part of an overall series and I think the series is going to be entitled “Bare Bones – A Jump-start Guide To…”.

Some topics I really want to learn more about:

HTML5, CSS3, Ruby, Rails, git/git-hub, JavaScript/CoffeeScript, JQuery, JSON, BDD (RSpec, Cucumber)…etc.

I have to believe that others could benefit from them too. I hope to have the first one to share sometime in before the end of this year. I think I’ll start with HTML 5. Any requests?

Scratching Your Own Itch

There’s a saying in open-source software called “scratching your own itch” and it refers to building software that YOU need, not trying to figure out what the rest of the world wants.

That’s very wise advice.

I spent some time in that other frame of mind and it’s frustrating.

I plan on building a web app to manage the tax preparation business that I run on the side from my day job. I plan on building it with Ruby on Rails. Before I do that however, I really need to learn Ruby on Rails as a I am new to it.

So, I could dive right in, learning as I go, fearful of making a mistake on MY application, and I would spend more time thinking than I would coding.  Therefore, I am going to LEARN Rails first, and then I am going to PRACTICE Rails on stuff that doesn’t matter.

If you want to learn Ruby on Rails, there are NUMEROUS resources, many free and some available at very low cost, most of them EXCELLENT. I purchased a screen-cast series and book on PDF from RailsTutorials.  Michael Hartl’s training is top notch and covers not only Ruby as well as Rails, but through osmosis you’ll also learning about curl, git and TextMate.

As far as practicing goes, that’s tomorrow’s post because it’s worthy of a post all its own.

Thread.Sleep();

Learning Ruby

During the day I used .NET but I have recently become completely enamored with Ruby. I don’t know very much about it yet, but it’s syntax and dynamic typing are quite amazing for an old grizzled C#/Java type guy such as me.

 

Even though I most likely won’t use Ruby during my daytime job, just learning certain things about the Ruby community and some of their tools is already paying dividends in the form of discovering tools/methods available to the .NET community that I was not aware of before. In particular I am referring to the Behavior-Driven-Development tools such as NSpec, BDDify and SpecFlow, most of which were largely inspired by the corresponding Ruby tools, RSpec and Cucumber.

In some post soon, I will write about how to use some of these .NET BDD tools because there is precious little content available on how to use them.

 

Thread.Sleep();

 

 

Can’t Stop Coding

Hey there,

My name is Joe Meirow and this is the first post of “Can’t Stop Coding”.

I’ve been writing software for 28 years and I love it is much today as the day I started.

I’ve been learning some many new things lately that I just wanted to begin to capture what I am learning as I am learning it.

Thanks for dropping by  :)

Follow

Get every new post delivered to your Inbox.