JSON & XML parsing with Sinatra, RestClient & HTTParty

December 9, 2008

These days I was playing with Sinatra, RestClient and HTTParty microframeworks for Ruby and from what I know, there is no automatic parsing for RestClient like in HTTParty, so I tried the following test by using all three of them:

Sinatra quick test:

require 'rubygems'
require 'sinatra'

get "/my/get" do
  '{"response":{"error":false,"message":"Back to you from get"}}'
end
post "/my/post" do
  '<response><error>false</error><message>Back to you from post</message></response>'
end

RestClient & HTTParty quick test:

(for httparty > 0.4.2)

require 'rubygems'
require 'rest_client'
require 'httparty'

include HTTParty

string_response = RestClient.get("http://localhost:4567/my/get")
response = Crack::JSON.parse(string_response)
p response

string_response = RestClient.post ("http://localhost:4567/my/post", {})
response = Crack::XML.parse(string_response)
p response

… so, we have the same objects from both JSON and XML responsess

Ubuntu 8.10 libxml-ruby install

November 17, 2008

Two steps are needed:

1. sudo apt-get install libxml2-dev
2. sudo gem install libxml-ruby

Without running first step, something like that could come up:

Building native extensions.  This could take a while…
ERROR:  Error installing libxml-ruby:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb install libxml-ruby
checking for socket() in -lsocket… no
checking for gethostbyname() in -lnsl… yes
checking for atan() in -lm… no
checking for atan() in -lm… yes
checking for inflate() in -lz… no
checking for inflate() in -lzlib… no
checking for inflate() in -lzlib1… no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Thant should be all.

Ruby private vs. protected visibility

October 22, 2008

Very interesting notification about private / protected visibility in Ruby.

Folosind Mac OS X ca mediu de dezvoltare software

August 31, 2008

Da, sunt un dezvoltator de programe software (asta e “definitia” cea mai buna).

Cand fac acest lucru in afara serviciului, se intampla pe un MacBook clasic. Bineinteles, acesta are ca OS, Mac OS X Leopard. Un sinstem de operare orientat end user.

Limbaje

Limbajele principale, la care imi place sa cred ca ma pricep, sunt Java si Ruby (de altfel imi plac mult pentru application development). In primul rand as vrea sa va spun ca OS-ul vine dotat cu java development kit 1.5.0_13, care se poate updata la 1.6.0_05 (64bit) folosind mecanismul de “Software update …”.

Versiunea de ruby cu care sistemul de operare soseste, este 1.8.6 (destul de ok pentru momentul de fata).

Desigur, daca se doreste mai mult (si puteti fi sigur ca asa se va intampla) puteti folosi Macports pentru instalarea de suporturi pentru noi limbaje (Erlang, Lisp, etc.).

De asemenea, Perl/Python vin instalate ca limbaje default in os x (da, ceva destul de special daca faci o comparatie cu MS Windows).

Version control system

SVN – din macports

Mercurial – din macports

CVS – din macports

Git – din macports

Build systems

Maven/Maven2 (2.0.6) – din macports

Apache Ant (1.7.1) – din macports

C/C++

Xcode, descarcabil si instalabil de la Apple de pe site (include un IDE destul de interesant precum si support pentru Ruby/Python & Cocoa).

Editor/IDE

Eclipse functioneaza mai mult decat decent (la fel ca si orice alt IDE bazat pe java).

Vim – din macports

Emacs – din macports

Ftp

lftp – din macports

cyberduck – GUI ftp client

Ruby/Rails

rails – gem install

passenger – gem install

passenger – prefpane support

SGDB

mysql – din macports sau mysql web site

postgresql – din macports

….

Un singur motiv as avea contra java pe Mac OS X (desi acesta nu tine in mod direct de development): Azureus se pare ca nu prea vrea sa porneasca folosing Java 1.6 (se inlocuieste cu Transmission fara nicio problema).

In speranta ca voi mai gasi mai multe motive pro folosire Mac OS X pentru dezvoltare software, voi reveni.

Muista ?!

August 5, 2008

Credits go to Daniel:

Mac OS X 10.5 install

March 18, 2008

Yesterday I made my first osx install. I wanted a “linux way” to do that: Users to be on a separate partition (slice/volume) than root. It seems that even if partitioning is possible, selecting mount points is not…

The installation is VERY easy (3 or 4 mouse clicks are needed). After 2 hrs the job is completed and you’re ready to do whatever you want (it comes by default with: vim, ruby and rails, although is not quite complete, still sudo gem install rails need to be done since the version is 1.2.6)

Mac OS X 10.5 gem install mysql

March 16, 2008

Am tot dat de niste probleme legate de instalarea gem-ului de mysql pe mac os x.

Am tot cautat pe net cam ce si cum trebuie facut si asta e ce am gasit:

sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-dir=/usr/local/mysql

yey… I got a mac

March 15, 2008

De ceva vreme doresc sa imi cumpar un macbook si ieri tocmai s-a intamplat.

(vroiam un macbook black, dar nu am avut parte decat de cel white)

Dar de cand am decis sa imi iau jucaria, ceva interesant s-a petrecut: imediat cum am sunat la reseller, am aflat ca aceste macuri sunt la reducere (da, reducere cam de 20 mil each, de la 57 mil, la 39 mil). Alta chestie placuta, e ca atunci cand sa il platesc, s-au oferit sa imi faca upgrade free de la 1G ram, la 2G ram (in conditii normale m-ar fi costat in jur de 60 euro upgrade-ul).

Voi reveni si cu ceva poze ale jucariei :D

RoR FrontController

March 10, 2008

Zilele trecute am dat de o situaţie în care am fost nevoit să folosesc un front controller.
Cam aşa am facut (multumiri autorului postului original):

# Să presupunem ca avem un controller numit Admin.
class AdminController < ApplicationController
def process(request, response, method = :perform_action, *arguments)
super(request, response, :index)
end
def index
@req_host = request.env["HTTP_HOST"]
@req_url = request.env["PATH_INFO"]
find_controller.constantize.new.process(request, response)
rescue NameError
# Actiunea default care va fi folosită în caz ca nu există controller-ul specificat
render :action => ‘index’
end def find_controller
controller = “”
action = “”
# URL-ul aşteptat va avea forma de mai jos (ex: /admin/mycontroller/myaction )
if @req_url =~ /^\/(\w*\_*\w*)\/(\w*\_*\w*)\/*(\w*\_*\w*)$/
controller = $2.to_s.downcase
action = $3.to_s.downcase
end
if action.empty? && !controller.empty?
action = ‘index’
end
params[:action] = action
case controller
when “admin_workshoptypes”
# Numele clasei controller-ului va fi returnată în caz că acesta a fost găsit
return “AdminWorkshoptypesController”
else
# Dacă niciun controller nu a fost găsit, numele unui controller inexistent va fi returnat
return “noController”
end
end

end

Dacă vom folosi: http://localhost:3000/admin/mycontroller/myaction,
se va trece prin controller-ul de mai sus, apoi o redirectare va fi făcută către controller-ul “mycontroller” şi către acţiunea “myaction” aferentă acestuia.
Spor la treaba.

today…java stuff

February 29, 2008
try{
// Some code here
}catch(Exception e){}

Is there a stupidity scale to measure something like that ?

OK, I understand that maybe the person that wrote that piece of code is a beginner, but AFAIK this is a must have for a Java developer, even for a beginner.

How stupid can be someone in order to do something like that?

Please, please, pleaseeeeee… log the fucking exception … it’s that so hard ?

Place a freaking note somewhere (if will not help you, it doesn’t mean that will not help others).

Advice for persons that are hiring this kind of developers: DO NOT HIRE THEM !!! He/She’ll make other developer’s life a living hell….

PS: don’t place a possible NPE into a try..catch block. Check for the fucking possible null.


Follow

Get every new post delivered to your Inbox.