Monkeybars in the Manor

3,059 views

Published on

How to build a Twitter desktop client using JRuby + MonkeyBars. As demo’d at RubyManor

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

  • Be the first to like this

No Downloads
Views
Total views
3,059
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Monkeybars in the Manor

  1. 1. Monkeybars in the manor
  2. 2. What do you think? about Java?
  3. 3. DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE DIE, DIE
  4. 4. What do you think? About JRuby?
  5. 5. What do you think? About JRuby? (it’s not so bad really)
  6. 6. Before... JRuby Installed Netbeans Downloaded Gems
  7. 7. Before... JRuby Installed Netbeans Downloaded Gems
  8. 8. Before... JRuby Installed Netbeans Downloaded Gems
  9. 9. Before... JRuby Installed Netbeans Downloaded Gems
  10. 10. Twitter Client The Spec •Login •Report errors •Display friend tweets •Post tweets
  11. 11. Creating... the app
  12. 12. Initial Setup + scaffolding $> monkeybars projectname $> cd projectname $> rawr install (choose option 2) $> jruby -S rake generate ALL='src/login'
  13. 13. Install Gems and freeze Gems $> jruby -S gem install twitter $> cd lib/ruby $> jruby -S gem unpack twitter $> jruby -S gem unpack hpricot
  14. 14. Skeleton App will look something like this
  15. 15. Net Beans New Project 1. Import Existing Java project with sources 2. Add JRuby complete library 3. Add Swing.. Net Beans
  16. 16. Net Beans New Project 1. Import Existing Java project with sources 2. Add JRuby complete library 3. Add Swing.. LIVE DEMO! Net Beans
  17. 17. Under the hood Like Rails but not as you know it M VC
  18. 18. Under the hood Like Rails but not as you know it M VC
  19. 19. username.text View password.text login_button username Controller Model password
  20. 20. Sample Code a quick look controllers, models, views event handling
  21. 21. Controller class LoginController < ApplicationController set_model 'LoginModel' set_view 'LoginView' set_close_action :exit def login_button_action_performed model, view_transfer = view_state if @user = model.login(model.username, model.password) TweetController.instance.setup(model) TweetController.instance.open close else transfer[:errors] = model.errors signal(:login_failed) end end end
  22. 22. Model class LoginModel attr_accessor :username, :password, :message, :errors, :user def login(username, password) begin @user = Twitter::Base.new(username, password) @user.timeline self rescue Twitter::CantConnect => e @errors ||= [] @errors << $!.to_s return false end end end
  23. 23. View class LoginView < ApplicationView set_java_class 'login.LoginFrame' map :view => quot;username.textquot;, :model => :username map :view => quot;password.textquot;, :model => :password define_signal :name => :login_failed, :handler => :login_failed def login_failed(model, transfer) message.text = transfer[:errors].to_s end nom end nom..
  24. 24. The App pre alpha 0.333333333333
  25. 25. Result
  26. 26. Package For cross platform distro rawr install Text 2: Keep the current Rakefile, and add the rawr content to the top. http://rawr.rubyforge.org/
  27. 27. Package Commands For cross platform distro Text Remember to edit your build_configuration.yaml 1st
  28. 28. Package Sample Config for RAWR to generate app # Name of the created jar file project_name: tweetobix # Directory to create and place produced project files in output_dir: package ----------<SNIP>---------------- # Individual files that should be loaded on the classpath classpath_files: - lib/java/jruby-complete.jar - lib/java/swing-layout-1.0.3.jar see build_configuration.yaml
  29. 29. Tricks and tips
  30. 30. Why didn’t you use the twitter4r gem ?
  31. 31. jruby --command gem install twitter4r Building native extensions. This could take a while... mypath/jruby-1.1.4/lib/ruby/1.8/mkmf.rb:7: JRuby does not support native extensions. Check wiki.jruby.org for alternatives. (NotImplementedError) from mypath/jruby-1.1.4/lib/ruby/1.8/mkmf.rb:1:in `require' from extconf.rb:1 ERROR: Error installing twitter4r: ERROR: Failed to build gem native extension. mypath/jruby-1.1.4/bin/jruby extconf.rb install twitter4r Gem files will remain installed in /mypath/jruby-1.1.4/lib/ruby/ gems/1.8/gems/json-1.1.3 for inspection. Results logged to mypath/jruby-1.1.4/lib/ruby/gems/1.8/gems/ json-1.1.3/ext/json/ext/parser/gem_make.out
  32. 32. Requiring Gems Frozen require 'manifest' # Add vendored gems to load path and require them automatically Dir[File.expand_path(File.dirname(__FILE__) + quot;/../lib/ruby/**quot;)].map do |dir| lib = quot;#{dir}/libquot; lib = dir if lib =~ /.w+$/ #File.directory? is broken in current JRuby for dirs inside jars $: << lib gem_name = File.basename(dir.gsub(/-d+.d+.d+$/, '')) gem_path = dir + quot;/lib/quot; + gem_name + quot;.rbquot; begin require gem_path if File.exists? gem_path rescue p quot;could not load gem #{gem_path}quot; end end
  33. 33. Gotchas a few java.lang.NullPointerException: null build paths gems
  34. 34. -1’s The not so good bits • Assumes knowledge of Java • Quirky MVC • Learning curve • Early days...
  35. 35. +1’s The good bits • GUI Builder (using Netbeans) • Interface with mature Java libs • It’s all Ruby! • Cross platform distribution • No platform dependancy on Ruby • Cutting edge
  36. 36. Blinked? Go here http://tweetobix.com Steps plus code to download
  37. 37. Fin. martinbtt http://www.beyondthetype.com

×