Method: Rodbot::Refinements#uri_concat

Defined in:
lib/rodbot/refinements.rb

#uri_concatString

Safely concat path segments to a URI string

URI#join is ultimately used to add the given segments which has a maybe counter-intuitive API at first. Check out the docs of URI#join and the examples below.

Examples:

s = 'http://example.com'
s.uri_concat('foo')               # => "http://example.com/foo"
s.uri_concat('foo/')              # => "http://example.com/foo/"
s.uri_concat('foo', 'bar')        # => "http://example.com/bar"   <- sic!
s.uri_concat('foo/, 'bar')        # => "http://example.com/foo/bar"
s.uri_concat('foo/, 'bar.html')   # => "http://example.com/foo/bar.html"
s.uri_concat('föö')               # => "http://example.com/f%C3%B6%C3%B6"

Parameters:

  • segments (Array<String>)

    path segments

Returns:

  • (String)

    concatted URI



53
54
55
56
57
58
59
60
# File 'lib/rodbot/refinements.rb', line 53

refine String do
  def uri_concat(*segments)
    parser = URI::RFC2396_PARSER
    segments.inject(URI(self)) do |uri, segment|
      uri + parser.escape(segment)
    end.to_s
  end
end