[Beta] RemoteAttrAccessor¶ ↑
Purpose¶ ↑
Get remote server’s attributes and set them into local attributes via API.
For example : Micorservice’s user infomation¶ ↑
User’s infomation will be used by other servers. So, prepare remote(provider) server for common user infomation provider, and Other servers(client) use ‘remote_attr_accessor’ to get common infomation of users.
Settings¶ ↑
Set Config¶ ↑
Make file in “lib/remote_attr_accessor/config.rb” to Overwrite for your environment.
module RemoteAttrAccessor class Config def self.id_name 'id' end def self.prefix 'remote_' end def self.remote_json_key 'users' end def self.remote_attrs [:email, :last_name, :first_name] end def self.remote_attrs_with_prefix @remote_attrs_with_prefix = remote_attrs.map{|attr| "#{prefix}#{attr}"} end end end
Set API¶ ↑
Make file in “lib/remote_attr_accessor/api.rb” to Overwrite for your environments.
module RemoteAttrAccessor
class Api def self.get_remote_attrs(ids) # Write here API access code end def self.upsert_remote_attr(params) # Write here API access code end end
end
Response format of RemoteAttrAccessor::API.get_remote_attrs¶ ↑
{ "users": { "1": { "id": 1, "email": "kenjiszk@example.com", "last_name": szk, "first_name": kenji, "created_at": "2016-05-31T18:04:05.000Z", "updated_at": "2016-06-03T05:37:51.000Z" }, "2": { "id": 2, "email": "sample@example.com", "last_name": ple, "first_name": sam, "created_at": "2016-05-31T07:51:10.000Z", "updated_at": "2016-05-31T07:51:10.000Z" } } }
How to get/set remote attribute¶ ↑
Use sample for “User model”.
users = User.limit(1) user.first.remote_email # Get users email from remote server user.first.remote_email = 'test@example.com' # Set users email to remote server
Bulk get from remote attribute¶ ↑
users = User.limit(1) RemoteAttrAccessor::Bulk.load_remote_attr(users) # Get users info in bulk and set instance variable user.first.remote_email # Not access to remote server
This project rocks and uses MIT-LICENSE.