Module: RocketPants::HeaderMetadata

Defined in:
lib/rocket_pants/controller/header_metadata.rb

Instance Method Summary collapse

Instance Method Details

#build_header_hash(options, hash = {}, prefix = 'X-Api') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rocket_pants/controller/header_metadata.rb', line 24

def build_header_hash(options, hash = {}, prefix = 'X-Api')
  options.each_pair do |k, v|
    current = "#{prefix}-#{k.to_s.titleize.tr(" ", "-")}"
    if v.is_a?(Hash)
      build_header_hash v, hash, current
    else
      value = Array(v).join(", ")
      hash[current] = value if value.present?
    end
  end
  hash
end

#expose_metadata(metadata) ⇒ Hash{Symbol => Object}

Given a hash of request metadata, will:

  1. Write out the headers for the current metadata

  2. Return the hash, suitable for merging into the response hash

  3. Start a dance party.

Parameters:

  • metadata (Hash{Symbol => Object})

    the hash of the request metadata.

Returns:

  • (Hash{Symbol => Object})

    the passed in metadata



11
12
13
14
# File 'lib/rocket_pants/controller/header_metadata.rb', line 11

def ()
   { build_header_hash() }
  super # Call any other versions of the method.
end

#metadata_headers(&blk) ⇒ Object

Given a block which returns a Hash, will call and merge the block iff header metadata is enabled. This is to avoid the overhead of generating headers on every request when it’s disabled.



20
21
22
# File 'lib/rocket_pants/controller/header_metadata.rb', line 20

def (&blk)
  headers.merge! blk.call if RocketPants.header_metadata?
end