Sha256: 02a51e0598acc5ac316a6137e2416193c235dfb031c3e429a7cef0be0b90547d

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

# encoding=utf-8

module Roar
  module JSON
    module JSONAPI
      # Member Name formatting according to the JSON API specification.
      #
      # @see http://jsonapi.org/format/#document-member-names
      # @since 0.1.0
      class MemberName
        # @api private
        LENIENT_FILTER_REGEXP = /([^[:alnum:][-_ ]]+)/
        # @api private
        STRICT_FILTER_REGEXP  = /([^[0-9a-z][-_]]+)/

        # @see #call
        def self.call(name, options = {})
          new.(name, options)
        end

        # 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
        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
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roar-jsonapi-0.0.3 lib/roar/json/json_api/member_name.rb
roar-jsonapi-0.0.2 lib/roar/json/json_api/member_name.rb
roar-jsonapi-0.0.1 lib/roar/json/json_api/member_name.rb