Module: DisiidUser::ClassMethods

Defined in:
lib/disiid_user.rb

Overview

Class Methods

Instance Method Summary collapse

Instance Method Details

#build_from_identity_url(url) ⇒ Object Also known as: build_from_identity

Creates a new user object with the identity attribute assigned and returns a not presisted record. You’ll need to call .save or .save! on the returned result if you want to persist it in a local database.

Note: your local :users table should :identity_url string attribute for this to work.



58
59
60
61
62
# File 'lib/disiid_user.rb', line 58

def build_from_identity_url(url)
  user = new
  user.identity_url = url
  user
end

#find_local_or_remote_by_uuid!(id) ⇒ Object

Looks for a local user record with a given UUID by matching its local attribute :identity_url. Creates a new user if it doesn’t exist locally but is present remotely using #build_from_identity method. This method will throw ActiveRecord::RecordNotFound exception if persistance process didn’t go well.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/disiid_user.rb', line 69

def find_local_or_remote_by_uuid!(id)
  user = where('identity_url LIKE ?', "%#{id}").first
  if !user && DisiidUser::RemoteUser.exists?(id, params: {auth_token: DisiidUser::RemoteUser.auth_token})
    # DisiidUser::RemoteUser.site is a URI::HTTP object, not a string
    user_url = DisiidUser::RemoteUser.site.merge("#{DisiidUser::RemoteUser.collection_name}/#{id}").to_s
    user = build_from_identity(user_url)
    user.save!
  end
  user
rescue
  raise ::ActiveRecord::RecordNotFound
end

#remote_count(args = {}) ⇒ Object

Same as #search but returns just an integer, a number of how many search result items the #search method would return (give the same arguments)

User.remote_count(:q => "dude")                    # => 1
User.remote_count(:position => 'technical_staff')  # => 13


47
48
49
50
# File 'lib/disiid_user.rb', line 47

def remote_count(args = {})
  args.merge! :auth_token => DisiidUser::RemoteUser.auth_token
  DisiidUser::RemoteUser.get :count, args
end

#search(args = {}) ⇒ Object

Returns a list of users as ActiveResource objects that match given query params, e.g.

User.search(:q => "dude")            # => [{name: 'dude', ...}, {...}]
User.search(:position => 'whatever)  # => []


38
39
40
41
# File 'lib/disiid_user.rb', line 38

def search(args = {})
  args.merge! :auth_token => DisiidUser::RemoteUser.auth_token
  DisiidUser::RemoteUser.find :all, :params => args
end