views:

34

answers:

0

This may seem a bit subjective but I am also looking for specifics on implementation if possible. The existing popular how-to's on creating plugins seem to assume you want to get into the guts ActiveRecord itself, or write an acts_as_* plugin, which is not really the case here.

The requirement is to connect to a non-RESTful API at a 3rd party for the provisioning of data storage and transfer. Similar to Amazon S3 sort of, but based just on vanilla POSTs and parsing XML replies.

right now I am trying to build all the API calls into a plugin module which has methods for each API call and is mixed into the models that require it (only 2 out of dozens, so I don't want to extend ActiveRecord)

# vendor/plugins/bigfarm/lib/bigfarm.rb
module BigFarm

    def bf_connect_ingest
        { :uri => 'http://example.com/i8xsi2q7/' }
    end

    def bf_fetch_url
        # hardcoded urls for testing.
        'http://cdn.example.com/i8xsi2q7/' + @file.id + '/' + @file.name
    end

    def bf_store
        @file.name
        # do api stuff 
    end

    def bf_get_usage(@user)
        # more api stuff
    end
end

# app/models/files.rb
class File < ActiveRecord::Base
  include BigFarm
  belongs_to :user

  def store
    if bf_get_usage < user.max_storage_bytes
       # do stuff
       bf_store
    end
  end
end

So this works, but the question I have is, how to improve the namespace situation (prefixing methods with 'bf_' does not enforce). Also if we are to move from BigFarm, to using S3 buckets, for instance, it would be nice to have to only switch out the plugin, and make no changes to anything under app/.