Skip to content

Commit

Permalink
feat: add parse_to_return_type macro
Browse files Browse the repository at this point in the history
  • Loading branch information
kimburgess committed Jun 8, 2020
1 parent b351bc4 commit 42b17df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ To become responsible, prefix any response or method that returns a response wit
response = ~HTTP::Client.get("https://www.example.com")
```

Error conditions are clear:
This allows a clear way to express what should happen when errors occur.
```crystal
response.on_server_error do
# oh noes
Expand All @@ -57,6 +57,14 @@ response[:message]
# => hello world
```

Macros are also provided to keep common tasks clean, clear and concise.
```crystal
def example_request_wrapper : { message: String }
Responsible.parse_to_return_type do
HTTP::Client.get("https://www.example.com")
end
end
```
---

Responsible Responses™ maintain all existing functionality of a vanilla response object.
Expand Down
13 changes: 13 additions & 0 deletions src/responsible.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ module Responsible
{{ block.body if block.is_a? AstNode }}
end
end

# Wraps a expression that returns a supported response object into a
# `Responsible::Response` before attempting to parse this out into the return
# type of the surrounding method.
#
# This may be
macro parse_to_return_type(&block)
\{{ raise "no return type on method" if @def.return_type.is_a? Nop }}
%response = begin
{{ block.body }}
end
Responsible::Response.new(%response).parse_to(\{{ @def.return_type }})
end
end

Responsible.support HTTP::Client::Response

0 comments on commit 42b17df

Please sign in to comment.