Class: Lotus::Model::Adapters::Auth0::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/model/adapters/auth0/collection.rb

Constant Summary collapse

InvalidCollectionName =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_mapped_collection, _name, _identity) ⇒ Collection

Returns a new instance of Collection.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lotus/model/adapters/auth0/collection.rb', line 13

def initialize(_mapped_collection, _name, _identity)
  raise InvalidCollectionName unless allowed_collection_names.include?(_name.to_s)

  @client_token  = ENV['AUTH0_TOKEN']
  @client_domain = ENV['AUTH0_DOMAIN']

  @client = Auth0Client.new(
    api_version: 2,
    token: @client_token,
    domain: @client_domain
  )

  @mapped_collection = _mapped_collection
  @name = _name
  @identity = _identity
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/lotus/model/adapters/auth0/collection.rb', line 9

def client
  @client
end

Instance Method Details

#delete(entity) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/lotus/model/adapters/auth0/collection.rb', line 79

def delete(entity)
  deleted_entity  = case @name
  when :users
    id = entity.delete(identity)
    @client.delete_user(id)
  when :connections
    id = entity.delete(identity)
    @client.delete_connection(id)
  end
end

#insert(entity) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lotus/model/adapters/auth0/collection.rb', line 53

def insert(entity)
  inserted_entity  = case @name
  when :users
    entity.delete(:identities)
    @client.create_user(entity[:name], entity)
  when :connections
    @client.create_connection(entity)
  end

  inserted_entity
end

#search(query, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lotus/model/adapters/auth0/collection.rb', line 30

def search(query, options = {})
  results = case @name
  when :users
    # def users( per_page: nil, page: nil, include_totals: nil, sort: nil, connection: nil, fields: nil, q: nil )
    @client.users(q: query.to_s, page: options[:offset], per_page: options[:limit])
  when :connections
    results = @client.connections
    query.conditions.each do |condition|
      case condition.type
      when :where
        results.reject! { |result| result[condition.key.to_s] != condition.value }
        results
      when :or
        raise "#{@name} does not support this query condition"
      end
    end
    results
  else
    raise NotImplementedError, 'This collection can not be searched'
  end
  results
end

#truncateObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/lotus/model/adapters/auth0/collection.rb', line 90

def truncate
  case @name
  when :users
    @client.delete_users
  when :connections
    @client.connections.each do |connection|
      @client.delete_connection(connection['id'])
    end
  end
end

#update(entity) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lotus/model/adapters/auth0/collection.rb', line 65

def update(entity)
  updated_entities  = case @name
  when :users
    # We can't modify these
    entity.delete(:identities)
    entity.delete(:name)

    id = entity.delete(identity)
    @client.patch_user(id, entity)
  end

  updated_entities
end