Ruby-PayPal
I did a simple Ruby gem to allow easy access to PayPal’s NVP APIs recently. It isn’t much at this point in time, though both the Direct Payment and Express Checkout has been coded, only Direct Payment has really been tested. Here’s the writeup directly from the project site at http://ruby-paypal.rubyforge.org.
Installing Ruby-PayPal
A lightweight ruby wrapper for PayPal NVP APIs. To install type the following at the command line:
$ gem install ruby-paypal
Using Ruby-PayPal
It‘s critical that you understand how PayPal works and how the PayPal NVP API works. You should be relatively well-versed in the NVP API Developer Guide and Reference. You should also visit and register yourself with the PayPal Developer Network and get a Sandbox account with in the PayPal Development Central.
Note that this library only supports the API signature method of securing the API credentials.
Direct Payment
To use credit card payment through PayPal, you need to use the DoDirectPayment APIs:
username = <PayPal API username> password = <PayPal API password> signature = <PayPal API signature> ipaddress = '192.168.1.1' # can be any IP address amount = '100.00' # amount paid card_type = 'VISA' # can be Visa, Mastercard, Amex etc card_no = '4512345678901234' # credit card number exp_date = '022010' # expiry date of the credit card first_name = 'Sau Sheong' last_name = 'Chang' paypal = Paypal.new(username, password, signature) # uses the PayPal sandbox response = paypal.do_direct_payment_sale(ipaddress, amount, card_type, card_no, exp_date, first_name, last_name) if response.ack == 'Success' then # do your thing end
The above code is for a final sale only.
Note that the credit card number is checked against a modulo-10 algorithm (Luhn check) as well as a simple credit card type check. For more information please refer to http://en.wikipedia.org/wiki/Luhn_algorithm and http://en.wikipedia.org/wiki/Credit_card_number
Facebook application/Ruby on Rails mashup
Been busy fiddling around with Facebook application development, came up with one for my upcoming Ruby on Rails mashup book.
http://apps.facebook.com/job_board
This application allows you (as the Facebook user) to search for jobs (currently using Indeed.com) using information from your work history, education history and current location. The list of jobs can then be displayed on an online map (Google Maps) and news (from DayLife) and blog entries (from Technorati) can be displayed about the company that posted the job. The application is written with Ruby on Rails using RFacebook.
If you want to know how I wrote this application, remember to check out my book when it comes out!
Web Mashups with Ruby on Rails
I’m going to write a book! Tentatively titled ‘Web Mashups with Ruby on Rails’ for now, I’m working with Packt Publishing to finalize the paperwork but I’m already starting on it. I’ve categorized web mashups into 3 different types, and is writing 2 – 3 mashups projects for each category. The book will take about 7 months to complete writing, but will likely take close to 9 months to be completed.
In the meantime, I have a blog post on mashups if you’re interested.
JRuby on Rails running in Apache Tomcat
I tried JRuby for the first time in small project listed in my previous blog entry. While I wouldn’t say it’s love at first sight (or first use), it intrigued me sufficiently to try out other usages of JRuby.
What is JRuby? If you just think it this way — the Ruby that you and I know and love is a C implementation of the Ruby programming language — then JRuby is quite easy to understand. JRuby is the Java implementation of the Ruby programming language.
What the major difference and advantage in JRuby is that you’ll be able to define and interact with Java classes in Ruby directly. And that’s it. It’s not really different from Ruby, except that some built-in classes are not supported. And if you look at it from another angle, it’s not really that different between doing it this way (using JRuby) and using WIN32API or WIN32OLE, except of course you’ll need to know all the Java nuances and quirks.
Enter JRuby on Rails. Why JRuby on Rails? I think anyone who has done Java and Rails would be able to answer this one. It’s simply the vast ocean of Java libraries available, both good and bad, that gives JRuby on Rails the appeal for me. No doubt, the ‘enterprise’ factor of Java EE (i.e. running on a Java EE app server like Glassfish or the likes) is pretty appealing to those who care but I would say it’s not really worth it (at least at this point in time).
I was quite impressed with the demos done with JRuby and Glassfishv2 during JavaOne so I thought to give it a run to see what happens. The results are not too bad, although not everything works smoothly. For the current status of Rails support in JRuby click here.
Let me run through quickly how you can deploy a JRuby on Rails application on Apache Tomcat 6. Why Tomcat? While there are a growing number of Java EE application servers out there, Tomcat is probably the one that most Java EE programmers cut their teeth and is most familiar with.
1. You’ll need to install JRuby. Installation is quite easy, just download the latest package from http://dist.codehaus.org/jruby/ and then unzip it into any directory.
2. Now set the environment variable JRUBY_HOME to point to this directory and set your path to point to JRUBY_HOME/bin. If you’re doing this all the time you should set this permanently.
3. Now quickly test your JRuby installation by typing ‘jirb’ on your console and then the following:
$ jirb irb(main):001:0> require 'java' irb(main):002:0> frame = javax.swing.JFrame.new "JRuby Frame" irb(main):003:0> frame.add "Center", button irb(main):004:0> frame.setVisible true irb(main):004:0> frame.setSize 200,100
4. You’ll notice that the frame is resized dynamically
5. Now you’ll need to install the Rails gems. Go to JRUBY_HOME/bin and run this command:
$ jruby gem install rails
JRuby will automatically install the necessary rails gems.
6. You’ll need to install the ActiveRecord-JDBC gem as well
$ jruby gem install activerecord-jdbc
7. Now, you can create or take your existing rails application and add the Rails-Integration plugin.
$ rails JRubyRailsTest $ cd JRubyRailsTest $ script/plugin install svn://rubyforge.org/var/svn/jruby-extras/trunk/rails-integration/plugins/goldspike
8. Check the rake tasks available now, you should have a couple of new rake tasks that starts with war:
$ rake –tasks ... rake tmp:war:clear # Clears all files used in the creation of a war rake war:shared:create # Create a war for use on a Java server where JRuby is available rake war:standalone:create # Create a self-contained Java war rake war:standalone:run # Run the webapp in Jetty
The tasks are pretty self explanatory.
9. There are a couple of things you need to change in the Rails configuration itself to enable Rails to run properly under a Java EE application server so you need to go into the config directory now.
10. Open up environment.rb and add these couple of lines in :
if RUBY_PLATFORM =~ /java/ require 'rubygems' RAILS_CONNECTION_ADAPTERS = %w(jdbc) end
This should come just before
Rails::Initializer.run do |config|
This will enable you to use ActiveRecord-JDBC. There are a couple of configurations given on other tutorials but this is the one that works for me.
11. Now open up database.yml to configure the database connection.
development:adapter: jdbc driver: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/<myDevelopmentDatabase> username: <username> password: <password> production: adapter: jdbc driver: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/<myProductionDatabase> username: <username> password: <password>
Remember to change both the development and production. You can point them to the same database, but right now, the Rails-Integration will redirect to the production database once you archive your application into a WAR. In this example, I’m using MySQL but you can use any JDBC URL to connect to any JDBC enabled database.
12. If you’re doing a new application, and you want to generate some code, remember to use JRuby and not the normal Ruby interpreter. For example, to create a scaffold for User (assuming you have done all the necessary table creation in your database of choice)
$ jruby script/generate scaffold User
If you can’t generate, go back and check out steps 9 – 11. Also to note is that you’re running Java as well, so check your classpath if you are missing the JDBC drivers for your database.
13. For instant gratification, run WEBrick on JRuby:
$ jruby script/server
And try it out quickly to ensure it works.
14. Onwards to make it work for a Java EE application server! First, you’ll need to add in any libraries you need into the Rails-Integration configuration, so that it’ll include that library when it builds the WAR file. Go to <your Rails application>/vendor/plugins/goldspike/lib/war_config.rb and add in your libraries. For example, to use MySQL, you’ll need to put in the MySQL drivers like this:
add_library(maven_library ('mysql', 'mysql-connector-java', '5.0.4'))
Browse around the code until you find a place to put this line.
15. We’re almost there. Run the rake task in your Rails application folder to create the WAR file:
$ JRUBY_HOME/bin/jruby JRUBY_HOME/bin/rake war:standalone:create
This will create the WAR file by archiving your code and any libraries needed to run JRuby. The first time you run it might take a while and you’ll need to be connected to the Internet in order for Maven to bring in the Java libraries from its public repository but subsequently it’ll be much faster. A problem I faced earlier on is that while getting some libraries, the connection will expire and creation is aborted. I had to run it a few times before it completes properly.
16. You’re done! You now have a WAR file that wil run on Tomcat 6 or Glassfishv2 (I tried both). Just deploy them normally as you would any web application.
Singapore.rb event at the National Library
The Singapore.rb is going to have a meeting at the National Library on 19 April. The NLB has provided us with a room enough for 50 odd people and wifi-access. It’s going to be a general get-together plus a sharing session. Two speakers has been confirmed more or less — Choon Keat, who will be talking about KRJS and hpricot_forms and myself, who will be talking about FxRuby and Screensvr. Yes, one is about Rails and the other is about Ruby (not Rails). It should be fun and I hope it’ll be start of many more meetings to come, so join us if you’re around in Singapore!
For more information, check on this thread regularly!
leave a comment