Class: JsonCrudApi::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/json-crud-api/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
# File 'lib/json-crud-api/service.rb', line 8

def initialize(options)
  @log_service = options[:log_service]
  @model = options[:model]
  @scope_map = options[:scope_map]
  @user = nil
  @user_scopes = nil
end

Instance Attribute Details

#log_serviceObject

Returns the value of attribute log_service.



6
7
8
# File 'lib/json-crud-api/service.rb', line 6

def log_service
  @log_service
end

#modelObject

Returns the value of attribute model.



6
7
8
# File 'lib/json-crud-api/service.rb', line 6

def model
  @model
end

#scope_mapObject

Returns the value of attribute scope_map.



6
7
8
# File 'lib/json-crud-api/service.rb', line 6

def scope_map
  @scope_map
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/json-crud-api/service.rb', line 6

def user
  @user
end

#user_scopesObject

Returns the value of attribute user_scopes.



6
7
8
# File 'lib/json-crud-api/service.rb', line 6

def user_scopes
  @user_scopes
end

Instance Method Details

#create(params) ⇒ Object

Create a record with the given attributes



17
18
19
# File 'lib/json-crud-api/service.rb', line 17

def create(params)
  @model.create(params)
end

#delete(key) ⇒ Object

Delete a record with the given key Returns false if the record does not exist.



47
48
49
50
51
52
# File 'lib/json-crud-api/service.rb', line 47

def delete(key)
  record = get(key)
  return false if record.nil?

  record.destroy
end

#exists?(key) ⇒ Boolean

Determine if a record with the given key exists

Returns:

  • (Boolean)


22
23
24
# File 'lib/json-crud-api/service.rb', line 22

def exists?(key)
  @model.all(@model.key.first.name => key).count > 0
end

#get(key) ⇒ Object

Get the first record with the given key



32
33
34
# File 'lib/json-crud-api/service.rb', line 32

def get(key)
  @model.first(@model.key.first.name => key)
end

#get_allObject

Get all records



27
28
29
# File 'lib/json-crud-api/service.rb', line 27

def get_all
  @model.all
end

#set_user(user) ⇒ Object

Set the current user



55
56
57
58
# File 'lib/json-crud-api/service.rb', line 55

def set_user(user)
  @user = user
  set_user_scopes(user[:scopes]) unless @user.nil?
end

#set_user_scopes(user_scopes) ⇒ Object

Set the current user scopes



61
62
63
# File 'lib/json-crud-api/service.rb', line 61

def set_user_scopes(user_scopes)
  @user_scopes = user_scopes
end

#update(key, params) ⇒ Object

Update a record with the given key with the given attributes Returns false if the record does not exist.



38
39
40
41
42
43
# File 'lib/json-crud-api/service.rb', line 38

def update(key, params)
  record = get(key)
  return false if record.nil?

  record.update(params)
end

#user_authorized_for?(operation) ⇒ Boolean

Determine if the current user is authorized for the given operation

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/json-crud-api/service.rb', line 66

def user_authorized_for?(operation)
  # Auth is disabled if scope map is nil
  return true if @scope_map.nil?
  # Auth succeeds if there is no map for this operation
  return true if @scope_map[operation].nil?
  # Auth fails if user is not logged in
  return false if @user.nil?
  # Auth fails if user has no scopes
  return false if @user_scopes.nil? or @user_scopes.empty?

  if @scope_map[operation].is_a?(Array)
    # Auth succeeds if the intersection of allowed scopes and mapped scopes is non-empty.
    return !((@scope_map[operation] & @user_scopes).empty?)
  end

  # Auth succeeds if the mapped scope is singular and the user posesses it
  @user_scopes.include?(@scope_map[operation])
end