Linkography

Lecture Notes

Summary

What is a Web Service

Pause to look at the big picture

Retrieving information

$ sudo gem install xml-simple
$ sudo gem sources -a http://gems.github.com 
$ sudo gem install jdpace-weatherman

require 'rubygems'
require 'weather_man'

WeatherMan.partner_id = '1191575384'
WeatherMan.license_key = 'c3df3b7f29fe164e'

# Search for a location
# Returns an array of WeatherMan objects
locations = WeatherMan.search('Arlington, MA')

# I happen to know the code for Arlington MA
arlington = WeatherMan.new('USMA0011')

# Fetch only current conditions in metric units
weather = arlington.fetch(:days => 0, :unit => 'm')

# Fetch a 3 day forecast only
weather = arlington.fetch(:days => 3, :current_conditions => false)

# Fetch the current conditions and 5 day forecast in 'standard' units
weather = arlington.fetch

# Print a weather report
puts "Current temperature is: #{weather.current_conditions.temperature}"
puts "But it feels like: #{weather.current_conditions.feels_like}"
puts "Wind speed is: #{weather.current_conditions.wind.speed}"
puts "And the direction is #{weather.current_conditions.wind.direction}"

 $ gem which weather_man
/opt/local/lib/ruby/gems/1.8/gems/jdpace-weatherman-0.1.2/lib/weather_man.rb
 $ mate /opt/local/lib/ruby/gems/1.8/gems/jdpace-weatherman-0.1.2/lib/weather_man.rb

RPC - Remote Procedure Calls

REST - A different way to think about RPC

 http://cookieserver.org/fortunes/1
 http://cookieserver.org/fortunes
 http://cookieserver.org/fortunes/random

Extending the API

   i = PhonebookService("localhost:3000")
   i.create_new_card(:name => "Pito Salas", :business => "6173886367", :home => "7816462540")

   i = PhonebookService("localhost:3000")
   i.api_key = "F123FD123"
   i.create_new_card(:name => "Pito Salas", :business => "6173886367", :home => "7816462540")
 

$ sudo gem install httparty
$ sudo gem install crack

$ httparty -f xml http://localhost:3000/cards/1.xml

require 'rubygems'
require 'httparty'

# trivial front end to be able to send a rest URL to the server. Much is hardwired.
# Note: I had  a problem getting a PUT to work, so I added a special action. Debugging required.

class CardUtil
  include HTTParty
  base_uri 'localhost:3000'
  
  def update_card(cardnum, text)
    options = {:query => {:card => {:name => text}} }
    self.class.get("/cards/#{cardnum}/store.xml", options)
  end
end

cu = CardUtil.new
cu.update_card(ARGV[0], ARGV[1])

def store
    begin
      @card = Card.find(params[:id])
    rescue
      @card = Card.new
    end
    respond_to do |format|
      if @card.update_attributes(params[:card])
        flash[:notice] = 'Card was successfully updated.'
        format.html { redirect_to(@card) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @card.errors, :status => :unprocessable_entity }
      end
    end
  end

$ ruby addcard.rb 3 "john"

Processing CardsController#store to xml (for 127.0.0.1 at 2010-06-15 20:34:14) [GET]
  Parameters: {"id"=>"3", "card"=>{"name"=>"john"}}
  Card Load (0.4ms)   SELECT * FROM "cards" WHERE ("cards"."id" = 3) 
  Card Update (0.7ms)   UPDATE "cards" SET "updated_at" = '2010-06-16 00:34:14', "name" = 'john' WHERE "id" = 3
Completed in 36ms (View: 1, DB: 1) | 200 OK [http://localhost/cards/3/store.xml?card[name]=john]

~/brandeis-cosci/restlecture/assoc (master) $ script/console
Loading development environment (Rails 2.3.5)
installed irb goodies from ~/.irbrc
>> Card.all
  Card Load (1.4ms)   SELECT * FROM "cards" 
+----+------------+------------+--------------+-------------------------+------------------+
| id | name       | home_phone | office_phone | created_at              | updated_at              |
+----+------------+------------+--------------+-------------------------+------------------+
| 1  | hello      |            |              | 2010-06-14 19:45:50 UTC | 2010-06-16 00:17:26 UTC |
| 2  | pito salas |            |              | 2010-06-14 19:51:12 UTC | 2010-06-16 00:23:37 UTC |
| 3  | john       |            |              | 2010-06-16 00:27:11 UTC | 2010-06-16 00:34:14 UTC |
+----+------------+------------+--------------+-------------------------+------------------+
3 rows in set
>> 

Cloud Computing

None: RestWebServ (last edited 2010-06-21 00:37:21 by PitoSalas)