Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: df3399f07556
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4e079f91809a
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Oct 25, 2017

  1. Avoid misleading error message in HTTP::Params.encode

    Restrict types supported by HTTP::Params.encode to avoid long and confusing
    error messages.
    
    With this change, attempt to use a `Hash` other than `Hash(String, String)`
    will result in a better error message:
    
        require "http/params"
    
        puts HTTP::Params.encode({"foo" => 10})
    
    Output:
    
        no overload matches 'HTTP::Params.encode' with type Hash(String, Int32)
        Overloads are:
         - HTTP::Params.encode(hash : Hash(String, String))
         - HTTP::Params.encode(named_tuple : NamedTuple)
    
    Also includes examples on usage.
    
    Fixes #5172
    luislavena committed Oct 25, 2017
    Copy the full SHA
    488dda7 View commit details

Commits on Oct 26, 2017

  1. Merge pull request #5184 from luislavena/restrict-type-on-http-params…

    …-encode
    
    Avoid misleading error message in HTTP::Params.encode
    asterite authored Oct 26, 2017
    Copy the full SHA
    4e079f9 View commit details
Showing with 11 additions and 4 deletions.
  1. +11 −4 src/http/params.cr
15 changes: 11 additions & 4 deletions src/http/params.cr
Original file line number Diff line number Diff line change
@@ -76,17 +76,24 @@ module HTTP
end
end

# Returns the given key value pairs as a
# url-encoded HTTP form/query.
def self.encode(hash : Hash(String, _))
# Returns the given key value pairs as a url-encoded HTTP form/query.
#
# ```
# HTTP::Params.encode({"foo" => "bar", "baz" => "qux"}) # => foo=bar&baz=qux
# ```
def self.encode(hash : Hash(String, String))
build do |builder|
hash.each do |key, value|
builder.add key, value
end
end
end

# ditto
# Returns the given key value pairs as a url-encoded HTTP form/query.
#
# ```
# HTTP::Params.encode({foo: "bar", baz: "qux"}) # => foo=bar&baz=qux
# ```
def self.encode(named_tuple : NamedTuple)
build do |builder|
named_tuple.each do |key, value|