Class: Versionist::VersioningStrategy::Header

Inherits:
Base
  • Object
show all
Defined in:
lib/versionist/versioning_strategy/header.rb

Overview

Implements the header versioning strategy.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Header

Creates a new Header VersioningStrategy object. config must contain the following keys:

  • :header the header hash to inspect

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
# File 'lib/versionist/versioning_strategy/header.rb', line 8

def initialize(config)
  super
  raise ArgumentError, "you must specify :name in the :header configuration Hash" if !config[:header].has_key?(:name)
  raise ArgumentError, "you must specify :value in the :header configuration Hash" if !config[:header].has_key?(:value)
  raise ArgumentError, "a header :name of 'version' will clash with the built in rack header \"HTTP_VERSION\". You must use a different header name" if config[:header][:name].downcase == "version"
  Versionist.configuration.header_versions << self if !Versionist.configuration.header_versions.include?(self)
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
39
40
# File 'lib/versionist/versioning_strategy/header.rb', line 36

def ==(other)
  super
  return false if !other.is_a?(Versionist::VersioningStrategy::Header)
  return self.config[:header][:name] == other.config[:header][:name] && self.config[:header][:value] == other.config[:header][:value]
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/versionist/versioning_strategy/header.rb', line 16

def matches?(request)
  header_string = request.headers[config[:header][:name]].to_s
  if !header_string.blank?
    potential_matches = Versionist.configuration.header_versions.select {|hv| header_string.include?(hv.config[:header][:value])}
    if !potential_matches.empty?
      if potential_matches.include?(self)
        if potential_matches.size == 1
          return true
        else
          # when finding multiple potential matches, the match with the longest value wins
          #  (i.e. v2.1 trumps v2), as one is a subset of the other
          longest = potential_matches.max {|a,b| a.config[:header][:value].length <=> b.config[:header][:value].length}
          return longest == self
        end
      end
    end
  end
  false
end