class Roar::JSON::JSONAPI::MemberName

Member Name formatting according to the JSON API specification.

@see jsonapi.org/format/#document-member-names @since 0.1.0

Constants

LENIENT_FILTER_REGEXP

@api private

STRICT_FILTER_REGEXP

@api private

Public Class Methods

call(name, options = {}) click to toggle source

@see call

# File lib/roar/json/json_api/member_name.rb, line 17
def self.call(name, options = {})
  new.(name, options)
end

Public Instance Methods

call(name, options = {}) click to toggle source

Format a member name

@param [String, Symbol] name

member name.

@option options [Boolean] :strict

whether strict mode is enabled.

Strict mode applies additional JSON Specification *RECOMMENDATIONS*,
permitting only non-reserved, URL safe characters specified in RFC 3986.
The member name will be lower-cased and underscores will be
transformed to hyphens.

Non-strict mode permits:
* non-ASCII alphanumeric Unicode characters.
* spaces, underscores and hyphens, except as the first or last character.

@return [String] formatted member name.

@api public

# File lib/roar/json/json_api/member_name.rb, line 40
def call(name, options = {})
  name    = name.to_s
  strict  = options.fetch(:strict, true)
  if strict
    name.downcase!
    name.gsub!(STRICT_FILTER_REGEXP, ''.freeze)
  else
    name.gsub!(LENIENT_FILTER_REGEXP, ''.freeze)
  end
  name.gsub!(/\A([-_ ])/, '')
  name.gsub!(/([-_ ])\z/, '')
  name.tr!('_', '-') if strict
  name
end