Module: USCoreTestKit::GranularScope

Included in:
GranularScopeReadTest, GranularScopeSearchTest
Defined in:
lib/us_core_test_kit/granular_scope.rb

Instance Method Summary collapse

Instance Method Details

#granular_scope_search_paramsObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/us_core_test_kit/granular_scope.rb', line 15

def granular_scope_search_params
  @granular_scope_search_params ||=
    granular_scopes.map do |scope|
      _, granular_scope = scope.split('?')
      name, value = granular_scope.split('=')

      {
        name:,
        value:
      }
    end
end

#granular_scopesObject



5
6
7
8
9
10
11
12
13
# File 'lib/us_core_test_kit/granular_scope.rb', line 5

def granular_scopes
  @granular_scopes ||=
    received_scopes
      .split(' ')
      .select do |scope|
        (scope.start_with?("patient/#{resource_type}") || scope.start_with?("user/#{resource_type}")) &&
          scope.include?('?')
      end
end

#previous_request_resourcesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/us_core_test_kit/granular_scope.rb', line 60

def previous_request_resources
  first_request = previous_requests.first
  next_page_url = nil
  hash = Hash.new { |hash, key| hash[key] = [] }
  previous_requests.each_with_object(hash) do |request, request_resource_hash|
    request_resources =
      if request.status == 200
        request.resource.entry.map(&:resource).select { |resource| resource.resourceType == resource_type }
      else
        []
      end

    # Check if current request URL matches the next page URL from the previous request
    # If not, this is a new search, so update first_request
    resolved_next_page_url = resolve_url(next_page_url)
    first_request = request if request.url != resolved_next_page_url

    request_resource_hash[first_request].concat(request_resources)

    # Extract the next page URL from the current request's bundle for the next iteration
    next if request.resource&.resourceType != 'Bundle'

    next_page_url = request.resource&.link&.find { |link| link.relation == 'next' }&.url
  end
end

#resolve_url(url_string) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/us_core_test_kit/granular_scope.rb', line 41

def resolve_url(url_string)
  return nil if url_string.nil?
  
  uri = URI.parse(url_string)
  # If already absolute, return as-is
  return url_string if uri.absolute?
  
  # If relative, resolve against the configured FHIR base URL
  # Per FHIR spec, relative URLs are interpreted relative to the FHIR base URL
  # Strip leading slash from relative URL if present to prevent URI.join from
  # treating it as absolute path from root, and ensure base has trailing slash
  base_with_slash = url.end_with?('/') ? url : "#{url}/"
  relative_without_leading_slash = url_string.start_with?('/') ? url_string[1..-1] : url_string
  resolved = URI.join(base_with_slash, relative_without_leading_slash)
  resolved.to_s
rescue URI::InvalidURIError
  url_string
end

#resource_specific_granular_scope_search_paramsObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/us_core_test_kit/granular_scope.rb', line 28

def resource_specific_granular_scope_search_params
  @resource_specific_granular_scope_search_params ||=
    granular_scopes.select {|scope| scope.include?(resource_type)}.map do |scope|
      _, granular_scope = scope.split('?')
      name, value = granular_scope.split('=')
      _, value = value.split('|')
      {
        name:,
        value:
      }
  end
end