Class: InternetMessage::ContentType

Inherits:
Object
  • Object
show all
Defined in:
lib/internet_message/content_type.rb

Constant Summary collapse

TOKEN_RE =
/[0-9a-zA-Z\!\#\$\%\&\'\*\+\-\.\^\_\`\{\|\}\~]+/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, subtype, attribute = {}) ⇒ ContentType

Returns a new instance of ContentType.

Parameters:

  • type (String)
  • subtype (String)
  • attribute (Hash) (defaults to: {})


27
28
29
# File 'lib/internet_message/content_type.rb', line 27

def initialize(type, subtype, attribute={})
  @type, @subtype, @attribute = type.downcase, subtype.downcase, attribute
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



22
23
24
# File 'lib/internet_message/content_type.rb', line 22

def attribute
  @attribute
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



22
23
24
# File 'lib/internet_message/content_type.rb', line 22

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/internet_message/content_type.rb', line 22

def type
  @type
end

Class Method Details

.parse(src) ⇒ ContentType

Parameters:

Returns:



11
12
13
14
15
16
17
18
19
20
# File 'lib/internet_message/content_type.rb', line 11

def self.parse(src)
  tokens = src.is_a?(String) ? Tokenizer.new(src, :token_re=>TOKEN_RE).tokenize : src.dup
  tokens.delete_if{|t| t.type == :WSP or t.type == :COMMENT}
  unless tokens.size >= 3 && tokens[0].type == :TOKEN && tokens[1].value == '/' && tokens[2].type == :TOKEN
    return nil
  end
  type, subtype = tokens[0].value, tokens[2].value
  tokens.shift 3
  ContentType.new(type, subtype, ContentAttribute.parse_attribute(tokens))
end

Instance Method Details

#==(other) ⇒ true, false

Compare self and other

Parameters:

Returns:

  • (true, false)


34
35
36
# File 'lib/internet_message/content_type.rb', line 34

def ==(other)
  other.is_a?(ContentType) && other.type == self.type && other.subtype == self.subtype && other.attribute == self.attribute
end