Module: OpenURI::Meta
- Defined in:
- lib/rubysl/open-uri/open-uri.rb
Overview
Mixin for holding meta-information.
Constant Summary collapse
- RE_LWS =
:stopdoc:
/[\r\n\t ]+/n
- RE_TOKEN =
%r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n
- RE_QUOTED_STRING =
%r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n
- RE_PARAMETERS =
%r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
returns a URI that is the base of relative URIs in the data.
-
#meta ⇒ Object
readonly
returns a Hash that represents header fields.
-
#status ⇒ Object
returns an Array that consists of status code and message.
Class Method Summary collapse
-
.init(obj, src = nil) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#charset ⇒ Object
returns a charset parameter in Content-Type field.
-
#content_encoding ⇒ Object
returns a list of encodings in Content-Encoding field as an Array of String.
-
#content_type ⇒ Object
returns “type/subtype” which is MIME Content-Type.
-
#content_type_parse ⇒ Object
:startdoc:.
-
#last_modified ⇒ Object
returns a Time that represents the Last-Modified field.
-
#meta_add_field(name, value) ⇒ Object
:nodoc:.
-
#meta_setup_encoding ⇒ Object
:nodoc:.
Instance Attribute Details
#base_uri ⇒ Object
returns a URI that is the base of relative URIs in the data. It may differ from the URI supplied by a user due to redirection.
424 425 426 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 424 def base_uri @base_uri end |
#meta ⇒ Object (readonly)
returns a Hash that represents header fields. The Hash keys are downcased for canonicalization.
428 429 430 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 428 def @meta end |
#status ⇒ Object
returns an Array that consists of status code and message.
420 421 422 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 420 def status @status end |
Class Method Details
.init(obj, src = nil) ⇒ Object
:nodoc:
404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 404 def Meta.init(obj, src=nil) # :nodoc: obj.extend Meta obj.instance_eval { @base_uri = nil @meta = {} } if src obj.status = src.status obj.base_uri = src.base_uri src..each {|name, value| obj.(name, value) } end end |
Instance Method Details
#charset ⇒ Object
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.
508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 508 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 ⇒ Object
returns a list of encodings in Content-Encoding field as an Array of String. The encodings are downcased for canonicalization.
525 526 527 528 529 530 531 532 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 525 def content_encoding v = @meta['content-encoding'] if v && %r{\A#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?(?:,#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?)*}o =~ v v.scan(RE_TOKEN).map {|content_coding| content_coding.downcase} else [] end end |
#content_type ⇒ Object
returns “type/subtype” which is MIME Content-Type. It is downcased for canonicalization. Content-Type parameters are stripped.
493 494 495 496 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 493 def content_type type, *_ = content_type_parse type || 'application/octet-stream' end |
#content_type_parse ⇒ Object
:startdoc:
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 471 def content_type_parse # :nodoc: v = @meta['content-type'] # The last (?:;#{RE_LWS}?)? matches extra ";" which violates RFC2045. if v && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})(?:;#{RE_LWS}?)?\z}no =~ v 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 ⇒ Object
returns a Time that represents the Last-Modified field.
456 457 458 459 460 461 462 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 456 def last_modified if v = @meta['last-modified'] Time.httpdate(v) else nil end end |
#meta_add_field(name, value) ⇒ Object
:nodoc:
449 450 451 452 453 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 449 def (name, value) # :nodoc: name = name.downcase @meta[name] = value if name == 'content-type' end |
#meta_setup_encoding ⇒ Object
:nodoc:
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/rubysl/open-uri/open-uri.rb', line 430 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 |