Slideshare.net (beta)

 
Post: 
Myspace Hi5 Friendster Xanga LiveJournal Facebook Blogger Tagged Typepad Freewebs BlackPlanet gigya icons



All comments

Add a comment on Slide 1

If you have a SlideShare account, login to comment; else you can comment as a guest


Showing 1-50 of 6 (more)

Making and Breaking Web Services with Ruby

From err, 1 year ago

Mainly about SOAP and Microformats and Newsletters.

5419 views  |  1 comment  |  5 favorites  |  198 downloads  |  5 embeds (Stats)
 
 
 

Privacy InfoNew!

This slideshow is Public

 
Embed in your blog
Embed (wordpress.com)
custom

Slideshow Statistics
Total Views: 5419
on Slideshare: 5408
from embeds: 11* * Views from embeds since 21 Aug, 07

Slideshow transcript

Slide 1: Making and Breaking Web Services (with Ruby) Chris Wanstrath Err Free http://errfree.com

Slide 2: ttp://farm1.static.flickr.com/138/320699460_b8e7c1e7e6_o.jpg

Slide 3: SOAP • Simple Object Access Protocol? • Lies. • Service Oriented Architecture Protocol • wtf.

Slide 4: Newsletters!

Slide 5: “Outbound” • Slow response time • Duplication of data • Hard to debug • require ‘soap/wsdlDriver’

Slide 6: Ruby SOAP Library: Your Friend • Creates methods on the fly • Seems to work pretty well • Transparently converts Ruby types to SOAP definitions

Slide 7: Ruby SOAP Library: Your Enemy • Hard to debug (dump req/res to file) • No one has ever used it • There are not any alternatives • The code is a jungle

Slide 8: Why use SOAP? • One reason: legacy.

Slide 10: Wrapping SOAP def update_user(email, subs = [], unsubs = []) client.updateUser(brand, email, email, demo, subs, unsubs) end

Slide 11: Wrapping SOAP def update_user(email, subs = [], unsubs = []) client.updateUser(brand, email, email, demo, subs, unsubs) end def get_user(email) client.getUser(email, brand) end

Slide 12: Wrapping SOAP def update_user(email, subs = [], unsubs = []) client.updateUser(brand, email, email, demo, subs, unsubs) end def get_user(email) client.getUser(email, brand) end

Slide 13: Wrapping SOAP def update_user(email, subs = [], unsubs = []) client.updateUser(brand, email, email, demo, subs, unsubs) end def get_user(email) client.getUser(email, brand) end

Slide 15: Testing SOAP • Use mocks • Mocha: http://mocha.rubyforge.org

Slide 16: Testing SOAP def test_get_user_should_hit_client email = 'chrisw@nstrath.com' Outbound.client.expects(:getUser).with(email, Outbound.brand) Outbound.get_user(email) end

Slide 17: Testing SOAP soap_methods = { :getUser => true, :updateUser => true } Outbound.stubs(:client).returns(mock('SOAP Service', soap_methods))

Slide 18: Running a SOAP Server in Ruby

Slide 19: Just Kidding

Slide 20: Microformats! • Your website is your API • Plant classes in HTML • Tell parsers what information is important • http://microformats.org

Slide 21: hReview

Slide 22: hReview <div id="review_16873" class="hreview"> <h5 class="item"><span class="fn">Beringer California Collection White Merlot 20 <abbr class="dtreviewed" title="20070420">(1 day ago)</abbr> <span class="reviewer vcard"> <img class="photo" src="/assets/avatars/c19d1b2080f73765d420b461d3133ffa.jpg" <a class="url fn" href="/people/garyvaynerchuk">garyvaynerchuk</a> </span> <abbr class="rating" title="50.0">50.0<em>/100</em></abbr> <blockquote class="description">Had this in an industry event the other day, man <p class="tags">Tasting Tags: <a href="/tags/pink" rel="tag" class="rel-tag" title="view all wines with this t <a href="/tags/sugar" rel="tag" class="rel-tag" title="view all wines with this </p> </div>

Slide 23: hReview <div id="review_16873" class="hreview"> <h5 class="item"><span class="fn">Beringer California Collection White Merlot 20 <abbr class="dtreviewed" title="20070420">(1 day ago)</abbr> <span class="reviewer vcard"> <img class="photo" src="/assets/avatars/c19d1b2080f73765d420b461d3133ffa.jpg" <a class="url fn" href="/people/garyvaynerchuk">garyvaynerchuk</a> </span> <abbr class="rating" title="50.0">50.0<em>/100</em></abbr> <blockquote class="description">Had this in an industry event the other day, man <p class="tags">Tasting Tags: <a href="/tags/pink" rel="tag" class="rel-tag" title="view all wines with this t <a href="/tags/sugar" rel="tag" class="rel-tag" title="view all wines with this </p> </div>

Slide 24: mofo $ sudo gem install mofo Successfully installed mofo-0.2.2 $ irb -rubygems >> require 'mofo/hreview' => true >> review = HReview.find :first => 'http://corkd.com/wine/view/21670' => #<HReview:0x1598d04 ...> >> review.properties => ["description", "item", "dtreviewed", "tags", "rating", "reviewer"] >> review.rating => 50.0 >> review.item.fn => "Beringer California Collection White Merlot 2005" >> review.reviewer.fn => "garyvaynerchuk"

Slide 25: mofo/hreview.rb class HReview < Microformat one :version, :summary, :type, :dtreviewed, :rating, :description one :reviewer => HCard one :item! do one :fn end end

Slide 26: microformat.rb def collector collector = Hash.new([]) def collector.method_missing(method, *classes) super unless %w(one many).include? method.to_s self[method] += Microformat.send(:break_out_hashes, classes) end collector end

Slide 27: mofo supports... • xoxo • hCard • geo • hCalendar • adr • hReview • xfn • hEntry • hResume

Slide 30: What else can they do?

Slide 31: Operator

Slide 32: Operator

Slide 33: Okay.

Slide 34: Hpricot ( by _why )

Slide 35: Hpricot $ irb -rubygems -r'open-uri' >> require 'hpricot' => true >> page = Hpricot open('http://google.com') => #<Hpricot::Doc ...> >> page.search(:a).size => 20 >> page.at(:a) => {elem <a href="/url?sa=p&pref=ig&pval=3 &q=http://www.google.com/ig%3Fhl%3Den&usg= AFrqEzfPu3dYlSVsfjI7gUHePgEkcx_VXg"> "Personalize this page" </a>}

Slide 36: Hpricot Can use XPATH, CSS selectors, whatever, to search

Slide 37: Hpricot >> page = Hpricot(open('http://brainspl.at')) => #<Hpricot::Doc ...> >> page.search('.post').size => 10

Slide 38: Hpricot >> corkd = 'http://corkd.com/wine/view/21670' => 'http://corkd.com/wine/view/21670' >> page = Hpricot(open(corkd)) => #<Hpricot::Doc ...> >> page.search('.hreview').size => 4

Slide 39: Hpricot/:mofo >> page.at('.hreview').at('.item').at('.fn').inner_text => "Beringer California Collection White Merlot 2005" >> review.item.fn => "Beringer California Collection White Merlot 2005"

Slide 40: Oh, you can use Hpricot for XML, too. <Export> <Product> <SKU>403276</SKU> <ItemName>Trivet</ItemName> <CollectionNo>0</CollectionNo> <Pages>0</Pages> </Product> </Export>

Slide 41: Oh, you can use Hpricot for XML, too. fields = %w(SKU ItemName CollectionNo Pages) doc = Hpricot(open("my.xml")) (doc/:product).each do |xml_product| attributes = fields.inject({}) do |hash, field| hash.merge(field => xml_product.at(field).innerHTML) end Product.create(attributes) end ( also there’s Hpricot::XML() )

Slide 42: But who uses XML?

Slide 43: Cheat! http://cheat.errtheblog.com (insert short, live demo here)

Slide 44: Thanks • http://mofo.rubyforge.org • http://code.whytheluckystiff.net/hpricot • https://addons.mozilla.org/en-US/firefox/addon/4106 • http://microformats.org • http://upcoming.yahoo.com/ • http://corkd.com/ • http://chow.com • http://chowhound.com

Slide 45: Thanks • http://ronrothman.com/gallery/cnet/cnet_neon • http://flickr.com/photos/bootbearwdc/466751240/ • http://flickr.com/photos/segana/320699460/ • http://flickr.com/photos/spiffariffic/456211526/