class Apiphobic::AcceptHeader

Attributes

application_name[R]
raw_accept_header[RW]

Public Class Methods

new(header, application_name:) click to toggle source
# File lib/apiphobic/accept_header.rb, line 8
def initialize(header, application_name:)
  self.application_name  = application_name
  self.raw_accept_header = header || ''
end

Public Instance Methods

application_name=(other) click to toggle source
# File lib/apiphobic/accept_header.rb, line 13
def application_name=(other)
  @accept_header_data   = nil
  @accept_header_format = nil

  @application_name     = other
end
content_type() click to toggle source
# File lib/apiphobic/accept_header.rb, line 24
def content_type
  accept_header_data[1]
end
invalid?() click to toggle source
# File lib/apiphobic/accept_header.rb, line 32
def invalid?
  accept_header_data.nil?
end
to_s() click to toggle source
# File lib/apiphobic/accept_header.rb, line 36
def to_s
  raw_accept_header
end
valid?() click to toggle source
# File lib/apiphobic/accept_header.rb, line 28
def valid?
  !invalid?
end
version() click to toggle source
# File lib/apiphobic/accept_header.rb, line 20
def version
  accept_header_data[2]
end

Private Instance Methods

accept_header_data() click to toggle source
# File lib/apiphobic/accept_header.rb, line 42
def accept_header_data
  @accept_header_data ||= raw_accept_header.match(accept_header_format)
end
accept_header_format() click to toggle source
# File lib/apiphobic/accept_header.rb, line 46
def accept_header_format
  @accept_header_format ||= %r{
    ################################################################################
    # BEGINNING
    ################################################################################

    (?:
      (?<=\A)                       # the beginning of the accept header
      |                             # or
      (?<=,)                        # the beginning of an accept value
    )

    ################################################################################
    # APPLICATION NAME
    ################################################################################

    application/vnd\.#{application_name} # the application vendor string

    ################################################################################
    # SUBRESOURCE
    ################################################################################

    (?:
      \+                            # a literal '+'
      (\w+)                         # the subresource name
    )?

    ################################################################################
    # VERSION SECTION
    ################################################################################

    (?:
      ;                             # a literal ';'

      version=                      # a literal 'version='
      (
                                    # VERSION DIGIT GROUPINGS SECTION

        \d+                         # one or more digits
        (?:\.\d+){0,2}              # one or two groups of digits with preceeding '.'

                                    # VERSION BETA SECTION
        (?:
          beta                      # a literal 'beta'
          (?:\d*)                   # a optional beta version
        )?
      )
    )?

    ################################################################################
    # END
    ################################################################################

    (?:
      (?=\z)                        # the end of the accept header
      |                             # or
      (?=,)                         # the end of an accept value
    )
  }x
end