Class: ApiHammer::ContentTypeAttrs

Inherits:
Object
  • Object
show all
Defined in:
lib/api_hammer/faraday/request_logger.rb

Overview

parses attributes out of content type header

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type) ⇒ ContentTypeAttrs

Returns a new instance of ContentTypeAttrs.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/api_hammer/faraday/request_logger.rb', line 10

def initialize(content_type)
  @media_type = content_type.split(/\s*[;]\s*/, 2).first if content_type
  @media_type.strip! if @media_type
  @content_type = content_type
  @parsed = false
  @attributes = Hash.new { |h,k| h[k] = [] }
  catch(:unparseable) do
    throw(:unparseable) unless content_type
    uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
    scanner = StringScanner.new(content_type)
    scanner.scan(/.*;\s*/) || throw(:unparseable)
    while match = scanner.scan(/(\w+)=("?)([^"]*)("?)\s*(,?)\s*/)
      key = scanner[1]
      quote1 = scanner[2]
      value = scanner[3]
      quote2 = scanner[4]
      comma_follows = !scanner[5].empty?
      throw(:unparseable) unless quote1 == quote2
      throw(:unparseable) if !comma_follows && !scanner.eos?
      @attributes[uri_parser.unescape(key)] << uri_parser.unescape(value)
    end
    throw(:unparseable) unless scanner.eos?
    @parsed = true
  end
end

Instance Attribute Details

#media_type ⇒ Object (readonly)

Returns the value of attribute media_type.



36
37
38
# File 'lib/api_hammer/faraday/request_logger.rb', line 36

def media_type
  @media_type
end

Instance Method Details

#[](key) ⇒ Object



42
43
44
# File 'lib/api_hammer/faraday/request_logger.rb', line 42

def [](key)
  @attributes[key]
end

#parsed? ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/api_hammer/faraday/request_logger.rb', line 38

def parsed?
  @parsed
end