Ejemplo de un servicio RESTfull en Sinatra

Donde

  1. Uno! Use Sinatra to Implement a REST API. by Dan Schaefer. Published March 10, 2014
  2. Código en GitHub https://github.com/crguezl/sinatra-rest-api-example
  3. [~/sinatra/sinatra-rest-api-example(master)]$ pwd -P
    /Users/casiano/local/src/ruby/sinatra/sinatra-rest-api-example
    

Ejecución del servidor

[~/sinatra/sinatra-rest-api-example(master)]$ ruby uno-server.rb 
[2014-09-13 15:41:24] INFO  WEBrick 1.3.1
[2014-09-13 15:41:24] INFO  ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
== Sinatra/1.4.5 has taken the stage on 4567 for production with backup from WEBrick
[2014-09-13 15:41:24] INFO  WEBrick::HTTPServer#start: pid=94715 port=4567

Ejecución del cliente

[~/sinatra/sinatra-rest-api-example(master)]$ pry
[1] pry(main)> require './uno-client'
=> true
[2] pry(main)> bob = UnoClient.new 'bob'
=> #<UnoClient:0x007fbde4459468 @name="bob">
[3] pry(main)> carol = UnoClient.new 'carol'
=> #<UnoClient:0x007fbde43f1de0 @name="carol">
[4] pry(main)> ted = UnoClient.new 'ted'
=> #<UnoClient:0x007fbde438aa50 @name="ted">
[5] pry(main)> alice = UnoClient.new 'alice'
=> #<UnoClient:0x007fbde4323b48 @name="alice">
[6] pry(main)> ralph = UnoClient.new 'ralph'
=> #<UnoClient:0x007fbde42c0250 @name="ralph">
[7] pry(main)> bob.join_game
{:status=>"welcome"}
=> nil
[8] pry(main)> carol.join_game
{:status=>"welcome"}
=> nil
[9] pry(main)> ted.join_game
{:status=>"welcome"}
=> nil
[10] pry(main)> alice.join_game
{:status=>"welcome"}
=> nil
[11] pry(main)> ralph.join_game
{:status=>"sorry - game not accepting new players"}
=> nil
[12] pry(main)> bob.deal
{"status":"success"}
=> nil
[13] pry(main)> bob.get_cards
{"cards":["6-heart","jack-diamond","7-spade","queen-club","jack-spade"]}
=> nil
[14] pry(main)> carol.get_cards
{"cards":["9-heart","4-spade","10-spade","9-spade","10-club"]}
=> nil
[15] pry(main)> ted.get_cards
{"cards":["6-spade","ace-diamond","ace-club","3-heart","2-heart"]}
=> nil
[16] pry(main)> alice.get_cards
{"cards":["9-diamond","8-spade","8-club","8-heart","jack-club"]}
=> nil
[17] pry(main)>



Subsecciones

Casiano Rodriguez León 2015-06-18