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)


48
49
50
51
52
53
54
55
56
# File 'lib/peddler/headers.rb', line 48

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

  string = match_data[1].upcase
  string = 'UTF-8' if string == 'UTF8'

  Encoding.find(string)
end

#content_lengthString?

The size of the response body in bytes

Returns:

  • (String, nil)


10
11
12
13
14
# File 'lib/peddler/headers.rb', line 10

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)


18
19
20
# File 'lib/peddler/headers.rb', line 18

def content_md5
  headers['Content-MD5']
end

#content_media_typeString?

The MIME type of the response

Returns:

  • (String, nil)


24
25
26
27
28
# File 'lib/peddler/headers.rb', line 24

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)


40
41
42
43
44
# File 'lib/peddler/headers.rb', line 40

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)


32
33
34
35
36
# File 'lib/peddler/headers.rb', line 32

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)


60
61
62
63
64
# File 'lib/peddler/headers.rb', line 60

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)


68
69
70
71
72
# File 'lib/peddler/headers.rb', line 68

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)


76
77
78
79
80
# File 'lib/peddler/headers.rb', line 76

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)


84
85
86
# File 'lib/peddler/headers.rb', line 84

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

#mws_response_contextString?

The context of the response

Returns:

  • (String, nil)


98
99
100
# File 'lib/peddler/headers.rb', line 98

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

#mws_timestampTime?

The timestamp of the request

Returns:

  • (Time, nil)


90
91
92
93
94
# File 'lib/peddler/headers.rb', line 90

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

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