module ShafClient::ResourceMapper
Public Class Methods
default=(clazz)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 26 def default=(clazz) mapping.default = clazz end
for(content_type:, headers: {}, payload: nil, client: nil)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 8 def for(content_type:, headers: {}, payload: nil, client: nil) content_type = content_type&.to_sym profile = profile_from(content_type, headers, payload) clazz, extensions = result_for(content_type, payload, profile, client) raise_unsupported_error(content_type) unless clazz [clazz, extensions] end
register(content_type, profile = nil, clazz)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 18 def register(content_type, profile = nil, clazz) mapping[content_type&.to_sym, profile] = clazz end
unregister(content_type, profile = nil)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 22 def unregister(content_type, profile = nil) mapping.delete(content_type.to_sym, profile) end
Private Class Methods
extensions_for(clazz, profile, payload, client)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 81 def extensions_for(clazz, profile, payload, client) return [] unless clazz && profile && client profile_resource = fetch_profile(profile, client) link_relations = clazz.new(nil, payload).actions if payload ResourceExtension.for(profile_resource, clazz, link_relations, client) rescue StandardError => err warn "Exception while resolving extensions for profile " \ "#{profile_resource&.name || profile}: #{err}" [] end
fetch_profile(profile, client)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 94 def fetch_profile(profile, client) return unless profile&.start_with? %r{https?://} client.get(profile) end
mapping()
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 32 def mapping @mapping ||= ContentTypeMap.new end
profile_from(content_type, headers, payload)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 52 def profile_from(content_type, headers, payload) profile_from_content_type(content_type) || profile_from_link_header(headers) || profile_from_payload_link(content_type, payload) rescue StandardError => err warn "Exception while looking up profile link relation: #{err}" end
profile_from_content_type(content_type)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 60 def profile_from_content_type(content_type) return unless content_type content_type[/profile="?([^"\s;]*)/, 1] end
profile_from_link_header(headers)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 66 def profile_from_link_header(headers) links = String(headers["link"] || headers["Link"]).split(',') profile_link = links.find { |link| link.match?(/rel="?profile"?/) } profile_link[/<(.*)>/, 1] if profile_link end
profile_from_payload_link(content_type, payload)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 72 def profile_from_payload_link(content_type, payload) clazz = mapping[content_type] resource = clazz&.new(nil, payload) return unless resource.respond_to? :link link = resource.link(:profile) { nil } link&.href end
raise_unsupported_error(content_type)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 100 def raise_unsupported_error(content_type) raise UnSupportedContentType, "Can't handle Content-Type: #{content_type}" end
result_for(content_type, payload, profile, client)
click to toggle source
# File lib/shaf_client/resource_mapper.rb, line 36 def result_for(content_type, payload, profile, client) clazz = nil extensions = [] # Note: mapping typically has a default value, so we need to check that the key really exist if mapping.key? content_type, profile # Registered classes with profile takes precedence over linked profiles clazz = mapping[content_type, profile] else clazz = mapping[content_type] extensions = extensions_for(clazz, profile, payload, client) if profile end [clazz, extensions] end