Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sinatra extension registration #337

Merged
merged 4 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Gemfile.lock
.bundle
.ruby-*
.tags
.byebug_history
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- **BREAKING**: Register `Serialize` sinatra extension correctly. This will require a change in `lib/endpoints/base.rb` - see [here](https://github.com/interagent/pliny/pull/337/files#diff-c7736e8c14f72274bc01c22fe809a6bb). ([#337](https://github.com/interagent/pliny/pull/337))

## [0.28.0] - 2020-05-06
### Changed
Expand Down
57 changes: 28 additions & 29 deletions lib/pliny/helpers/serialize.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
module Pliny::Helpers
module Serialize
def self.included(base)
base.send :extend, ClassMethods
def self.registered(base)
base.helpers Helpers
base.set :serializer_class, nil
end

def serialize(data, structure = :default)
serializer_class = self.class.serializer_class
module Helpers
def serialize(data, structure = :default)
serializer_class = settings.serializer_class

if serializer_class.nil?
raise <<-eos.strip
No serializer has been specified for this endpoint. Please specify one with
`serializer Serializers::ModelName` in the endpoint.
eos
end
if serializer_class.nil?
raise <<~eos.strip
No serializer has been specified for this endpoint. Please specify one with
`serializer Serializers::ModelName` in the endpoint.
eos
end

env['pliny.serializer_arity'] = data.respond_to?(:size) ? data.size : 1
env['pliny.serializer_arity'] = data.respond_to?(:size) ? data.size : 1

start = Time.now
serializer_class.new(structure).serialize(data).tap do
env['pliny.serializer_timing'] = (Time.now - start).to_f
start = Time.now
serializer_class.new(structure).serialize(data).tap do
env['pliny.serializer_timing'] = (Time.now - start).to_f
end
end
end

module ClassMethods
# Provide a way to specify endpoint serializer class.
#
# class Endpoints::User < Base
# serializer Serializers::User
#
# get do
# encode serialize(User.all)
# end
# end
def serializer(serializer_class)
@serializer_class = serializer_class
end

attr_reader :serializer_class
# Provide a way to specify endpoint serializer class.
#
# class Endpoints::User < Base
# serializer Serializers::User
#
# get do
# encode serialize(User.all)
# end
# end
def serializer(serializer_class)
set :serializer_class, serializer_class
end
end
end
2 changes: 1 addition & 1 deletion lib/template/lib/endpoints/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Base < Sinatra::Base

helpers Pliny::Helpers::Encode
helpers Pliny::Helpers::Params
helpers Pliny::Helpers::Serialize
register Pliny::Helpers::Serialize

set :dump_errors, false
set :raise_errors, true
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/serialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
context "without a serializer" do
def app
Sinatra.new do
helpers Pliny::Helpers::Serialize
register Pliny::Helpers::Serialize

get "/" do
MultiJson.encode(serialize([]))
Expand All @@ -29,7 +29,7 @@ def serialize(data)

def app
Sinatra.new do
helpers Pliny::Helpers::Serialize
register Pliny::Helpers::Serialize

serializer Serializer

Expand Down