Sha256: 9dc157e8ad2eeef9184148c6d320b6d5f8792f20f94353d192c23a272040f0d7
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
# frozen_string_literal: true require_relative "builder/version" require "uri" module URI module Builder class Error < StandardError; end class DSL attr_reader :uri def initialize(uri) @uri = uri.clone end [:host, :query, :fragment, :port].each do |property| define_method property do |value| wrap property, value end end def scheme(value) if @uri.scheme # Handles URLs without schemes, like https://example.com/foo target_scheme = URI.scheme_list[value.upcase] args = Hash[target_scheme.component.map { |attr| [ attr, @uri.send(attr) ] }] @uri = target_scheme.build(**args) else # Handles URLs without schemes, like example.com/foo uri = URI.parse("#{value}://#{@uri.path}") (uri.component - %i[host path scheme]).each do |component| uri.send "#{component}=", @uri.send(component) end @uri = uri end end def query(value) value = case value when Hash URI.encode_www_form value else value end wrap :query, value end def path(*segments) # Make sure there's a leading / if a non leading / is given. wrap :path, ::File.join(*segments.compact.map(&:to_s).prepend("/")) end def to_s uri.to_s end private def wrap(property, value) @uri.send "#{property}=", value self end end end def build Builder::DSL.new self end def self.build(value) URI(value).build end def self.env(key, default = nil) build ENV.fetch key, default end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
uri-builder-0.1.4 | lib/uri/builder.rb |