Sunday, October 19, 2014

Copy to Clipboard in Linux

1. Install xclip : sudo apt-get install xclip
2. Copy test.txt to clipboard : cat test.txt | xclip
3. Acess the text from the clipboard: xclip -o

Reference:

1. Copy Output of a Command to Clipboard

Wednesday, September 24, 2014

Changing PDF Meta Data in Ruby

According to the home page of pdf-toolkit gem :

pdf-toolkit allows you to access pdf metadata in read-write in a very simple way, through the pdftk commandline tool.

1. Install pdftk version 2.02

$ pdftk -version

pdftk 2.02 a Handy Tool for Manipulating PDF Documents
Copyright (c) 2003-13 Steward and Lee, LLC - Please Visit: www.pdftk.com
This is free software; see the source code for copying conditions. There is
NO warranty, not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2. gem install pdf-toolkit

3. Read or Write pdf meta data.

require 'pdf/toolkit'

pdf = PDF::Toolkit.open('tmux_p3_0.pdf')
#puts pdf.public_methods(false).sort

puts pdf.author
pdf.author='Daffy Duck'
if pdf.save
  'PDF meta-data saved successfully'
else
  'Failed to save pdf meta-data'
end
# save! to raise exception on failure

puts "after change"
puts "Author"
puts pdf.author
puts 'Title'
puts pdf.title
puts 'Subject'
puts pdf.subject
puts 'Page count'
puts pdf.page_count
puts 'Creator (Publisher)'
puts pdf.creator
puts 'Created At'
puts pdf.created_at
puts 'keywords'
puts pdf.keywords
puts 'producer'
puts pdf.producer
puts 'version'
puts pdf.version
puts 'Updated at'
puts pdf.updated_at

4. Verify the meta-data in Preview -> Tools -> Show Inspector. You will see the author, title, subject, producer, creator, creation date, updated date, number of pages etc.

5. Read the tests in the pdf-toolkit source code to see the available methods.

Tuesday, September 23, 2014

Fast Typing, IDE shortcuts, Fast Coding

It does not matter how fast you type in the keyboard, know every short-cut in the IDE, never use the mouse or how fast you can code. Remember : It's garbage in, garbage out.

The quality of software depends on:

1. Quality of your thinking.
2. Learning from your mistakes. Did you learn from your own mistakes?
3. Are you making new mistakes or oblivious and making the same old mistakes over and over again?
4. Are you learning from the mistakes of others to minimize making mistakes in the first place?
5. Are you learning to master the best practices?
6. Are you reading books by : Martin Fowler, Alaister Coburn, Eric Evans, Robert Martin etc? Are you applying it to your projects?



Friday, September 19, 2014

wrong number of parameters 2 for in stylesheet_link_tag rails

This error occurs with an ambiguous error message. It does not show the line number of the source where the cause of the problem resides. This happened due to sass-rails dependency on sass gem. Since the sass gem was not declared in the Gemfile, the sass-rails upgraded sass gem to 3.4.4 which broke the application layout. By locking the sass gem to a lower version in the Gemfile:

gem sass, "~ 3.2.1"

The sass-rails picked up this version that works.

Monday, September 01, 2014

The Scientific Method of Troubleshooting

Notes from the presentation by Blithe Rocher.

Set of Techniques for Acquiring Knowledge.
Methodical
Systematic

1. Define the Problem
Expected behavior
Actual behavior
Criteria for success

2. Do Your Research
Know your environment
Read the literature
Discussions
Make it fail

3. Establish a Hypothesis
4. Design the Experiment
Divide and conquer
Limit the variables
Try something weird
Hierarchy of Blame

4. Gather Data
Current status
Read the error message

5. Analyze Your Results
 Problem Solved?
Learn anything?
Understand the Why
Future Experiments
Embrace the Success

6. Keep a Good Lab Notebook
You won't remember
Logs aren't enough
Commit messages
Update the docs
Contribute
Blog it
Share the knowledge



Friday, August 22, 2014

How to play with ActiveModel validators in the rails console

1. bundle open activemodel
2. Open the validator.rb file.
3. You can see the comments that shows you how to mixin the validators. Once you mixin, you can add any validator and define a dummy method for the attribute to play with it in the irb:

class Person
  include ActiveModel::Validations
  SSN_REGEX = /your ssn regex goes here/
  validates_format_of :ssn, with: SSN_REGEX
 
 
  def ssn   
    111-11-1111
  end
end

4. Change the ssn method to invalid ssn to test your regex.
 

Thursday, August 21, 2014

How to install Exception Notification gem in Rails 4.1

1. Add the gem to Gemfile:

gem 'exception_notification'



bundle install

2. In config/environments/production.rb:

Whatever::Application.config.middleware.use ExceptionNotification::Rack,
  :email => {
    :email_prefix => "[Whatever] ",
    :sender_address => %{"notifier" },
    :exception_recipients => %w{exceptions@example.com}
  }

3. Configure ActionMailer, in config/environments/production.rb:

I am using Sendgrid SMTP API so:

config.action_mailer.delivery_method = :smtp

If you have sendmail installed on the production server:

config.action_mailer.delivery_method = :sendmail

4. In production.rb:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

How to install pdftk on Mac OS

You can download the installer from https://www.pdflabs.com/tools/pdftk-server/

After installation, open a new terminal and check installation:

$ pdftk --version

pdftk 2.02 a Handy Tool for Manipulating PDF Documents
Copyright (c) 2003-13 Steward and Lee, LLC - Please Visit: www.pdftk.com
This is free software; see the source code for copying conditions. There is
NO warranty, not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$