Class: Nyara::HeaderHash

Inherits:
ParamHash show all
Defined in:
lib/nyara/hashes/header_hash.rb,
ext/hashes.c

Overview

This is a hash that Keys ignore case, and values all string, suitable for use in http header
TODO check invalid chars in values
TODO integrate extra lines

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze

Instance Method Summary collapse

Methods inherited from ParamHash

#_aref, #_aset, #nested_aref, #nested_aset, parse_cookie, parse_param, split_name

Methods inherited from Hash

#to_param

Instance Method Details

#[]Object


393
394
395
# File 'ext/hashes.c', line 393

static VALUE header_hash_aref(VALUE self, VALUE key) {
  return rb_hash_aref(self, header_hash_tidy_key(key));
}

#[]=Object


402
403
404
405
406
407
408
409
# File 'ext/hashes.c', line 402

static VALUE header_hash_aset(VALUE self, VALUE key, VALUE value) {
  key = header_hash_tidy_key(key);
  if (TYPE(value) != T_STRING) {
    value = rb_funcall(value, id_to_s, 0);
  }

  return rb_hash_aset(self, key, value);
}

#aref_content_typeObject


10
11
12
# File 'lib/nyara/hashes/header_hash.rb', line 10

def aref_content_type
  _aref CONTENT_TYPE
end

#aset_content_type(value) ⇒ Object


14
15
16
17
18
19
# File 'lib/nyara/hashes/header_hash.rb', line 14

def aset_content_type value
  unless value.index 'charset'
    value = "#{value}; charset=UTF-8"
  end
  _aset CONTENT_TYPE, value
end

#key?Boolean Also known as: has_key?

Returns:

  • (Boolean)

397
398
399
# File 'ext/hashes.c', line 397

static VALUE header_hash_key_p(VALUE self, VALUE key) {
  return nyara_rb_hash_has_key(self, header_hash_tidy_key(key)) ? Qtrue : Qfalse;
}

#reverse_merge!Object


419
420
421
422
423
424
425
426
# File 'ext/hashes.c', line 419

static VALUE header_hash_reverse_merge_bang(VALUE self, VALUE other) {
  if (!rb_obj_is_kind_of(other, nyara_header_hash_class)) {
    rb_raise(rb_eArgError, "need a Nyara::HeaderHash");
  }
  st_table* t = rb_hash_tbl(self);
  rb_hash_foreach(other, _reverse_merge_func, (VALUE)t);
  return self;
}

#serializeObject


445
446
447
448
449
450
451
452
453
454
# File 'ext/hashes.c', line 445

static VALUE header_hash_serialize(VALUE self) {
# ifdef HAVE_RB_ARY_NEW_CAPA
  long size = (!RHASH(self)->ntbl ? RHASH(self)->ntbl->num_entries : 0);
  volatile VALUE arr = rb_ary_new_capa(size);
# else
  volatile VALUE arr = rb_ary_new();
# endif
  rb_hash_foreach(self, _serialize_func, arr);
  return arr;
}