Class: Simple::HTTP::BodyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/http/body_builder.rb

Overview

parses content_type to return media_type and charset, and can reencode bodies.

Constant Summary collapse

SPLIT_PATTERN =
%r{\s*[;,]\s*}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type) ⇒ BodyBuilder

Returns a new instance of BodyBuilder.



9
10
11
12
# File 'lib/simple/http/body_builder.rb', line 9

def initialize(content_type)
  @media_type = content_type.split(SPLIT_PATTERN, 2).first.downcase if content_type
  @charset = fetch_content_type_param(content_type, "charset", default: nil)
end

Instance Attribute Details

#charsetObject (readonly)

The charset as embedded on the Content-Type header



23
24
25
# File 'lib/simple/http/body_builder.rb', line 23

def charset
  @charset
end

#media_typeObject (readonly)

The media type (type/subtype) portion of the CONTENT_TYPE header without any media type parameters. e.g., when CONTENT_TYPE is “text/plain;charset=utf-8”, the media-type is “text/plain”.

For more information on the use of media types in HTTP, see: www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7



20
21
22
# File 'lib/simple/http/body_builder.rb', line 20

def media_type
  @media_type
end

Instance Method Details

#reencode(body) ⇒ Object

returns the body

This method reencodes the text body into UTF-8. Non-text bodies should be encoded as ASCII-8BIT (a.k.a. “BINARY”)



29
30
31
# File 'lib/simple/http/body_builder.rb', line 29

def reencode(body)
  body&.encode(best_encoding)
end