Module: OpenURI::Meta
Relationships & Source Files | |
Defined in: | lib/open-uri.rb |
Overview
Mixin for holding meta-information.
Constant Summary
-
RE_LWS =
# File 'lib/open-uri.rb', line 505/[\r\n\t ]+/n
-
RE_PARAMETERS =
# File 'lib/open-uri.rb', line 508%r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
-
RE_QUOTED_STRING =
# File 'lib/open-uri.rb', line 507%r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n
-
RE_TOKEN =
# File 'lib/open-uri.rb', line 506%r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n
Class Method Summary
Instance Attribute Summary
Instance Method Summary
-
#charset
returns a charset parameter in Content-Type field.
-
#content_encoding
Returns a list of encodings in Content-Encoding field as an array of strings.
-
#content_type
returns “type/subtype” which is MIME Content-Type.
- #content_type_parse
-
#last_modified
returns a Time that represents the Last-Modified field.
- #meta_add_field(name, value)
- #meta_add_field2(name, values)
- #meta_setup_encoding
Class Method Details
.init(obj, src = nil)
[ GitHub ]# File 'lib/open-uri.rb', line 429
def Meta.init(obj, src=nil) # :nodoc: obj.extend Meta obj.instance_eval { @base_uri = nil @meta = {} # name to string. legacy. @metas = {} # name to array of strings. } if src obj.status = src.status obj.base_uri = src.base_uri src. .each {|name, values| obj. (name, values) } end end
Instance Attribute Details
#base_uri (rw)
# File 'lib/open-uri.rb', line 450
attr_accessor :base_uri
#meta (readonly)
returns a Hash that represents header fields. The Hash keys are downcased for canonicalization. The Hash values are a field body. If there are multiple field with same field name, the field values are concatenated with a comma.
# File 'lib/open-uri.rb', line 457
attr_reader :
#metas (readonly)
returns a Hash that represents header fields. The Hash keys are downcased for canonicalization. The Hash value are an array of field values.
# File 'lib/open-uri.rb', line 462
attr_reader :
#status (rw)
returns an Array that consists of status code and message.
# File 'lib/open-uri.rb', line 446
attr_accessor :status
Instance Method Details
#charset
returns a charset parameter in Content-Type field. It is downcased for canonicalization.
If charset parameter is not given but a block is given, the block is called and its result is returned. It can be used to guess charset.
If charset parameter and block is not given, nil is returned except text type in HTTP. In that case, “iso-8859-1” is returned as defined by RFC2616 3.7.1.
# File 'lib/open-uri.rb', line 548
def charset type, *parameters = content_type_parse if pair = parameters.assoc('charset') pair.last.downcase elsif block_given? yield elsif type && %r{\Atext/} =~ type && @base_uri && /\Ahttp\z/i =~ @base_uri.scheme "iso-8859-1" # RFC2616 3.7.1 else nil end end
#content_encoding
Returns a list of encodings in Content-Encoding field as an array of strings.
The encodings are downcased for canonicalization.
#content_type
returns “type/subtype” which is MIME Content-Type. It is downcased for canonicalization. Content-Type parameters are stripped.
# File 'lib/open-uri.rb', line 533
def content_type type, *_ = content_type_parse type || 'application/octet-stream' end
#content_type_parse
[ GitHub ]# File 'lib/open-uri.rb', line 511
def content_type_parse # :nodoc: vs = @metas['content-type'] # The last (?:;#{RE_LWS}?)? matches extra ";" which violates RFC2045. if vs && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})(?:;#{RE_LWS}?)?\z}no =~ vs.join(', ') type = $1.downcase subtype = $2.downcase parameters = [] $3.scan(/;#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?=#{RE_LWS}?(?:(#{RE_TOKEN})|(#{RE_QUOTED_STRING}))/no) {|att, val, qval| if qval val = qval[1...-1].gsub(/[\r\n\t !#-\[\]-~\x80-\xff]+|(\\[\x00-\x7f])/n) { $1 ? $1[1,1] : $& } end parameters << [att.downcase, val] } ["#{type}/#{subtype}", *parameters] else nil end end
#last_modified
returns a Time that represents the Last-Modified field.
# File 'lib/open-uri.rb', line 495
def last_modified if vs = @metas['last-modified'] v = vs.join(', ') Time.httpdate(v) else nil end end
#meta_add_field(name, value)
[ GitHub ]# File 'lib/open-uri.rb', line 490
def (name, value) # :nodoc: (name, [value]) end
#meta_add_field2(name, values)
[ GitHub ]# File 'lib/open-uri.rb', line 483
def (name, values) # :nodoc: name = name.downcase @metas[name] = values @meta[name] = values.join(', ') if name == 'content-type' end
#meta_setup_encoding
[ GitHub ]# File 'lib/open-uri.rb', line 464
def # :nodoc: charset = self.charset enc = nil if charset begin enc = Encoding.find(charset) rescue ArgumentError end end enc = Encoding::ASCII_8BIT unless enc if self.respond_to? :force_encoding self.force_encoding(enc) elsif self.respond_to? :string self.string.force_encoding(enc) else # Tempfile self.set_encoding enc end end