File: headers_processor.rb

package info (click to toggle)
ruby-httparty 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 908 kB
  • sloc: ruby: 6,782; xml: 425; sh: 35; makefile: 14
file content (32 lines) | stat: -rw-r--r-- 853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

module HTTParty
  class HeadersProcessor
    attr_reader :headers, :options

    def initialize(headers, options)
      @headers = headers
      @options = options
    end

    def call
      return unless options[:headers]

      options[:headers] = headers.merge(options[:headers]) if headers.any?
      options[:headers] = Utils.stringify_keys(process_dynamic_headers)
    end

    private

    def process_dynamic_headers
      options[:headers].each_with_object({}) do |header, processed_headers|
        key, value = header
        processed_headers[key] = if value.respond_to?(:call)
                                   value.arity == 0 ? value.call : value.call(options)
                                 else
                                   value
                                 end
      end
    end
  end
end