Ruby NET::HTTP requests library cheatsheet

Nice cheat sheet resource of Ruby HTTP requests is published on rubyinside.com. Since the comments are disabled I decided to add one note here:

When using

uri = URI.parse("http://google.com/")

it is good practice to encode the url with

URI.encode("http://google.com", "[]")

The second argument is optional and could be regex or string. In case of string the symbols are escaped. Check the docs.

Tagged with: ,
Posted in Ruby

Tools for cleaning your CSS files

Few useful tools for analyzing unused CSS and cleaning it. I will sure forget later, so I am adding the links in order to have them:

https://github.com/zmoazeni/csscss

https://github.com/geuis/helium-css

http://www.cleancss.com/

http://www.makeuseof.com/dir/csssuperscrub-make-css-cleaner/

Tagged with: ,
Posted in CSS

Upgrade Postgres minor version on Ubuntu

I am not a DBA, but recently I have to upgrade Postrges minor version with a security update. This is not either a tutorial or some best practice, but some small snippet of the command you will need, before doing the upgrade. Always check the documentation, command arguments and be sure that a previously made backup will restore the database. If you are not experienced with Linux, make sure that you have easy way to restore the system too.

First check the current version of postgres. I used

apt-show-versions

which is not installed by default on Ubuntu 12.04. You can add it with

sudo apt-get install apt-show-versions

Then run

sudo apt-show-versions | grep postgresql

to see the actual version of the package and if there is newer binary. make sure you execute

sudo apt-get update

to ensure you have an updated packages list.

Upgrading from Postgres 9.2.3 to 9.2.4 took about 10 minutes. The process stops and starts the DB daemon  automatically.

Tagged with: ,
Posted in Databases, Postgres

Use Railroady to generate UML diagrams for your project’s controllers and models

Introduction
Railroady is a Railroad fork, a Ruby On Rails diagram generator for models and controllers. If you are using Rails 2.3.x with Ruby 1.8.7 checkout Railroad gem. Otherwise use Railroady with Rails 3.x.

Both gems are using graphviz open source tools for painting diagrams and convert them in PNG or SVG format. There is one more useful open source tool for editing svg formats Inkscape (open source illustrator).

Some snippets
I tried Railroady and it is pretty straightforward to use it. There are two options to add it – either in your Gemfile

group :development do
  gem 'railroady'
end

or install it as separate gem with

gem install railroady

Adding it as a gem has one plus – it adds rake tasks to your project, which is easier to use.

Here are some snippets to generate models and controllers UML diagrams in dot format

railroady -o models.dot -M
railroady -o controllers.dot -C

Converting the dot files to PNG

dot -Tpng models.dot > models.png
neato -Tpng controllers.dot > controllers.png

Or in svg files format

dot -Tsvg models.dot > models.svg
neato -Tscg controllers.dot > controllers.svg

Note the graphviz tools bin directory should be in your environment path. Check the repo for more examples and additional documentation.

Tagged with: , ,
Posted in Ruby On Rails

Connecting Matlab to Postgres database

Assumptions
My primary focus is not on Matlab, but I used it for a while in the Uni. I have to support connecting Matlab installed on Windows machine with Postgres 9.2 database. I used the Matlab command window, so this have to be straightforward to do on you environment, assuming the database driver do not differ much in other environments.

Connection configuration
Connecting Matlab to Postgres requires to have JDBC driver, which you can download from here. Select current version or choose one appropriate for you OS from the Supported versions table. Put the jar file somewhere in your drive. I put mine at “C:\Program Files\MATLAB\R2011b\java\jar\postgresql-9.2-1002.jdbc4.jar” (1). Open Matlab command window and type

edit classpath.txt

Add the full path (quoted above) at the end of the file. In my case I added “$matlabroot/java/jar/postgresql-9.2-1002.jdbc4.jar” (2). Then close and start Matlab and verify the driver is loaded with typing in command window

javaclasspath

You should see the added driver path (1) above. Matlab supports connecting with and without SSL. Here are example strings to use in either cases:

JDBC Driver: org.postgresql.Driver
Database URL:jdbc:postgresql://localhost:5432/database_name

JDBC Driver: org.postgresql.Driver
Database URL: jdbc:postgresql:servername:database_name:ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory&

Querying connection
Connection string should look like

conn = database(database_name, database_user, user_password, 'Vendor', 'PostGreSQL');

Here is example to query “database_table” and show the data for the first row:

conn = database('database_name', 'database_user', 'user_password', 'org.postgresql.Driver', 'jdbc:postgresql://123.123.123:5432/database_name')
curs = exec(conn, ['SELECT * FROM "database_table";']);
row = fetch(curs, 1);
row.Data

Used arguments are self explaining and should be straightforward to follow.

Tagged with: ,
Posted in Postgres

cannot load such file — mkmf

Installing bcrypt-ruby give this error:

Installing bcrypt-ruby (3.0.1) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /usr/bin/ruby1.9.1 extconf.rb 
/usr/local/lib/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- mkmf (LoadError)
    from /usr/local/lib/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:45:in `require'
    from extconf.rb:36:in `<main>'

This has a dependency with the dev package, which one can install with

sudo gem install ruby1.9.1-dev

Here is a list of all packages needed when installing rvm.

sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

 

Tagged with: , ,
Posted in Ruby On Rails

Invalid date format in gemspecs

Spotted several lines with invalid date format when installing a gem

invalid date format in specification: "2011-08-25 00:00:00.000000000Z"

While one can go through the gems specs and fix the format, one can just install rubygems-update

sudo gem install rubygems-update

Then run that command and should be good to go.

sudo update_rubygems
Tagged with: , ,
Posted in Ruby On Rails

Error installing json and Failed to build gem native extension – Windows specific

After updating Windows bundle install and gem install json gives the following error

ERROR: Error installing json.
ERROR: Failed to build gem native extension.

Found solution here. In my case it happen after Windows update and “Scenario B” worked for me.

Tagged with: ,
Posted in Ruby On Rails

Convert column to geometry in Postgres

I noticed a column in Postgres which is varchar, but has geo information and should be geometry. Here are short steps to do in order to convert the data in geometry instead of varchar.

1. Rename the column to the_geom_1

2. Then add geometry column

SELECT AddGeometryColumn('table_name', 'the_geom', 4326, 'POINT', 2)

3. Move the data from the varchar column to the geometry column

UPDATE table_name SET the_geom = 'SRID=4326;' || ST_AsText(ST_GeomFromEWKT(the_geom_1))
Tagged with: ,
Posted in Postgres

Sync Total Commander pane navigation

A quick shortcut command for synchronizing Total Commander panes navigation. Copy / paste it in the command line to turn on / off.

cm_SyncChangeDir
Tagged with: ,
Posted in Articles
Sites
Categories
Archives