Skip to content

Commit

Permalink
feat (Renderers): rename Text renderer to Plain
Browse files Browse the repository at this point in the history
BREAKING CHANGE

Closes #67
  • Loading branch information
vladfaust committed Mar 3, 2019
1 parent fc512fd commit d1769a8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions spec/onyx-rest/action_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "../spec_helper"
require "../../src/onyx-rest/renderers/text"
require "../../src/onyx-rest/renderers/plain"

struct FooAction
include Onyx::REST::Action
Expand Down Expand Up @@ -73,7 +73,7 @@ class ActionSpecServer
getter server

def initialize
renderer = Onyx::REST::Renderers::Text.new
renderer = Onyx::REST::Renderers::Plain.new
rescuer = Onyx::REST::Rescuer.new(renderer)
router = Onyx::HTTP::Router.new do
post "/foo/:path_param", FooAction
Expand Down
2 changes: 1 addition & 1 deletion spec/onyx-rest/renderers/json_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct JSONView

json({foo: @foo})

# `Renderers::Text` is required in another spec,
# `Renderers::Plain` is required in another spec,
# therefore Crystal assumes this view could be invoked with #to_text as well
text(raise NotImplementedError.new(self))
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../../spec_helper"
require "../../../src/onyx-rest/renderers/text"
require "../../../src/onyx-rest/renderers/plain"

struct TextView
struct PlainView
include Onyx::REST::View

def initialize(@foo : String)
Expand All @@ -18,7 +18,7 @@ struct TextView
json(raise NotImplementedError.new(self))
end

class TextError < Onyx::REST::Error(505)
class PlainError < Onyx::REST::Error(505)
def initialize(@foo : String)
super(@foo)
end
Expand All @@ -28,15 +28,15 @@ class TextError < Onyx::REST::Error(505)
end
end

class TextRendererSpecServer
class PlainRendererSpecServer
def initialize
renderer = Onyx::REST::Renderers::Text.new
renderer = Onyx::REST::Renderers::Plain.new
router = Onyx::HTTP::Router.new do
get "/" do |env|
if env.request.query_params["raise"]?
env.response.error = TextError.new("Boom!")
env.response.error = PlainError.new("Boom!")
else
env.response.view = TextView.new("OK")
env.response.view = PlainView.new("OK")
end
end

Expand All @@ -49,8 +49,8 @@ class TextRendererSpecServer
getter server
end

describe Onyx::REST::Renderers::Text do
server = TextRendererSpecServer.new
describe Onyx::REST::Renderers::Plain do
server = PlainRendererSpecServer.new

spawn do
server.server.bind_tcp(4890)
Expand Down
2 changes: 1 addition & 1 deletion spec/onyx-rest/renderers/template_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct TemplateView

template("./templates/test.ecr")

# `Renderers::Text` is required in another spec,
# `Renderers::Plain` is required in another spec,
# therefore Crystal assumes this view could be invoked with #to_text as well
text("foo: #{@foo}")

Expand Down
8 changes: 4 additions & 4 deletions spec/onyx-rest/view_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ describe Onyx::REST::View do
io.to_s.should eq %Q[{"foo":"baz","bar":42}]
end

it "has #to_text" do
view.to_text.should eq "foo: baz, bar: 42"
it "has #to_plain_text" do
view.to_plain_text.should eq "foo: baz, bar: 42"
end

it "has #to_text(io)" do
it "has #to_plain_text(io)" do
io = IO::Memory.new
view.to_text(io)
view.to_plain_text(io)
io.to_s.should eq "foo: baz, bar: 42"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Onyx::REST
# It updates the `"Content-Type"` header **only** if error of view is present.
# Should be put after router.
# Calls the next handler if it's present.
class Text
class Plain
include ::HTTP::Handler

CONTENT_TYPE = "text/plain; charset=utf-8"
Expand Down Expand Up @@ -48,7 +48,7 @@ module Onyx::REST
context.response << code << " " << message
elsif view = context.response.view
context.response.content_type = CONTENT_TYPE
view.to_text(context.response)
view.to_plain_text(context.response)
end

if self.next
Expand Down
20 changes: 10 additions & 10 deletions src/onyx-rest/view.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module Onyx::REST::View
end
end

# Define a `#to_text` method to be rendered with `Renderers::Text`.
# Define a `#to_plain_text` method to be rendered with `Renderers::Plain`.
#
# ```
# struct UserView
Expand All @@ -133,21 +133,21 @@ module Onyx::REST::View
# end
# end
#
# UserView.new(user).to_text # => "id: 1, name: John"
# UserView.new(user).to_plain_text # => "id: 1, name: John"
# ```
macro text(&block)
def to_text
def to_plain_text
io = IO::Memory.new
to_text(io)
to_plain_text(io)
io.to_s
end

def to_text(io)
def to_plain_text(io)
io << ({{yield}})
end
end

# Define a `#to_text` method with an arbitrary object to be rendered with `Renderers::Text`.
# Define a `#to_plain_text` method with an arbitrary object to be rendered with `Renderers::Plain`.
# This object will be invoked with `#to_s`.
#
# ```
Expand All @@ -160,16 +160,16 @@ module Onyx::REST::View
# text({id: @user.id, name: @user.name})
# end
#
# UserView.new(user).to_text # => "{id: 1, name: \"John\"}"
# UserView.new(user).to_plain_text # => "{id: 1, name: \"John\"}"
# ```
macro text(value)
def to_text
def to_plain_text
io = IO::Memory.new
to_text(io)
to_plain_text(io)
io.to_s
end

def to_text(io)
def to_plain_text(io)
value = ({{value}})
io << value
end
Expand Down

0 comments on commit d1769a8

Please sign in to comment.