Module: Peddler::Headers

Defined in:
lib/peddler/headers.rb

Overview

Parses useful metadata returned in response headers

Instance Method Summary collapse

Instance Method Details

#content_charsetEncoding?

The character encoding of the response

Returns:

  • (Encoding, nil)


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

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

  Encoding.find(match_data[1])
end

#content_lengthString?

The size of the response body in bytes

Returns:

  • (String, nil)


8
9
10
11
12
# File 'lib/peddler/headers.rb', line 8

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

  headers['Content-Length'].to_i
end

#content_md5String?

The MD5 digest of the response body

Returns:

  • (String, nil)


16
17
18
# File 'lib/peddler/headers.rb', line 16

def content_md5
  headers['Content-MD5']
end

#content_media_typeString?

The MIME type of the response

Returns:

  • (String, nil)


22
23
24
25
26
# File 'lib/peddler/headers.rb', line 22

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

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

#content_subtypeString?

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

Returns:

  • (String, nil)


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

def content_subtype
  return unless content_media_type

  content_media_type.split('/').last
end

#content_typeString?

The general category into which the MIME type falls

Returns:

  • (String, nil)


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

def content_type
  return unless content_media_type

  content_media_type.split('/').first
end

#mws_quota_maxInteger?

The max hourly request quota for the requested operation

Returns:

  • (Integer, nil)


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

def mws_quota_max
  return unless headers['x-mws-quota-max']

  headers['x-mws-quota-max'].to_i
end

#mws_quota_remainingInteger?

The remaining hourly request quota for the requested operation

Returns:

  • (Integer, nil)


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

def mws_quota_remaining
  return unless headers['x-mws-quota-remaining']

  headers['x-mws-quota-remaining'].to_i
end

#mws_quota_resets_onTime?

When the hourly request quota for the requested operation resets

Returns:

  • (Time, nil)


71
72
73
74
75
# File 'lib/peddler/headers.rb', line 71

def mws_quota_resets_on
  return unless headers['x-mws-quota-resetsOn']

  Time.parse(headers['x-mws-quota-resetsOn'])
end

#mws_request_idString?

The ID of the request

Returns:

  • (String, nil)


79
80
81
# File 'lib/peddler/headers.rb', line 79

def mws_request_id
  headers['x-mws-request-id']
end

#mws_response_contextString?

The context of the response

Returns:

  • (String, nil)


93
94
95
# File 'lib/peddler/headers.rb', line 93

def mws_response_context
  headers['x-mws-response-context']
end

#mws_timestampTime?

The timestamp of the request

Returns:

  • (Time, nil)


85
86
87
88
89
# File 'lib/peddler/headers.rb', line 85

def mws_timestamp
  return unless headers['x-mws-timestamp']

  Time.parse(headers['x-mws-timestamp'])
end