Fake My Party
-
Upload
tanja-otto -
Category
Technology
-
view
1.797 -
download
4
description
Transcript of Fake My Party
![Page 1: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/1.jpg)
Fake my PartyTanja Otto
24.11.2009Düsseldorf on Rails
![Page 2: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/2.jpg)
SalesLentz::DevTeam
![Page 3: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/3.jpg)
Ü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
![Page 4: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/4.jpg)
HTTParty
• einfaches senden von HTTP-Anfragen
• JSON und XML werden automatisch in Ruby-Hashes umgewandelt
• http://github.com/jnunemaker/httparty
![Page 5: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/5.jpg)
HTTParty
XML
JSON
HTTParty HASH
![Page 6: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/6.jpg)
Installation
sudo gem install httparty
![Page 7: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/7.jpg)
Ein erstes Beispielrequire "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
![Page 8: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/8.jpg)
Beispiel Twitter-APIclass 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) endend
twitter = Twitter.new(USERNAME, PASSWORD)pp twitter.timeline.map{|t| t['user']['name']}
![Page 9: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/9.jpg)
weitere Beispiele
• http://github.com/jnunemaker/httparty/tree/master/examples
![Page 10: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/10.jpg)
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/
![Page 11: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/11.jpg)
Installation
sudo gem install fakeweb
![Page 12: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/12.jpg)
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
![Page 13: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/13.jpg)
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"
![Page 14: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/14.jpg)
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
![Page 15: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/15.jpg)
Wiederholende Anfragenrequire "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 Timelineputs response.code # => 200puts response.message # => OK
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')puts response.body # => Timeline not foundputs response.code # => 404puts response.message # => Not Found
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')puts response.body # => Timeline not foundputs response.code # => 404puts response.message # => Not Found
![Page 16: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/16.jpg)
Wiederholende Anfragenrequire "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
![Page 17: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/17.jpg)
HTTP Authentifizierungrequire "rubygems"require "fakeweb"require "httparty"
FakeWeb.register_uri(:get, "http://example.com/secret", :body => "Unauthorized", :status => ["401", "Unauthorized"])FakeWeb.register_uri(:get, "http://user:[email protected]/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
![Page 18: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/18.jpg)
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
![Page 19: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/19.jpg)
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"
# defaultFakeWeb.allow_net_connect = true
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')# live Anfrage wird gesendet
![Page 20: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/20.jpg)
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"]}}
![Page 21: Fake My Party](https://reader034.fdocuments.net/reader034/viewer/2022052310/5555d600d8b42aaf158b4f45/html5/thumbnails/21.jpg)
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"]}