Module: OpenURI::Meta

Defined in:
opal/stdlib/open-uri.rb

Overview

Mixin for holding meta-information.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_uriObject

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.



223
224
225
# File 'opal/stdlib/open-uri.rb', line 223

def base_uri
  @base_uri
end

#metaObject (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.



230
231
232
# File 'opal/stdlib/open-uri.rb', line 230

def meta
  @meta
end

#metasObject (readonly)

returns a Hash that represents header fields. The Hash keys are downcased for canonicalization. The Hash value are an array of field values.



235
236
237
# File 'opal/stdlib/open-uri.rb', line 235

def metas
  @metas
end

#statusObject

returns an Array that consists of status code and message.



219
220
221
# File 'opal/stdlib/open-uri.rb', line 219

def status
  @status
end

Class Method Details

.init(obj, src = nil) ⇒ Object

:nodoc:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'opal/stdlib/open-uri.rb', line 202

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.metas.each {|name, values|
      obj.meta_add_field2(name, values)
    }
  end
end

Instance Method Details

#charsetObject

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.



300
301
302
303
304
305
306
307
# File 'opal/stdlib/open-uri.rb', line 300

def charset
  type = content_type_parse
  if type && %r{\Atext/} =~ type && @base_uri && /\Ahttp\z/i =~ @base_uri.scheme
    'iso-8859-1' # RFC2616 3.7.1
  else
    nil
  end
end

#content_typeObject

returns "type/subtype" which is MIME Content-Type. It is downcased for canonicalization. Content-Type parameters are stripped.



312
313
314
315
# File 'opal/stdlib/open-uri.rb', line 312

def content_type
  type = content_type_parse
  type || 'application/octet-stream'
end

#content_type_parseObject

:nodoc:



284
285
286
287
288
# File 'opal/stdlib/open-uri.rb', line 284

def content_type_parse # :nodoc:
  content_type = @metas['content-type']
  # FIXME Extract type, subtype and parameters
  content_type.join(', ')
end

#find_encoding(charset) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
# File 'opal/stdlib/open-uri.rb', line 253

def find_encoding(charset)
  enc = nil
  if charset
    begin
      enc = Encoding.find(charset)
    rescue ArgumentError
    end
  end
  enc = Encoding::ASCII_8BIT unless enc
  enc
end

#last_modifiedObject



276
277
278
279
280
281
282
# File 'opal/stdlib/open-uri.rb', line 276

def last_modified
  if (vs = @metas['last-modified'])
    Time.at(`Date.parse(#{vs.join(', ')}) / 1000`).utc
  else
    nil
  end
end

#meta_add_field(name, value) ⇒ Object

:nodoc:



272
273
274
# File 'opal/stdlib/open-uri.rb', line 272

def meta_add_field(name, value) # :nodoc:
  meta_add_field2(name, [value])
end

#meta_add_field2(name, values) ⇒ Object

:nodoc:



265
266
267
268
269
270
# File 'opal/stdlib/open-uri.rb', line 265

def meta_add_field2(name, values) # :nodoc:
  name = name.downcase
  @metas[name] = values
  @meta[name] = values.join(', ')
  meta_setup_encoding if name == 'content-type'
end

#meta_setup_encodingObject

:nodoc:



237
238
239
240
241
# File 'opal/stdlib/open-uri.rb', line 237

def meta_setup_encoding # :nodoc:
  charset = self.charset
  enc = find_encoding(charset)
  set_encoding(enc)
end

#set_encoding(enc) ⇒ Object



243
244
245
246
247
248
249
250
251
# File 'opal/stdlib/open-uri.rb', line 243

def set_encoding(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