Skip to content

Instantly share code, notes, and snippets.

@cilim
Created September 11, 2019 12:51
Show Gist options
  • Save cilim/edfbf7eaf74c0be3171cc93273ac1f4f to your computer and use it in GitHub Desktop.
Save cilim/edfbf7eaf74c0be3171cc93273ac1f4f to your computer and use it in GitHub Desktop.
JsonApiDeserializer
class JsonApiDeserializer
MEDIA_TYPE = 'application/vnd.api+json'.freeze
def initialize(request)
@request = request
end
def params
deserialized_params if valid?
end
private
def deserialized_params
ActionController::Parameters.new(deserialize(JSON.parse(request_params)))
end
def deserialize(body)
JSONAPI::Deserializable::Resource.call(body)
rescue JSONAPI::Parser::InvalidDocument => error
raise Networking::JsonApiError, error
end
def request_params
request.body.read
end
class MediaTypeError < ArgumentError
def message
I18n.t('media_type_error')
end
end
attr_reader :request
end
module Api
module V1
module Auth
class SessionsController < ApiController
def create
respond_with(user, class: serializer)
end
private
def user
@user ||= ::Users::Operation::Save.new(params: user_params).call
end
def user_params
current_fair_type.authentication.new(session_params).response
end
def session_params
deserialized_params.permit(:email, :password, :terms_accepted_at)
end
def serializer
CurrentUserSerializer
end
# ovo mozes staviti u neki concern i includeati u API base controlleru mozda
def deserialized_params
@deserialized_params ||= JsonApiDeserializer.new(request).params
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment