Archive for the ‘Ruby/Rails’ Category

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.

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.

easy ?

February 27, 2008

using ruby 1.9, in order to get a character from a string using a specific position, you’ll do:

irb(main):001:0> "abc"[1]
=> "b"

but in ruby 1.8:

irb(main):001:0> "abc"[1..1]
=> "b"

There is an easier way, for 1.8, to do that?

to whom may concern…

February 26, 2008

New ruby book out there (also covers 1.9 version)


Follow

Get every new post delivered to your Inbox.