Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Fake my Party
      Tanja Otto

     24.11.2009
  Düsseldorf on Rails
SalesLentz::DevTeam
Über mich
• internes Entwicklerteam von Sales-Lentz
• IBEs für Reisen, Bustickets, Eventtickets
• seit 2006 entwickeln wir...
HTTParty

• einfaches senden von HTTP-Anfragen
• JSON und XML werden automatisch in
  Ruby-Hashes umgewandelt
• http://git...
HTTParty

XML

         HTTParty   HASH

JSON
Installation


sudo gem install httparty
Ein erstes Beispiel
require "rubygems"
require "httparty"

response = HTTParty.get('http://twitter.com/statuses/public_tim...
Beispiel Twitter-API
class Twitter
  include HTTParty
  base_uri 'twitter.com'

  def initialize(u, p)
    @auth = {:usern...
weitere Beispiele

• http://github.com/jnunemaker/httparty/tree/
  master/examples
FakeWeb
• Helper, um HTTP-Anfragen zu stubben
• Testumgebungen von live-Anfragen
  entkoppeln
• Tests sind auch lauffähig,...
Installation


sudo gem install fakeweb
Mit einfacher
     Zeichenkette antworten
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.register_uri(:g...
Einer Antwort einen
             Status hinzufügen
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.regist...
Auf jede HTTP
            Methode antworten
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.register_uri(...
Wiederholende Anfragen
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.register_uri(:get, "http://twitter...
Wiederholende Anfragen
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.register_uri(:get, "http://twitter...
HTTP Authentifizierung
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.register_uri(:get, "http://example....
Alle registrierten URIs
               löschen
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.register_u...
Alle live Anfragen
                   blockieren
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.allow_ne...
automatisches einlesen
            einer Datei
<?xml version="1.0" encoding="UTF-8"?>
<countries>
  <country>Belgien</coun...
HTTP response
             headers definieren
require "rubygems"
require "fakeweb"
require "httparty"

FakeWeb.register_uri...
Upcoming SlideShare
Loading in …5
×

Fake My Party

2,194 views

Published on

Verwendung von HTTParty und Fakeweb

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Fake My Party

  1. 1. Fake my Party Tanja Otto 24.11.2009 Düsseldorf on Rails
  2. 2. SalesLentz::DevTeam
  3. 3. Über mich • internes Entwicklerteam von Sales-Lentz • IBEs für Reisen, Bustickets, Eventtickets • seit 2006 entwickeln wir mit Ruby on Rails • Mit Hussein Morsy Buch Ruby on Rails 2 Galileo Press http://www.railsbuch.de http://twitter.com/ajnato http://devteam.sales-lentz.lu
  4. 4. HTTParty • einfaches senden von HTTP-Anfragen • JSON und XML werden automatisch in Ruby-Hashes umgewandelt • http://github.com/jnunemaker/httparty
  5. 5. HTTParty XML HTTParty HASH JSON
  6. 6. Installation sudo gem install httparty
  7. 7. Ein erstes Beispiel require "rubygems" require "httparty" response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') # puts response.body # puts response.code # => 200 # puts response.message # => OK # puts response.headers.inspect response.each do |item| puts item['user']['screen_name'] end # => alexmedeiros59 # => EmySayers # => ciquuuee # => bray1972 # => shasoGORGEOUS # => kimix
  8. 8. Beispiel Twitter-API class Twitter include HTTParty base_uri 'twitter.com' def initialize(u, p) @auth = {:username => u, :password => p} end def timeline(which=:friends, options={}) options.merge!({:basic_auth => @auth}) self.class.get("/statuses/#{which}_timeline.json", options) end def post(text) options = { :query => {:status => text}, :basic_auth => @auth } self.class.post('/statuses/update.json', options) end end twitter = Twitter.new(USERNAME, PASSWORD) pp twitter.timeline.map{|t| t['user']['name']}
  9. 9. weitere Beispiele • http://github.com/jnunemaker/httparty/tree/ master/examples
  10. 10. FakeWeb • Helper, um HTTP-Anfragen zu stubben • Testumgebungen von live-Anfragen entkoppeln • Tests sind auch lauffähig, wenn keine Netzwerkverbindung besteht • arbeitet mit allen Libraries die auf Net::HTTP basieren • http://github.com/chrisk/fakeweb/
  11. 11. Installation sudo gem install fakeweb
  12. 12. Mit einfacher Zeichenkette antworten require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:get, "http://twitter.com/statuses/ public_timeline.json", :body => "Hello World!") response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Hello World! response = HTTParty.get('http://search.twitter.com/trends.json') puts response.body # es wird die Antwort der echten Anfrage zurückgeliefert
  13. 13. Einer Antwort einen Status hinzufügen require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:get, "http://twitter.com/statuses/ public_timeline.json", :body => "Nothing found", :status => ["404", "Not Found"]) response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.code # => "404" puts response.message # => "Not Found" puts response.body # => "Nothing found"
  14. 14. Auf jede HTTP Methode antworten require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:any, "http://twitter.com/statuses/ public_timeline.json", :body => "response for any HTTP method") response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => response for any HTTP method response = HTTParty.post('http://twitter.com/statuses/public_timeline.json') puts response.body # response for any HTTP method
  15. 15. Wiederholende Anfragen require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:get, "http://twitter.com/statuses/public_timeline.json", [{:body => "Public Timeline", :status => ["200", "OK"]}, {:body => "Timeline not found", :status => ["404", "Not Found"]}]) response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Public Timeline puts response.code # => 200 puts response.message # => OK response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Timeline not found puts response.code # => 404 puts response.message # => Not Found response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Timeline not found puts response.code # => 404 puts response.message # => Not Found
  16. 16. Wiederholende Anfragen require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:get, "http://twitter.com/statuses/ public_timeline.json", [{:body => "Public Timeline", :status => ["200", "OK"], :times => 2}, {:body => "Timeline not found", :status => ["404", "Not Found"]}]) response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Public Timeline response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Public Timeline response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Timeline not found
  17. 17. HTTP Authentifizierung require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:get, "http://example.com/secret", :body => "Unauthorized", :status => ["401", "Unauthorized"]) FakeWeb.register_uri(:get, "http://user:pwd@example.com/secret", :body => "Authorized") response = HTTParty.get('http://example.com/secret') puts response.body # => Unauthorized response = HTTParty.get('http://example.com/secret', :basic_auth => {:username => "user", :password => "pwd"}) puts response.body # => Authorized
  18. 18. Alle registrierten URIs löschen require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:get, "http://twitter.com/statuses/ public_timeline.json", :body => "Hello World!") response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Hello World! FakeWeb.clean_registry response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # es wird die Antwort der echten Anfrage zurückgeliefert
  19. 19. Alle live Anfragen blockieren require "rubygems" require "fakeweb" require "httparty" FakeWeb.allow_net_connect = false response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') # => raises FakeWeb::NetConnectNotAllowedError require "rubygems" require "fakeweb" require "httparty" # default FakeWeb.allow_net_connect = true response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') # live Anfrage wird gesendet
  20. 20. automatisches einlesen einer Datei <?xml version="1.0" encoding="UTF-8"?> <countries> <country>Belgien</country> <country>Deutschland</country> <country>Luxembourg</country> </countries> require "rubygems" require "httparty" require "fakeweb" FakeWeb.register_uri(:get, "http://example.com/countries.xml", :body => 'fixtures/countries.xml', :content_type => "text/xml") response = HTTParty.get('http://example.com/countries.xml') puts response.inspect # => {"countries"=>{"country"=>["Belgien", "Deutschland", "Luxembourg"]}}
  21. 21. HTTP response headers definieren require "rubygems" require "fakeweb" require "httparty" FakeWeb.register_uri(:get, "http://twitter.com/statuses/ public_timeline.json", :body => "Hello World!", :content_type => "text/plain") response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') puts response.body # => Hello World! puts response.headers.inspect # => {"content-type"=>["text/plain"]}

×