Ruby as a glue language

3,681 views

Published on

my speak at rubyconfchina 2010

Published in: Business, Technology
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,681
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
58
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Ruby as a glue language

  1. 1. RubyConfChina 2010 Ruby as a glue language – connecting software components together
  2. 2. Quake Wang <ul><li>I'm a ruby programmer
  3. 3. I was a developer of javaeye.com
  4. 4. I'm working on an on-line game operation platform </li></ul>
  5. 5. Glue language <ul><li>http://en.wikipedia.org/wiki/Glue_language
  6. 6. Interpreted
  7. 7. Dynamically typed
  8. 8. Quick programming
  9. 9. Domain specific language (DSL) </li></ul>
  10. 10. Demo <ul><li>Generate some game activation code
  11. 11. Import, main, javac, classpath, debug ...
  12. 12. 杀鸡用牛刀?
  13. 13. Ruby rescued me </li></ul>
  14. 14. Demo : generate activation code ./generate.rb > code.txt 100.times do puts (0...8).map{ ('a'..'z').to_a[rand(26)] }.join end
  15. 15. Open source gems introduction <ul><li>Full text search - Solr
  16. 16. Integration with SOAP web service
  17. 17. XMPP chat bot and game matching </li></ul>
  18. 18. <ul>Example : full text search - Solr </ul><ul><li>A popular, mature, open source search server
  19. 19. HTTP, XML/JSON Interfaces
  20. 20. Index replication, Cache ... </li></ul>Solr Servlet Container admin update select Standard request handler Custom request handler XML response writer JSON response writer XML Update Handler CSV Update Handler Lucene
  21. 21. <ul>Sunspot </ul><ul><li>gem install sunspot
  22. 22. Sunspot.config.solr.url = ' http://solr.host:9818/solr '
  23. 23. Setup classes for search and indexing </li></ul>Sunspot.setup(Post) do text :title, :default_boost => 2 text :body time :published_at end
  24. 24. <ul>Sunspot </ul><ul><li>Full text search </li></ul>Sunspot.search Post do keywords 'ruby' without :title, 'gem' with(:published_at).less_than 4.weeks.ago paginate :page => 2, :per_page => 15 order_by(:published_at, :desc) end
  25. 25. <ul>Example : Integration with SOAP </ul><ul><li>SOAP: Simple Object Access Protocol
  26. 26. SIMPLE?
  27. 27. I love REST more than SOAP
  28. 28. I love JSON more than XML
  29. 29. 不如意事常八九
  30. 30. Ruby rescued me </li></ul>
  31. 31. <ul>Savon </ul><ul><li>gem install savon
  32. 32. Call SOAP </li></ul>client = Savon::Client.new &quot;http://host/service.wsdl&quot; client.login! do |soap| soap.body = { &quot;userName&quot; => &quot;myusername&quot;, &quot;password&quot; => &quot;mypassword&quot; } end
  33. 33. <ul>Example : XMPP Bot </ul><ul><li>gem install blather
  34. 34. Write a gtalk echo bot in 3 blocks </li></ul>require 'rubygems' require 'blather/client' setup 'rubyconfchina2010@gmail.com', 'password'
  35. 35. <ul>Example : XMPP Bot </ul># Auto approve subscription requests subscription :request? do |s| write_to_stream s.approve! end # Echo back what was said message :chat?, :body do |m| say m.from, &quot;You sent: #{m.body}&quot; end
  36. 36. <ul>Demo </ul>
  37. 37. <ul>Example : game matching </ul><ul><li>We are using OpenFire as XMPP server
  38. 38. Write a game matching room allocator in few lines </li></ul>message :chat?, :body => 'request_room' do |m| user = User.find(m.from.resource.to_i) room = user.request_room server.say m.from, room.id end
  39. 39. <ul>A good ruby gem should be </ul><ul><li>Simple
  40. 40. Intuitive
  41. 41. Expressive
  42. 42. DSL style </li></ul>
  43. 43. <ul>Example: xmpp4r v.s blather </ul>cl = Client.new(&quot;rubyconfchina2010@gmail.com&quot;) cl.connect cl.auth(&quot;password&quot;) cl.send(Presence.new) cl.add_message_callback do |m| if m.type != :error m2 = Message.new(m.from, &quot;You sent: #{m.body}&quot;) m2.type = m.type cl.send(m2) end end
  44. 44. <ul>Thanks </ul><ul><li>Twitter: quakewang
  45. 45. Blog: http://quake.javaeye.com
  46. 46. Email: quake.wang@gmail.com </li></ul>

×