Posted in Uncategorized

self.send

In Ruby the send method is a helper in passing a method and its arguments to the corresponding object. Its a way to DRY things up, but if you dont know what you are looking at, it can be really confusing.

class Report
  def basic
    “This is a basic report"
  end

  def complex
    “This is a complex report"
  end
end

@report = Report.new
@report.send(:basic)# same thing as @report.basic
>> “This is a basic report”

My initial confusion was in the context of a controller action ‘show’ —

  def show
    @sub_title = "Reports: " + @report.title
    self.send(@report.type)
    respond_to do |format|
      format.html
      format.xml
    end
  end

@report is instantiated in a before_filter based on what a user selected on the index page. In this particular case, self is the controller object, so you find the @report.type which is the report type attribute, is also a method within the controller.

Lets call type in the this case ‘basic’, so you would find in the controller(in my case) a method corresponding to it…although,I would likely move this to a controller helper to keep the controller skinny.

  def basic
    
  end

As you can imagine, this gives you alot of flexibity in building out templates and supplying the needed objects to each report.

Advertisements
Posted in Procs

Procs

Procs are new concept to me. As I’ve stated before, I work in large legacy app that started life as a Rails 2 app. I started with the group when they were at 3.2. Procs are used in complex processes within the application and a process that is breaking for some of our users…while traversing the code base getting myself familair with the workflow, I came across this bit of code:

merge_proc = proc do |key, old_value, new_value|

(old_value + new_value).uniq

end

To preface this code, there was nothing to instantiate any of the values within the proc so this led me down the path that the proc has deeper purpose than meets the eye. If I build this out in IRB, then a proc object is created.

If I look at how merge_proc is defined then it is a block that maintains local variables and how they will be processed. So it will take 2 arrays, combine them and then drop the duplicates in the above example.

Lets try another example:

h1, h2 = {},{}

h1[:object1] = %w(a1 a2 a3)

h2[:object1] = %w(a3 a4 a5)

h1.merge(h2)

=> {:object1=>[“a1”, “a2”, “a3”, “a4”, “a5” ]}

The standard merge method overwrites values in h1.

Lets change the way the proc is defined:

merge_proc = Proc.new{|key, old, new| old + new }

h1.merge(h2, &merge_proc)

=> {:object1=>[“a1”, “a2”, “a3”, “a3”, “a4”, “a5” ]}

That is pretty cool.

Posted in MacOS, Security

Adding Self Signed Certificates to Mac Keychain

So if you occassional run into situations where you want to test a website but you need to use self-signed certificates then you can simply import the certificate from Chrome into your keychain and stop getting those annoying ssl error messages.

Simply click on the lock icon with the x and then click the Details link. The Debug Console will open up with the security information. Simply click the ‘View certificate’ button. A drop down window will show the certificate with a little image of it. Drag and drop the image to your desktop.

Open your Keychain app and click on ‘File’ and then ‘Import Items…’

Find your certificate and select it for import. You will need to enter your admin password. After its in your list of System Certificates, double click on it and a new window will open up. Expand the Trust header and select ‘Always Trust’.

You will be asked to enter password again. After that, open a new browser window and you should now get the green lock and no more annoying ssl warnings:

Posted in Data, ElasticSearch, Kibana

Elasticsearch, Kibana, Logstash Oh My!

I don’t have access to our production logs. That stinks. We’ve been discussing internally on how to gain access, so with a little searching I found this wonderful gem called logstasher. It provides a way for our rails app to convert its logs to JSON to allow logstash to grab and send to elasticsearch then the Cou de Gras! —Kibana. Kibana provides a visual interface to view and query against elasticsearch.

Installation on my local was more involved since the installation instructions were not well documented. I had to figure it out between installing the gem and finally installing the above apps. The gem says that kibana comes with logstash but I couldn’t get that to work.

For MacOS–El Capitan:

$brew install elasticsearch

$brew install logstash

$brew install kibana

$vi ~/path/to/kibana.yml

uncomment the default line for elasticsearch url

In your rails app, add gem ‘logstasher’ to the Gemfile and bundle. Add the following to your development.rb file:

#Enable the logstasher logs for the current env

config.logstasher.enabled = true

Create a name_that_makes_sense.conf file in directory where you will maintain your logstasher configurations(you could add it to your app root) and add these lines:

input {

file {

type => “rails”

path => “/Users/username/app_name/log/development.log”

codec => json {

charset => “UTF-8”

}

}

}

output {

# Print each event to stdout.

stdout {

codec => rubydebug

}

elasticsearch {

hosts => [“localhost:9200”]

}

}

After you save that, start all the services if you havent already(you will need to restart kibana if you setup the yml file after you already started it):

$elasticsearch & #you can browse to localhost:9200 and see if you get a response

$kibana & #kibana has its on webserver with the latest release

$logstash -f path/to/name_that_makes_sense.conf

$rails s

Then open a browser at http://localhost:5601

The kibana application will not let you configure any indices until your logstash setup is sending data to elasticsearch, so the app needs to serve some request before it has data to search and query. It will not initialize and convert an existing log.

Have Fun!

Posted in Uncategorized

What the The Iron Yard did for me

It was February 2015 when I sat down and had coffee with John Saddington to talk about enrolling into a new coding bootcamp that was launching in Atlanta. I had heard about the IY being in North Carolina and wished that they would come to the Atlanta area. It was a wish I made buried under a workload of documents, procedures and regulations. I was in the medical device space and I was not fond of the work I was doing and hadnt been for about 6 years. I wanted a change and software seemed like something I could do. They were promising a ‘Zero to Hero’ sort of career launch into software development regardless of experience.

I met John about 5 years earlier for breakfest looking for advice on how to build an app. His advice? Build a blog within my expertise. He basically said I was too far into my career to transition into software development. John said I would likely find a developer to help me build the app I wanted if I would put my effort into building a presence. Intersting how viewpoints change over time. I suspect the advent of iOS and AWS changed that for alot of people.

I didn’t take Johns advice. I really wanted to build software. I don’t know where I would be if I had built that blog, but three kids later, I still wanted to get into software. However, family took all my time, and with what little energy I had left, online and unstructured courses didnt help me get there. From what I recall from my early years in college was that I learned better in groups.

The IY was a 12 week fulltime course and it was a big commitment, especially for a single income family like ours. The thought of even asking my wife, Tracey, about it was ludicrous, but as God would have it, I was unemployed in a lax hiring market within my field of expertise. The subject would eventually came up which lead to a series of steps, first one being to reach out to John. That meeting led us to take the biggest hail mary leap of our life.

This would likely not have happened under any other circumstances because major changes like this in the midst of a young family of 5 can be considered foolish if not crazy.

In hindsight, the decision wasn’t crazy, but the journey was. Hours were long, stress was high, concepts were missed and would need to be grasped on top of newer material getting crammed down my throat. It was a firehose bolted to my mouth.

After 3 months of training, and another month to recover and make my portfolio pretty, my job hunt was at full capacity. By August I was employed with a small start-up. It was a huge relief and the culmination of a dream. Life was good, until some family medical issues came to light.

Now I will say, that my best effort was not put forward, but we were under allot of stress. Two months went by and I was fired. I was an experiment that did not work out. I was devastated and so was my wife. We did not need this, but life has a way of working itself out if you dont give up.

At this point, I was open to going back into my old field. I was not crazy about it, but I was open to it. I had only two months of salary in the previous 10 months, and it was hurting. Needless to say, I needed income.

I did take on a contract gig that ended in failure and I’ll probably post about that another day, but my perseverence paid off and I landed a software engineering position in a most unlikely industry for a ROR position. Insurance.

To say that I love my work is an understatement. I cant believe I get paid to do this kind of work. I’m going into June 2016 at my current role and its like an extended version of the IY, except I’m getting paid!