class JSONAPI::PathSegment

Public Class Methods

parse(source_resource_klass:, segment_string:, parse_fields: true) click to toggle source
# File lib/jsonapi/path_segment.rb, line 3
def self.parse(source_resource_klass:, segment_string:, parse_fields: true)
  first_part, last_part = segment_string.split('#', 2)
  relationship = source_resource_klass._relationship(first_part)

  if relationship
    if last_part
      unless relationship.resource_types.include?(last_part)
        raise JSONAPI::Exceptions::InvalidRelationship.new(source_resource_klass._type, segment_string)
      end
      resource_klass = source_resource_klass.resource_klass_for(last_part)
    end
    return PathSegment::Relationship.new(relationship: relationship, resource_klass: resource_klass)
  else
    if last_part.blank? && parse_fields
      return PathSegment::Field.new(resource_klass: source_resource_klass, field_name: first_part)
    else
      raise JSONAPI::Exceptions::InvalidRelationship.new(source_resource_klass._type, segment_string)
    end
  end
end