mardi 13 novembre 2018

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.

Aucun commentaire:

Enregistrer un commentaire