Skip to content

Commit

Permalink
feat: Action and Channel are modules now
Browse files Browse the repository at this point in the history
BREAKING CHANGE: they used to be abstract Struct and Class before

Closes #23
  • Loading branch information
vladfaust committed Aug 20, 2018
1 parent 12e7b79 commit d09b4f6
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 54 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ Please refer to the API documentation available online at [github.vladfaust.com/
```crystal
require "prism"
struct KnockKnock < Prism::Action
include Params
struct KnockKnock
include Prism::Action
include Prism::Action::Params
params do
param :who, String
Expand Down Expand Up @@ -70,7 +71,9 @@ We call them *Channels* for convenience.
```crystal
require "prism"
class Notifications < Prism::Channel
class Notifications
include Prism::Channel
@@subscriptions = Array(self).new
def self.notify(message)
Expand Down
10 changes: 6 additions & 4 deletions spec/action/auth/complex_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ module Prism::Action::Auth::ComplexSpec
end
end

struct StrictAction < Prism::Action
include Auth(Authable)
struct StrictAction
include Prism::Action
include Prism::Action::Auth(Authable)

auth!(:admin, level: :god)

Expand All @@ -41,8 +42,9 @@ module Prism::Action::Auth::ComplexSpec
end
end

struct NonStrictAction < Prism::Action
include Auth(Authable)
struct NonStrictAction
include Prism::Action
include Prism::Action::Auth(Authable)

def call
begin
Expand Down
10 changes: 6 additions & 4 deletions spec/action/auth/simple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ module Prism::Action::Auth::SimpleSpec
end
end

struct StrictAction < Prism::Action
include Auth(Authable)
struct StrictAction
include Prism::Action
include Prism::Action::Auth(Authable)

auth!

Expand All @@ -23,8 +24,9 @@ module Prism::Action::Auth::SimpleSpec
end
end

struct NonStrictAction < Prism::Action
include Auth(Authable)
struct NonStrictAction
include Prism::Action
include Prism::Action::Auth(Authable)

def call
if auth?
Expand Down
5 changes: 3 additions & 2 deletions spec/action/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ require "../spec_helper"
require "../../src/prism/action"

module Prism::Action::Params::Spec
struct PrismAction < Prism::Action
include Params
struct PrismAction
include Prism::Action
include Prism::Action::Params

params do
param :id, Int32, validate: {gte: 42}
Expand Down
38 changes: 28 additions & 10 deletions spec/action_spec.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require "./spec_helper"
require "../src/prism/action"

abstract struct Prism::Action
struct OkAction < Prism::Action
module Prism::Action
struct OkAction
include Prism::Action

def call
status(205)
text("ok")
Expand All @@ -21,7 +23,9 @@ abstract struct Prism::Action
end
end

struct OkActionWithStatus < Prism::Action
struct OkActionWithStatus
include Prism::Action

def call
text(205, "ok")
end
Expand All @@ -39,7 +43,9 @@ abstract struct Prism::Action
end
end

struct JsonAction < Prism::Action
struct JsonAction
include Prism::Action

def call
json({"foo" => "bar"})
end
Expand All @@ -57,7 +63,9 @@ abstract struct Prism::Action
end
end

struct JsonWithStatusAction < Prism::Action
struct JsonWithStatusAction
include Prism::Action

def call
json(201, {
foo: "bar",
Expand All @@ -81,7 +89,9 @@ abstract struct Prism::Action
end
end

struct HaltAction < Prism::Action
struct HaltAction
include Prism::Action

class_property unwanted_calls_count = 0

def call
Expand All @@ -108,7 +118,9 @@ abstract struct Prism::Action
end
end

struct TextHaltAction < Prism::Action
struct TextHaltAction
include Prism::Action

def call
halt!(404, "Nope")
end
Expand All @@ -122,7 +134,9 @@ abstract struct Prism::Action
end
end

struct JSONHaltAction < Prism::Action
struct JSONHaltAction
include Prism::Action

def call
halt!(403, {error: "Oops"})
end
Expand All @@ -136,7 +150,9 @@ abstract struct Prism::Action
end
end

struct BodyAction < Prism::Action
struct BodyAction
include Prism::Action

class_property last_body : String? = nil

def call
Expand All @@ -152,7 +168,9 @@ abstract struct Prism::Action
end
end

struct CallbacksAction < Prism::Action
struct CallbacksAction
include Prism::Action

class_property buffer = [] of String

before do
Expand Down
20 changes: 13 additions & 7 deletions src/prism/action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module Prism
# NOTE: *(From [API](https://crystal-lang.org/api/0.23.1/HTTP/Server/Response.html)) The response #status_code and #headers must be configured before writing the response body. Once response output is written, changing the status and #headers properties has no effect.*
#
# ```
# struct MyAction < Prism::Action
# struct MyAction
# include Prism::Action
#
# def call
# text("ok")
# end
Expand All @@ -22,15 +24,19 @@ module Prism
# MyAction.call(env)
# # => "ok"
# ```
abstract struct Action
module Action
include Callbacks

abstract def call

# Initialize and invoke `#call` with callbacks.
# Learn more about callbacks at https://github.com/vladfaust/callbacks.cr.
def self.call(context : ::HTTP::Server::Context)
new(context).call_with_callbacks
macro included
# Initialize and invoke `#call` with callbacks.
# Learn more about callbacks at https://github.com/vladfaust/callbacks.cr.
def self.call(context : ::HTTP::Server::Context)
new(context).call_with_callbacks
end

class_property max_body_size
end

# :nodoc:
Expand All @@ -39,7 +45,7 @@ module Prism
end

# Will **not** raise on exceed, defaults to 8 MB.
class_property max_body_size = UInt64.new(8 * 1024 ** 2)
@@max_body_size = UInt64.new(8 * 1024 ** 2)

@body : String?

Expand Down
5 changes: 3 additions & 2 deletions src/prism/action/auth.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require "../auth"

module Prism
abstract struct Action
module Action
# An `Action` module which adds `auth!` macro, attempting to do auth in before callback.
#
# ```
# struct StrictAction < Prism::Action
# struct StrictAction
# include Prism::Action
# include Prism::Action::Auth(AuthableObject)
#
# auth!(:admin) # Would try to auth before call, halt otherwise
Expand Down
7 changes: 4 additions & 3 deletions src/prism/action/params.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require "../params"

module Prism
abstract struct Action
module Action
# Params module for `Prism::Action`. It injects params parsing into `before` callback.
#
# Halts with 422 if `Prism::Params::InvalidParamTypeError`, `Prism::Params::ParamNotFoundError` or `Prism::Params::InvalidParamError` raised.
#
# ```
# struct MyAction < Prism::Action
# include Params
# struct MyAction
# include Prism::Action
# include Prism::Params
#
# params do
# param :foo, Int32
Expand Down
3 changes: 2 additions & 1 deletion src/prism/auth.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module Prism
# end
# end
#
# struct MyAction < Prism::Action
# struct MyAction
# include Prism::Action
# include Prism::Auth(AuthableObject)
#
# def call
Expand Down
25 changes: 14 additions & 11 deletions src/prism/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module Prism
# A callable websocket Channel with [Callbacks](https://github.com/vladfaust/callbacks.cr) module included.
#
# ```
# class UserNotifications < Prism::Channel
# include Auth
# include Params
# class UserNotifications
# include Prism::Channel
# include Prism::Channel::Auth
# include Prism::Channel::Params
#
# # Will close the socket if unauthorized
# auth!
Expand Down Expand Up @@ -50,7 +51,7 @@ module Prism
#
# UserNotifications.notify(user, "You've got a message!")
# ```
class Channel
module Channel
include Callbacks

# Called once when a new socket is opened.
Expand Down Expand Up @@ -78,14 +79,16 @@ module Prism
def on_close
end

# Initialize a new instance and invoke `#subscribe_with_callbacks` on it.
def self.subscribe(socket : HTTP::WebSocket, context : HTTP::Server::Context)
new(socket, context).subscribe_with_callbacks
end
macro included
# Initialize a new instance and invoke `#subscribe_with_callbacks` on it.
def self.subscribe(socket : HTTP::WebSocket, context : HTTP::Server::Context)
new(socket, context).subscribe_with_callbacks
end

# ditto
def self.call(socket, context)
subscribe(socket, context)
# ditto
def self.call(socket, context)
subscribe(socket, context)
end
end

# Call `#on_open` and bind to the `socket`'s events. Read more in [Crystal API docs](https://crystal-lang.org/api/0.23.1/HTTP/WebSocket.html).
Expand Down
5 changes: 3 additions & 2 deletions src/prism/channel/auth.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require "../auth"

module Prism
class Channel
module Channel
# An `Channel` module which adds `auth!` macro, attempting to do auth in before callback.
#
# ```
# class MyChannel < Prism::Channel
# class MyChannel
# include Prism::Channel
# include Prism::Channel::Auth(AuthableObject)
#
# auth!(:admin)
Expand Down
5 changes: 3 additions & 2 deletions src/prism/channel/params.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require "../params"

module Prism
class Channel
module Channel
# Params module for `Prism::Channel`. It injects params parsing into `before` callback.
#
# Closes the socket if `Prism::Params::InvalidParamTypeError`, `Prism::Params::ParamNotFoundError` or `Prism::Params::InvalidParamError` raised.
#
# ```
# class MyChannel < Prism::Channel
# class MyChannel
# include Prism::Channel
# include Params
#
# params do
Expand Down
7 changes: 4 additions & 3 deletions src/prism/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ require "./params/**"
#
# If you want to implement your own type casting, extend it with `.from_param` method (see `Int.from_param` for example).
#
# If included into `Prism::Action`, it will automatically inject `parse_params` into `Action#before` callback:
# If included along with `Prism::Action`, it will automatically inject `parse_params` into `#before` callback:
#
# ```
# struct MyPrismAction < Prism::Action
# include Params
# struct MyPrismAction
# include Prism::Action
# include Prism::Action::Params
#
# params do
# param :id, Int32
Expand Down

0 comments on commit d09b4f6

Please sign in to comment.