Rfm

This version of rfm is a significant upgrade to rfm in a few ways. First and most important (to me anyway) is it allows for the use of secure connections and certificate files. Rfm 1.0.0 is broken in that way and does not allow for this. Second it has a few bug fixes that were never pushed to rfm 1.0.0. Last but not least is that the xml parser is switched to nokogiri which is a huge speed improvement over REXML (used in rfm 1.0.0) on large datasets.

Rfm was primarily designed by Six Fried Rice co-founder Geoff Coffey.

Other lead contributors:

  • Mufaddal Khumri helped architect Rfm in the most ruby-like way possible. He also contributed the outstanding error handling code and a comprehensive hierarchy of error classes.

  • Atsushi Matsuo was an early Rfm tester, and provided outstanding feedback, critical code fixes, and a lot of web exposure.

  • Jesse Antunes helped ensure that Rfm is stable and functional.

  • Larry Sprock added ssl support and switched the xml parser to a much faster Nokogiri.

Rdoc location: rdoc.info/projects/lardawge/rfm

Installation

Run the following if you haven’t already:

gem sources -a http://gemcutter.org

Followed by:

gem install lardawge-rfm

Once the gem is installed, you can use rfm in your ruby scripts by requiring it:

require 'rubygems'
require 'rfm'

Connecting

IMPORTANT:SSL and Certificate verification are on by default. Please see Server#new in rdocs for explanation and setup. You connect with the Rfm::Server object. This little buddy will be your window into FileMaker data.

require 'rfm/rfm'

my_server = Rfm::Server.new(
  :host => 'myservername',
  :username => 'user',
  :password => 'pw',
  :ssl => false
)

if your web publishing engine runs on a port other than 80, you can provide the port number as well:

my_server = Rfm::Server.new(
  :host => 'myservername',
  :username => 'user',
  :password => 'pw',
  :port => 8080, 
  :ssl => false
)

Databases and Layouts

All access to data in FileMaker’s XML interface is done through layouts, and layouts live in databases. The Rfm::Server object has a collection of databases called ‘db’. So to get ahold of a database called “My Database”, you can do this:

my_db = my_server.db["My Database"]

As a convenience, you can do this too:

my_db = my_server["My Database"]

Finally, if you want to introspect the server and find out what databases are available, you can do this:

all_dbs = my_server.db.all

In any case, you get back Rfm::Database objects. A database object in turn has a property called “layout”:

my_layout = my_db.layout["My Layout"]

Again, for convenience:

my_layout = my_db["My Layout"]

And to get them all:

all_layouts = my_db.layout.all

Bringing it all together, you can do this to go straight from a server to a specific layout:

my_layout = my_server["My Database"]["My Layout"]

NOTE: for the ruby experts, Rfm::Server#db is an Rfm::Factory::DatabaseFactory object, which is just a fancied up Hash. So anything you can do with a hash, you can also do with my_server.db. The same goes for my_db.layouts, which is an Rfm::Factory::LayoutFactory.

Working with Layouts

Once you have a layout object, you can start doing some real work. To get every record from the layout:

my_layout.all   # be careful with this

To get a random record:

my_layout.any

To find every record with “Arizona” in the “State” field:

my_layout.find({"State" => "Arizona"})

To add a new record with my personal info:

my_layout.create({
  :first_name => "Geoff",
  :last_name => "Coffey",
  :email => "[email protected]"}
)

Notice that in this case I used symbols instead of strings for the hash keys. The API will accept either form, so if your field names don’t have whitespace or punctuation, you might prefer the symbol notation.

To edit the record whos recid (filemaker internal record id) is 200:

my_layout.edit(200, {:first_name => 'Mamie'})

Note: See the “Record Objects” section below for more on editing records.

To delete the record whose recid is 200:

my_layout.delete(200)

All of these methods return an Rfm::Result::ResultSet object (see below), and every one of them takes an optional parameter (the very last one) with additional options. For example, to find just a page full of records, you can do this:

my_layout.find({:state => "AZ"}, {:max_records => 10, :skip_records => 100})

For a complete list of the available options, see the “expand_options” method in the Rfm::Server object in the file named rfm_command.rb.

Finally, if filemaker returns an error when executing any of these methods, an error will be raised in your ruby script. There is one exception to this, though. If a find results in no records being found (FileMaker error # 401) I just ignore it and return you a ResultSet with zero records in it. If you prefer an error in this case, add :raise_on_401 => true to the options you pass the Rfm::Server when you create it.

ResultSet and Record Objects

Any method on the Layout object that returns data will return a ResultSet object. Rfm::Result::ResultSet is a subclass of Array, so first and foremost, you can use it like any other array:

my_result = my_layout.any
my_result.size  # returns '1'
my_result[0]    # returns the first record (an Rfm::Result::Record object)

The ResultSet object also tells you information about the fields and portals in the result. ResultSet#fields and ResultSet#portals are both standard ruby hashes, with strings for keys. The fields hash has Rfm::Result::Field objects for values. The portals hash has another hash for its values. This nested hash is the fields on the portal. This would print out all the field names:

my_result.fields.each { |name, field| puts name }

This would print out the tables each portal on the layout is associated with. Below each table name, and indented, it will print the names of all the fields on each portal.

my_result.portals.each { |table, fields|
  puts "table: #{table}"
  fields.each { |name, field| puts "\t#{name}"}
}

But most importantly, the ResultSet contains record objects. Rfm::Result::Record is a subclass of Hash, so it can be used in many standard ways. This code would print the value in the ‘first_name’ field in the first record of the ResultSet:

my_record = my_result[0]
puts my_record["first_name"]

As a convenience, if your field names are valid ruby method names (ie, they don’t have spaces or odd punctuation in them), you can do this instead:

puts my_record.first_name

Since ResultSets are arrays and Records are hashes, you can take advantage of Ruby’s wonderful expressiveness. For example, to get a comma-separated list of the full names of all the people in California, you could do this:

my_layout.find({:state => 'CA'}).collect {|rec| "#{rec.first_name} #{rec.last_name}"}.join(", ")

Record objects can also be edited:

my_record.first_name = 'Isabel'

Once you have made a series of edits, you can save them back to the database like this:

my_record.save

The save operation causes the record to be reloaded from the database, so any changes that have been made outside your script will also be picked up after the save.

If you want to detect concurrent modification, you can do this instead:

my_record.save_if_not_modified

This version will refuse to update the database and raise an error if the record was modified after it was loaded but before it was saved.

Record objects also have portals. While the portals in a ResultSet tell you about the tables and fields the portals show, the portals in a Record have the actual data. For example, if an Order record has Line Item records, you could do this:

my_order = order_layout.any[0]  # the [0] is important!
my_lines = my_order.portals["Line Items"]

At the end of the previous block of code, my_lines is an array of Record objects. In this case, they are the records in the “Line Items” portal for the particular order record. You can then operate on them as you would any other record.

NOTE: Fields on a portal have the table name and the “::” stripped off of their names if they belong to the table the portal is tied to. In other words, if our “Line Items” portal includes a quantity field and a price field, you would do this:

my_lines[0]["Quantity"]
my_lines[0]["Price"]

You would NOT do this:

my_lines[0]["Line Items::Quantity"]
my_lines[0]["Line Items::Quantity"]

My feeling is that the table name is redundant and cumbersome if it is the same as the portal’s table. This is also up for debate.

Again, you can string things together with Ruby. This will calculate the total dollar amount of the order:

total = 0.0
my_order.portals["Line Items"].each {|line| total += line.quantity * line.price}

I intend to add a ‘delete’ method to the Record class as well, but I haven’t done this yet. Finally, should you be able to create new records using the Record class? I’m not sure…lots of points to consider on this.

Data Types

FileMaker’s field types are coerced to Ruby types thusly:

Text Field -> String object
Number Field -> BigDecimal object  # see below
Date Field -> Date object
Time Field -> DateTime object # see below
TimeStamp Field -> DateTime object
Container Field -> URI object

FileMaker’s number field is insanely robust. The only data type in ruby that can handle the same magnitude and precision of a FileMaker number is Ruby’s BigDecimal. (This is an extension class, so you have to require ‘bigdecimal’ to use it yourself). Unfortuantely, BigDecimal is not a “normal” ruby numeric class, so it might be really annoying that your tiny filemaker numbers have to go this route. This is a great topic for debate.

Also, Ruby doesn’t have a Time type that stores just a normal time (with no date attached). The Time class in ruby is a lot like DateTime, or a Timestamp in FileMaker. When I get a Time field from FileMaker, I turn it into a DateTime object, and set its date to the oldest date Ruby supports. You can still compare these in all the normal ways, so this should be fine, but it will look weird if you, ie, to_s one and see an odd date attached to your time.

Finally, container fields will come back as URI objects. You can:

- use Net::HTTP to download the contents of the container field using this URI
- to_s the URI and use it as the src attribute of an HTML image tag
- etc...

Specifically, the URI refers to the contents of the container field. When accessed, the file, picture, or movie in the field will be downloaded.

Troubleshooting

There are two cheesy methods to help track down problems. When you create a server object, you can provide two additional optional parameters:

:log_actions When this is ‘true’ your script will write every URL it sends to the web publishing engine to standard out. For the rails users, this means the action url will wind up in your WEBrick or Mongrel log. If you can’t make sense of what you’re getting, you might try copying the URL into your browser to see what is actually coming back from FileMaker.

:log_responses When this is ‘true’ your script will dump the actual response it got from FileMaker to standard out (again, in rails, check your logs).

So, for an annoying, but detailed load of output, make a connection like this:

my_server = Rfm::Server.new(
  :host => 'myservername',
  :username => 'user',
  :password => 'pw',
  :log_actions => true,
  :log_responses => true
)

These options will change in the future. They’re only there now as a quick-easy way for me to track down problems. I’m open to suggestions for what kind of login options will make sense in the final release.

Copyright © 2007 Six Fried Rice, LLC and Mufaddal Khumr. See LICENSE for details.