httprb / http

HTTP (The Gem! a.k.a. http.rb) - a fast Ruby HTTP client with a chainable API, streaming support, and timeouts
MIT License
3.01k stars 321 forks source link

[Feature] Pattern Matching extensions #642

Open baweaver opened 3 years ago

baweaver commented 3 years ago

I'd like to propose adding pattern matching to some of the core classes of http, primarily deconstruct_keys, to allow for matching against things like Responses and Requests.

Consider:

response in {
  status: 200..299,
  body: /Header I want/
}

case response
in { status: 200..299, body: } then Success(body)
in { status: 300.., body: } then Failure(body)
else Failure('unknown')
end

This is just a theoretical example and may not be 100% accurate to the API, but I wanted to run things by the core team before implementing this as it ends up being a decent amount of additional code.

It should be noted that adding the interfaces (deconstruct and deconstruct_keys) will be backward compatible, but testing it with pattern matching will not be.