Class: HttpLinkHeader::HttpLinkParams

Inherits:
Hash
  • Object
show all
Defined in:
lib/http_link_header/http_link_params.rb

Defined Under Namespace

Classes: RelationTypes

Constant Summary collapse

DELIMETER =
';'
RELATION_TYPE_ATTRIBUTES =
::Set['rel', 'rev']

Instance Method Summary collapse

Constructor Details

#initialize(link_params) ⇒ HttpLinkParams

Returns a new instance of HttpLinkParams.



37
38
39
# File 'lib/http_link_header/http_link_params.rb', line 37

def initialize(link_params)
  add(link_params)
end

Instance Method Details

#[](attribute) ⇒ Object



41
42
43
# File 'lib/http_link_header/http_link_params.rb', line 41

def [](attribute)
  super(attribute.to_s)
end

#[]=(attribute, value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/http_link_header/http_link_params.rb', line 45

def []=(attribute, value)
  if RELATION_TYPE_ATTRIBUTES.include?(attribute.to_s)
    if self[attribute]
      self[attribute].add(value)
    else
      super(attribute.to_s, RelationTypes.new(value))
    end
  else
    super(attribute.to_s, value)
  end
end

#add(link_params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/http_link_header/http_link_params.rb', line 57

def add(link_params)
  link_params.split(DELIMETER).each do |link_param|
    link_param.strip!
    next if link_param.empty?

    if link_param_match = link_param.match(/\A([\w!#\$&\+\-\.\^`\|~]+\*?)\s*(=\s*(.*))?\z/)
      attribute, value = link_param_match.values_at(1, 3)
      self[attribute.strip] = value ? value.strip : nil
    else
      raise ArgumentError, "invalid link-param: #{link_param}"
    end
  end
end

#to_aObject



71
72
73
# File 'lib/http_link_header/http_link_params.rb', line 71

def to_a
  map { |attribute, value| value ? "#{attribute}=#{value.to_s}" : attribute }
end

#to_sObject



75
76
77
# File 'lib/http_link_header/http_link_params.rb', line 75

def to_s
  to_a.join("#{DELIMETER} ")
end