posts/gets to other servers inside of action?

Hi all,

We are in a 'web-service-like' scenario in which a controller action
needs to save some things to the database and issue a couple of
POSTS/GETS to other servers. These aren't web services, per se, but
rather perl scripts on a wiki. My solution feels clunky, and I wondered
if there's a cleaner way.

class AccountController
  require 'net/http'
  require 'uri'

  def create
    # Create account in DB
    ...
    account.save!

    # Create account page on wiki
    wiki_url = URI.parse(‘http://ourwiki/save_page’)
    result = Net::HTTP.post_form(wiki_url, {:page_name => account.name +
‘Page’})
    raise “Couldn’t create” unless response.body =~ /Welcome to
#{account.name}/

    redirect_to :action => 'show', :id => account.id
  end
end

Thanks.

Brian

Brian Hartin wrote:

Hi all,

We are in a 'web-service-like' scenario in which a controller action
needs to save some things to the database and issue a couple of
POSTS/GETS to other servers. These aren't web services, per se, but
rather perl scripts on a wiki. My solution feels clunky, and I wondered
if there's a cleaner way.

class AccountController
  require 'net/http'
  require 'uri'

  def create
    # Create account in DB
    ...
    account.save!

    # Create account page on wiki
    wiki_url = URI.parse(‘http://ourwiki/save_page’)
    result = Net::HTTP.post_form(wiki_url, {:page_name => account.name +
‘Page’})
    raise “Couldn’t create” unless response.body =~ /Welcome to
#{account.name}/

    redirect_to :action => 'show', :id => account.id
  end
end

First, I would move this to the model.

  after_save :create_wiki_page
  def create_wiki_page
    Net::Http.post_form(...)
  end

And if the interface got to be more than this one point of connection, I
would move it to an API library

  module WikiApi
    def self.create_user_page(user)
      ..
    end

    def self.create_project_page(user, project)
      ...
    end
  end

  # in the model
  after_save :create_wiki_page
  def create_wiki_page
    WikiApi.create_user_page(self)
  end

  def create_project(project_attributes)
    p = user.projects.create(project_attributes)
    WikiApi.create_project_page(user, p)
  end

Or something...