PunBB SDK for Ruby on Rails

March 12th, 2007

After my last post about getting IPB SDK working for the latest IPB Release, I realized that, regardless of my dislike of forums as forums for online communication (PUN intended, shapap, double-entendre in yo face, mother!@#$%^), I have tons of code and projects that revolved around PHP Forums, and Rails backend applications.

In keeping with my New Years Resolution of sharing those tidbits that make my life easier, here is a PunBB SDK/Wrapper! It creates ALL of the models needed for the punbb’s database objects, as well as a VERY simple controller/views for auto-login capabilities. The projects that I’ve used this on are very niche-oriented and don’t lend themselves to extracting much generic code. I can say, that a simple PunBB installation plus this generator can you have you running a fairly pimptastic site in no time! If you like forums, that is ;)

To use it, install your rails app, install your punbb installation, then grab the plugin


./script/plugin install svn://ahgsoftware.com/punbb_sdk/trunk

When developing a Rails application that requires at least ‘autologin’ integration with a PHP forum application, I usually have to decide between several routes.

    The direct DB/cookie method: You replicate the entire logic path of the forum system (PunBB/Vanilla EASY, IPB not so much).
    By Proxy: You use Net::HTTP, or libcurl, to submit the login form, intercept the cookie, and set the user’s browser cookies. This sucks, because some software makes the submitted variables jump through hoops of its own creation (coughJoomla/cough)
    AJAX: I am beginning to love this method, you just have your login form submit directly to your own software AND to the forum application via some nifty ajax. This PunBB SDK uses that particular technique

As far as forums go, PunBB is at the top of my list, because its developer friendly, and I just like the overall attitude that Rickard has over there! Consider donating to their cause if you end up using this plugin (the cause of bringing a SIMPLE forum to the world).

Erb Output Buffering

January 14th, 2007

For those of you that want some painless output buffering in Rails (when using ERB templates), I have a plugin that may help you


./script/plugin install svn://ahgsoftware.com/erb_buffer/trunk

It handles unlimited nesting of buffers, and its fairly simple to use and extend. Lemme know how you guys like it. I see plenty of people downloading but no ones commenting! Any feedback for improving them would be greatly appreciated!

Here’s an example of single buffering

1
2
3
4
5
<% @contents = capture_buffer do  %>
Buffered Content, show me later in green!
<% end %>

<p style="color: green;"><%= @contents %></p>

And here is a nesting example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<% @contents_surface = capture_buffer do  %>
        I'm heavy on top, like an inverted pyramid!
        <% @contents_shallow = capture_buffer do %>
                Only Shallow Hal would occupy this plane of existence.
                <% @contents_deep = capture_buffer do %>
                        deeper deeper
                <% end %>
        <% end %>
<% end %>

<p>ending nested buffers</p>

<%= @contents_surface %>
<%= @contents_shallow %>
<%= @contents_deep %>

I must say, that, I do not like the mvc(h) setup in Rails very much. For me, the ORM is nearly a work of art, and Rails itself is certainly a very well thought out glue for the components. I think it suffers from being too small for HUGE enterprise uses, and too large for simple 1-10 file scripts that could easily handle some simple webtask for you. I’m starting to discover that a microframework like Camping might suit 40% of my projects much better than a fullblow Rails installation, and use of captured buffers, akin to PHP’s ob_* methods, would make much more sense. I’ll let ya know when I get a production site up that breaks the Rails mold!

I really dig SWFObject in Javascript for making nice lightweight flash objects. What I really need, much more often though, is a way to present any Flash, Movie, Game, Song, or Thing easily in a view, and for it to be just like any other ActionView Helper.

So here is what I use, probably more often than any other plugin (except acts_as_authenticated)


./script/plugin install svn://ahgsoftware.com/simple_object/trunk

It’ll take whatever arbitrary parameters you give it, however, I’m lazy as hell and I just call it with no parameters, and let the code handle inferring decent defaults (although for video/image/swf you’ll want at least the width/height). Here’s how I use it.

1
2
3
4
5
6
7
8
9
simple_object '/swfs/game.swf'

simple_object '/songs/test.mp3'

simple_object '/swfs/mp3player.swf',:url=>"/mp3s/tool.mp3"

simple_object 'test.mov',:width=>"640",:height=>"480"

# and so on..

I’ll be adding support for more filetypes shortly. Enjoy!

GnuPG Ruby on Rails Plugin

January 1st, 2007

I have another alpha-level plugin for you folks. This one has also saved me arse on many an occasion. Keep in mind, encryption/decryption setups on servers vary like the color of leaves in autumn. This particular plugin works on the paradigm of having a web-accessible public key that encrypts user data, and a temporarily available secret key that is used for decryption. Its entirely up to you to ensure that things are kept secure, but, as a result, the plugin is tiny!


./script/plugin install svn://ahgsoftware.com/gnupg/trunk

Here’s a quick example, assuming you have a pub key—imported

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# We're using the same workdir for pub and sec, they could differ!
workdir = File.join(RAILS_ROOT,"tmp","gnupg")
passphrase = "uglydonkeys"

# This is a darwin-ports on Mac OS X version of gpg
gnupg = GnuPG.new :binary=>"/opt/local/bin/gpg",
   :workdir=>workdir,
   :homedir_pub=>workdir,
   :homedir_sec=>workdir,
   :recipient=>"your uid"

plain_message = "no more mustachoed tyrants, muhwahwahwa, buhwahwahwa"
encrypted_message = gnupg.encrypt(plain_message)

# Load ascii sec key, from wherever you might have it
gnupg.load_key File.read("sec_key.asc")
decrypted_message = gnupg.decrypt(encrypted_message, passphrase)
gnupg.drop_key

puts plain_message
puts encrypted_message
puts decrypted_message

PHP-to-Ruby Rails Plugin

December 29th, 2006

Aight, the alpha-version of my php/rails plugin is up. There is a ton of work left to do on it. I’ve yet to compile the various snippets that I have scattered amongst my projects, but this should get some aspiring neophyte php programmers into switching to the non-Dark Side of web development.

Install it with


./script/plugin install svn://ahgsoftware.com/php_compat/trunk

And here are some examples

1
2
3
4
5
6
7
8
9
10
# Dates
PhpCompat.date("F j, Y, g:i")   # Translated to Time.strftime chars
PhpCompat.time  # Integer unix timestamp

# Posix methods
PhpCompat.get_pwnam('localusername') # Struct with passwd info

# File methods (not network enabled... yet!)
PhpCompat.file_put_contents('/tmp/file.txt',"yo yo yo")
PhpCompat.file_get_contents('/tmp/file.txt')

A few caveats..

  1. The php date-format to ruby Time-strftime translations are about 90% complete. There aren’t any convenient fallbacks, so certain formatting characters will not translate at all.
  2. The posix functions are POSIX functions!! So don’t expect much luck on Windows, although, they work great on OS X (naturally ;) )
  3. File methods don’t work on url’s yet
  4. There are some functions that I’ll never translate, as some things you just HAVE to do the ruby way. Its so much more concise, readable, and effective when you do it the ‘matz way’.

Since the main focus of this plugin is to make software migrations a bit less painful, the next set of methods that I wanna stick in there, is the mysql functions. I’ve used this in migrating thousands of lines of php code to ruby, so, I hope it helps someone else out too!

 

Michael Cerna Chicago-based Rails Developer and Avid Musician. More ...

Search

Categories

  • Home (15)
  • Rails Plugins (5)
  • Pages (9)
  • Archives

    Tags

    BlogRoll