Search code examples
ruby-on-railsdesign-patternssoa

Model data sharing in rails using http


I have two rails app. Let's say App1 and App2. App1 has a model which is the core of my business and App2 wanted to use that.

One approach I was thinking was to create PORO which mimics ActiveRecord functionality but instead of interacting with the DB, it will interact with App1 over HTTP or https.

For example:

class User < NetworkRecord
  def self.find(id)
    new.find(id)
  end

  def find(id)
    endpoint = 'https:app1.com/user'
    get(endpoint, { id: id })
  end
end


class NetworkRecord
  def get(endpoint, params)
    Httparty.get(endpoint, params)
  end
end

I am not sure whether this approach is too intelligent or right, because we are giving some functionality of ActiveModel to an Object without inheriting from it.

If you know any better way to achieve the same thing, please let me know.


Solution

  • It feels like you're thinking along the same lines as ActiveResource, which provides an ActiveRecord-like interface to objects across a REST interface.

    If you're intending to keep the two codebases entirely separate, then some form of API-based relationship is probably the way to go. One thing you do have to be careful with is how much network traffic you generate with this approach. If these two apps are going to be the only users of the API you build, you have an opportunity to build more customised endpoints based on your use cases that can deliver the data you need as efficiently as possible.