Class: Peddler::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/peddler/content.rb

Overview

Parses content metadata provided in the headers of a response

Instance Method Summary collapse

Instance Method Details

#charsetEncoding?

The character encoding of the response

Returns:

  • (Encoding, nil)


54
55
56
57
58
59
# File 'lib/peddler/content.rb', line 54

def charset
  match_data = headers['Content-Type']&.match(/charset=(.*);?/)
  return unless match_data

  Encoding.find(match_data[1])
end

#lengthString?

The size of the response body in bytes

Returns:

  • (String, nil)


16
17
18
19
20
# File 'lib/peddler/content.rb', line 16

def length
  return unless headers['Content-Length']

  headers['Content-Length'].to_i
end

#md5String?

The MD5 digest of the response body

Returns:

  • (String, nil)


24
25
26
# File 'lib/peddler/content.rb', line 24

def md5
  headers['Content-MD5']
end

#media_typeString?

The MIME type of the response

Returns:

  • (String, nil)


30
31
32
33
34
# File 'lib/peddler/content.rb', line 30

def media_type
  return unless headers['Content-Type']

  headers['Content-Type'].split(';').first
end

#subtypeString?

The exact kind of data of the specified type the MIME type represents

Returns:

  • (String, nil)


46
47
48
49
50
# File 'lib/peddler/content.rb', line 46

def subtype
  return unless media_type

  media_type.split('/').last
end

#typeString?

The general category into which the MIME type falls

Returns:

  • (String, nil)


38
39
40
41
42
# File 'lib/peddler/content.rb', line 38

def type
  return unless media_type

  media_type.split('/').first
end

#xml?Boolean?

Whether the response is an XML document

Returns:

  • (Boolean, nil)


63
64
65
66
67
# File 'lib/peddler/content.rb', line 63

def xml?
  return unless subtype

  subtype == 'xml'
end