Class: Trailblazer::Endpoint::Handlers::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/endpoint/rails.rb

Overview

Generic matcher handlers for a Rails API backend.

Note that the path mechanics are experimental. PLEASE LET US KNOW WHAT YOU NEED/HOW YOU DID IT: gitter.im/trailblazer/chat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, options) ⇒ Rails

Returns a new instance of Rails.



9
10
11
12
# File 'lib/trailblazer/endpoint/rails.rb', line 9

def initialize(controller, options)
  @controller = controller
  @path       = options[:path]
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



14
15
16
# File 'lib/trailblazer/endpoint/rails.rb', line 14

def controller
  @controller
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/trailblazer/endpoint/rails.rb', line 16

def call
  ->(m) do
    m.not_found       { |result| controller.head 404 }
    m.unauthenticated { |result| controller.head 401 }
    m.present         { |result| controller.render json: result["representer.serializer.class"].new(result['model']), status: 200 }
    m.created         { |result| controller.head 201, location: "#{@path}/#{result["model"].id}" }#, result["representer.serializer.class"].new(result["model"]).to_json
    m.success         { |result| controller.head 200, location: "#{@path}/#{result["model"].id}" }
    m.invalid         { |result| controller.render json: result["representer.errors.class"].new(result['result.contract.default'].errors).to_json, status: 422 }
  end
end