File: proxy_options.rb

package info (click to toggle)
ruby-faraday 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,008 kB
  • sloc: ruby: 6,509; sh: 10; makefile: 8
file content (38 lines) | stat: -rw-r--r-- 1,161 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
33
34
35
36
37
38
# frozen_string_literal: true

module Faraday
  # @!parse
  #   # ProxyOptions contains the configurable properties for the proxy
  #   # configuration used when making an HTTP request.
  #   class ProxyOptions < Options; end
  ProxyOptions = Options.new(:uri, :user, :password) do
    extend Forwardable
    def_delegators :uri, :scheme, :scheme=, :host, :host=, :port, :port=,
                   :path, :path=

    def self.from(value)
      case value
      when ''
        value = nil
      when String
        # URIs without a scheme should default to http (like 'example:123').
        # This fixes #1282 and prevents a silent failure in some adapters.
        value = "http://#{value}" unless value.include?('://')
        value = { uri: Utils.URI(value) }
      when URI
        value = { uri: value }
      when Hash, Options
        if value[:uri]
          value = value.dup.tap do |duped|
            duped[:uri] = Utils.URI(duped[:uri])
          end
        end
      end

      super(value)
    end

    memoized(:user) { uri&.user && Utils.unescape(uri.user) }
    memoized(:password) { uri&.password && Utils.unescape(uri.password) }
  end
end