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.



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

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.



227
228
229
# File 'opal/stdlib/open-uri.rb', line 227

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.



232
233
234
# File 'opal/stdlib/open-uri.rb', line 232

def metas
  @metas
end

#statusObject

returns an Array that consists of status code and message.



216
217
218
# File 'opal/stdlib/open-uri.rb', line 216

def status
  @status
end

Class Method Details

.init(obj, src = nil) ⇒ Object

:nodoc:



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

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.



297
298
299
300
301
302
303
304
# File 'opal/stdlib/open-uri.rb', line 297

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.



309
310
311
312
# File 'opal/stdlib/open-uri.rb', line 309

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

#content_type_parseObject

:nodoc:



281
282
283
284
285
# File 'opal/stdlib/open-uri.rb', line 281

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

#find_encoding(charset) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'opal/stdlib/open-uri.rb', line 250

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



273
274
275
276
277
278
279
# File 'opal/stdlib/open-uri.rb', line 273

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:



269
270
271
# File 'opal/stdlib/open-uri.rb', line 269

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

#meta_add_field2(name, values) ⇒ Object

:nodoc:



262
263
264
265
266
267
# File 'opal/stdlib/open-uri.rb', line 262

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:



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

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

#set_encoding(enc) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'opal/stdlib/open-uri.rb', line 240

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