INCLUDE_DATA

 
»
S
I
D
E
B
A
R
«
aquisto cialis levitra svizzera tadalafil bestellen viagra prix acheter du viagra acquista levitra cialis sur internet citrate de sildenafil compro viagra levitra ricetta kamagra pharmaceuticals cialis vente libre cialis suisse levitra italia vente de cialis viagra suisse kamagra gel viagra quanto costa cialis 20 mg prix cialis 10mg viagra verkauf cialis moins cher viagra prezzo levitra kopen acheter tadalafil acheter kamagra tadalafil generico impuissance sexuelle cialis preco levitra donna viagra 100 mg comprar cialis generico comprar levitra cialis a vendre procurer du cialis generische cialis vardenafil generico vardenafil generika levitra sur internet generische viagra acheter cialis generique acquista viagra achat cialis viagra 100 mg cialis generico kamagra en france viagra ordonnance acheter viagra achat vardenafil pastilla levitra viagra cialis differenze impotenza sessuale venta viagra medicament cialis curare impotenza kamagra te koop achat cialis 20mg levitra pharmacie cialis receta acquisto viagra net cialis en ligne achat de viagra cialis generique acheter acheter du cialis cialis 20 mg vardenafil 10 mg viagra alternativo citrate de sildenafil cialis sin receta viagra kopen acheter cialis en pharmacie kamagra bestellen comprar viagra pela internet viagra prescrizione levitra donne vente cialis venta de sildenafil achete levitra acheter cialis france venta de levitra viagra kosten cialis marche pas comprar vardenafil disfunzione erettile rimedi vardenafil generique viagra recensioni cialis generico prezzo viagra versand cialis europe viagra venta libre impotenza rimedi cialis rezeptfrei acheter kamagra france levitra ohne rezept acquisto viagra in contrassegno prix de cialis cialis prescrizione viagra acquisto online achat viagra pildoras cialis kamagra generique cialis prezzo cialis inde cialis sur le net acheter cialis en espagne levitra ordonnance viagra naturel cialis 10 mg acquistare levitra procurer du viagra acquisto viagra senza ricetta viagra controindicazioni levitra 20 mg compra viagra impuissance erection acheter cialis pharmacie prezzi levitra viagra ohne rezept kamagra apcalis comprar viagra commander du cialis cialis ricetta medica sildenafil 50 mg sildenafil venta viagra italia pilule levitra sildenafil generico viagra prijs
Creating a Ruby Web Service Client from a WSDL
Feb 5th, 2009 by Jason

Remember the Java JAX-WS Web Service discussed what seems like long ago? Currently there is an have an instance of it running on this hosting server (got to love SSH). Experimenting with Ruby has led me to find the simplicity of creating a web service client. The duck typing of the language allows you to not need to generate a bunch of classes to pull in a WSDL (although it is possible using wsdl2r).

require 'soap/wsdlDriver'

URL = 'http://jstaten.com:7070/MathExample/MathService?wsdl'

begin
   driver = SOAP::WSDLDriverFactory.new(URL).create_rpc_driver
   p driver.addInts({:arg0 =>2, :arg1 => 4}).return
rescue => err
   p err.message
end

Steping through this code, you’re simply requiring the soap.wsdlDriver library and setting the URL of the WSDL to be read in. The begin statement is for error handling incase something goes wrong. Inside that, you create a new rpc driver using the WSDLDriverFactory. The “driver” within this code is the instance which contains methods pointing to the web service’s exposed operations. After creating the driver, we call a method on it. Because it requires two parameters (the two integers to be added), we pass it a map. The return field from running that method contains the value of the response. Finally, we rescue by printing the error message. If you’d like to test this against the web service in the code, feel free. Just please don’t abuse it.

Access MySQL Database using Ruby DBI
Jan 10th, 2009 by Jason

Back with more Ruby, I bring you some database connectivity because that’s always an important thing to know about a language.

Prerequisites

This tutorial requires that you have the mysql and dbi gems installed. If you have RubyGems installed, it should be as simple as

#gem install mysql
#gem install dbi

However, if you’re having difficulty, surely Google understands.

Also, this requires a MySql database running on your local machine with a database containing a customer table as follows

CREATE TABLE `customer` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(30) NOT NULL,
`telephone` char(10) default NULL,
PRIMARY KEY  (`id`),
UNIQUE KEY `telephone` (`telephone`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

Accessing the Database

require 'rubygems'
gem 'dbi'
require 'dbi'
begin
con = DBI.connect("DBI:Mysql:DATABASE_NAME:localhost","USER","PASSWORD")
result = con.execute "SELECT * FROM customer"
result.fetch_hash do |row|
printf "ID:%d NAME:%s PHONE:%s\n", row["id"], row["name"], row["telephone"]
end
result.finish
rescue DBI::DatabaseError => e
puts "Error, #{e.err}, #{e.errstr}"
ensure
con.disconnect if con
end

Breaking this down into some blocks, the program starts by requiring rubygems and dbi (gem ‘dbi’ allows the dbi libraries to be accessed). DBI is ruby’s interface to use several different databases. In this case we’ll be using MySQL.

The real work begins when we use DBI.connect to create a connection to a database. This connection is what we’ll use to send queries to the database. Be sure to replace DATABASE_NAME, USER, and PASSWORD with the appropriate information for your own database.

Next up, con.execute will return a result set that can be iterated over using fetch_hash. In this case we’ll simply print each result to the console. Notice that rows are hashes that allow you to access information based on column name. Finally, result.finish means we’re done using the result set given to us.

The rescue section simply makes sure to notify of an error if something goes wrong. Also, ensure does just what it says, ensures that the connection is closed if it exists.

Going Further

Inserting Data

Surely the output right now is rather boring because you don’t have any data in your table. Lets put some in, and lets do it with a prepared statement. That way when you build a Ruby app with user input, you’re less susceptible to SQL injection.

cmd = con.prepare "INSERT INTO customer(name,telephone) VALUES(?,?)"
cmd.execute "Bobby","2229998888"

Receiving Entire Datasets and Single Results

Remember working with result.fetch_hash? What about if you want the entire dataset? What about just the first row? You’re in luck, Ruby makes it easy.

#returns an array of DBI::Row objects
result.select_all "SELECT * FROM customer"

#returns the first row
result.select_one "SELECT * FROM customer"

Database access with Ruby is an important skill to know if you’re dealing with storing any sort of information. With this brief overview, grasping the idea should be straight forward.

Creating a Socket Server and Client in Ruby
Jan 8th, 2009 by Jason

Over break, I’ve been experimenting with Ruby a little bit. It’s rather different from the C#/Java syntaxes I’ve leared through schooling. However, I’ve enjoyed using it. And for the reference of myself and others, I’m slapping up a how-to for creating a simple socket server and client.

The Server

require "socket"
serv = TCPServer.new('localhost',7885)
count = 0
loop do
Thread.start(serv.accept) do |s|
count += 1
s.write "You are visitor #{count} to my TCP Ruby Server"
s.close
puts "New visitor: #{count}"
end
end

The server begins by getting the socket library that already comes with Ruby. It then creates an instance of TCPServer, binding it to localhost on port 7885. Count is used to keep track of the number of incoming connections the server receives.

Enter the loop, a new thread will be started upon the blocking method serv.accept and during the life of the thread, it will increment the count, send a message to the visitor, close the connection, and finally write the user count to the console.

The Client


require "socket"
client = TCPSocket.new("localhost",7885)
str = ""
while (add = client.recv(100)) != ""
str += add
end
puts str
client.close

The client simply receives all output sent to it.  It opens a connection with localhost on port 7885. After connecting, it recieves a buffer of 100 bytes until no more are read. The buffer is appended to the final output string, and the connection is closed.

And there you have it, a simple Ruby socket connection. From here you can create such things as a web server, or writing your own protocol. Enjoy!

»  Substance: WordPress   »  Style: Ahren Ahimsa Blog Flux Local - Utah
© Jason Staten 2009