martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
405 stars 23 forks source link

add HTTP Status code lookup class #124

Closed kates closed 11 months ago

kates commented 11 months ago

Taking a shot at adding this feature https://github.com/martenframework/marten/issues/120 using the mapping from https://crystal-lang.org/api/1.9.2/HTTP/Status.html

Blacksmoke16 commented 11 months ago

FWIW there's an easier way to implement this. Just type the status parameter as HTTP::Status | Int32 = :ok, then pass it to HTTP::Status.new to normalize everything and call it a day:

require "http/status"

def respond(status : HTTP::Status | Int32 = :ok)
  pp HTTP::Status.new status
end

respond                           # => HTTP::Status::OK
respond HTTP::Status::BAD_REQUEST # => HTTP::Status::BAD_REQUEST
respond 404.                      # => HTTP::Status::NOT_FOUND
respond :im_a_teapot.             # => HTTP::Status::IM_A_TEAPOT
ellmetha commented 11 months ago

@Blacksmoke16 Thanks a lot for the suggestion! 🙏 This is simpler indeed, definitely the way to go!

@kates could we simplify the PR as suggested by @Blacksmoke16? That way, by using HTTP::Status in the type restrictions for the status parameter, we wouldn't need to define a Marten::HTTP::Status module at all.

kates commented 11 months ago

Thanks @ellmetha @Blacksmoke16