The document discusses using JRuby to build a Twitter client application. It provides steps for setting up the development environment, creating the initial project structure using MVC patterns, integrating the Twitter API via gems, and packaging the application for cross-platform distribution. Potential issues addressed include working around the lack of native extensions in JRuby and ensuring gems are loaded properly.
24. 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
25. 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
26. 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..
32. 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
36. 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
37. 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
38. Gotchas
a few
java.lang.NullPointerException: null
build paths
gems
39. -1’s
The not so good
bits
• Assumes knowledge of Java
• Quirky MVC
• Learning curve
• Early days...
41. +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
43. Blinked?
Go here
http://tweetobix.com
Steps plus code to download