Class: DataAPI::Request

Inherits:
Object
  • Object
show all
Defined in:
app/models/data_api/request.rb

Overview

DataAPI request value object

Initialized by the requested path, method and access token, then we can get data of the request (e.g.: the scoped resource) form this object.

Caution: this will not verify requests, but only parses it and return the corresponding information. Verification, such as checking the validity of the access token, should be done in the controller.

Constant Summary collapse

MAX_MULTIGETTABLE_ITEMS =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, access_token: nil, include_inaccessible: false, include_not_public: false) ⇒ Request

Returns a new instance of Request.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/data_api/request.rb', line 17

def initialize(path, access_token: nil, include_inaccessible: false, include_not_public: false)
  @full_path = path
  @path = path
  @include_inaccessible = include_inaccessible
  @include_not_public = include_not_public  # remove format extension in path

  @path.slice!(%r{\..+$})  # remove versioning in path

  @path.slice!(%r{^v[0-9]{1,2}\/})

  # Is the requested resource scoped under a user?
  if @path.match(%r{^me\/})
    @scoped_under_user = true
    @resource_path = @path.gsub(%r{^me\/}, '')
  else
    @scoped_under_user = false
    @resource_path = @path
  end

  access_token = Doorkeeper::AccessToken.by_token(access_token) if access_token.is_a?(String)
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token


12
13
14
# File 'app/models/data_api/request.rb', line 12

def access_token
  @access_token
end

#full_pathObject (readonly)

Returns the value of attribute full_path


12
13
14
# File 'app/models/data_api/request.rb', line 12

def full_path
  @full_path
end

#pathObject (readonly)

Returns the value of attribute path


12
13
14
# File 'app/models/data_api/request.rb', line 12

def path
  @path
end

#request_methodObject (readonly)

Returns the value of attribute request_method


12
13
14
# File 'app/models/data_api/request.rb', line 12

def request_method
  @request_method
end

#resource_pathObject (readonly)

Returns the value of attribute resource_path


12
13
14
# File 'app/models/data_api/request.rb', line 12

def resource_path
  @resource_path
end

#scoped_under_userObject (readonly) Also known as: scoped_under_user?

Returns the value of attribute scoped_under_user


12
13
14
# File 'app/models/data_api/request.rb', line 12

def scoped_under_user
  @scoped_under_user
end

#scoped_userObject (readonly)

Returns the user that this request is scoped under of


52
53
54
# File 'app/models/data_api/request.rb', line 52

def scoped_user
  @scoped_user
end

Instance Method Details

#data_apiObject

Returns the corresponding DataAPI


41
42
43
44
# File 'app/models/data_api/request.rb', line 41

def data_api
  return @data_api if @data_api
  @data_api = DataAPI.find_by_path(resource_path, include_not_public: (scoped_under_user? || @include_not_public), include_inaccessible: @include_inaccessible)
end

#present?Boolean

Does the corresponding DataAPI presents?

Returns:


47
48
49
# File 'app/models/data_api/request.rb', line 47

def present?
  !!data_api
end

#resource_collectionObject

Returns the scoped requested resource collection


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/data_api/request.rb', line 59

def resource_collection
  return @resource_collection if @resource_collection

  @resource_collection = data_api.data_model.all

  # scope the collection to the current user if needed
  if scoped_under_user? && scoped_user.present?
    case data_api.owner_primary_key
    when 'uid'
      @resource_collection = @resource_collection.none if \
        scoped_user.organization_code != data_api.organization_code
      @resource_collection = @resource_collection.where(
        data_api.owner_foreign_key => scoped_user.try(data_api.owner_primary_key)
      )
    else
      @resource_collection = @resource_collection.where(
        data_api.owner_foreign_key => scoped_user.try(data_api.owner_primary_key)
      )
    end
  elsif scoped_under_user? && scoped_user.blank?
    @resource_collection = @resource_collection.none
  end

  @resource_collection
end

#resource_specified?Boolean

Is this request for specified resource? e.g.: '/resources/20' or '/resources/1,5,7'

Returns:


87
88
89
# File 'app/models/data_api/request.rb', line 87

def resource_specified?
  !!data_api.specified_resource_ids
end

#specified_resourceObject Also known as: specified_resources

Returns the specified requested resource


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/data_api/request.rb', line 105

def specified_resource
  return @requested_resource if @requested_resource_processed
  @requested_resource_processed = true

  if data_api.specified_resource_ids.present?    # multigettable, a request path can be like this: '/resources/1,5,7'

    ids = specified_resource_ids

    if ids.count > 1
      @requested_resource = resource_collection.where(data_api.primary_key => ids)
    else
      @requested_resource = resource_collection.find_by(data_api.primary_key => ids[0])
    end

    if @requested_resource.blank? && data_api.primary_key != 'id'
      if ids.count > 1
        @requested_resource = resource_collection.where(id: ids)
      else
        @requested_resource = resource_collection.find_by(id: ids[0])
      end
    end

    @requested_resource
  end
end

#specified_resource_idObject

Returns the single specified requested resource id


92
93
94
# File 'app/models/data_api/request.rb', line 92

def specified_resource_id
  data_api.specified_resource_ids
end

#specified_resource_idsObject

Returns the specified requested resource ids in an array, multigettable, a request path can be like this: '/resources/1,5,7'


98
99
100
101
102
# File 'app/models/data_api/request.rb', line 98

def specified_resource_ids
  ids = data_api.specified_resource_ids.split(',')
  ids = ids[0..(MAX_MULTIGETTABLE_ITEMS - 1)]
  ids
end