Class: LinkIO::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/linkio/result.rb

Overview

A framework-agnostic description of the response the caller should send. Client returns these so that the same logic can be wired into Rack, Rails, Sinatra, or anything else.

Constant Summary collapse

HTML_HEADERS =

Header names are lowercase to satisfy Rack 3's strict lint (Rack 2 and Rails accept them too).

{ "content-type" => "text/html; charset=utf-8" }.freeze
JSON_HEADERS =
{ "content-type" => "application/json; charset=utf-8" }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, status:, body:, headers: {}) ⇒ Result

Returns a new instance of Result.



20
21
22
23
24
25
# File 'lib/linkio/result.rb', line 20

def initialize(type:, status:, body:, headers: {})
  @type = type
  @status = status
  @body = body
  @headers = headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



18
19
20
# File 'lib/linkio/result.rb', line 18

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



18
19
20
# File 'lib/linkio/result.rb', line 18

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



18
19
20
# File 'lib/linkio/result.rb', line 18

def status
  @status
end

#typeSymbol (readonly)

Returns one of :redirect, :html, :json.

Returns:

  • (Symbol)

    one of :redirect, :html, :json



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/linkio/result.rb', line 12

class Result
  # Header names are lowercase to satisfy Rack 3's strict lint (Rack 2 and
  # Rails accept them too).
  HTML_HEADERS = { "content-type" => "text/html; charset=utf-8" }.freeze
  JSON_HEADERS = { "content-type" => "application/json; charset=utf-8" }.freeze

  attr_reader :type, :status, :body, :headers

  def initialize(type:, status:, body:, headers: {})
    @type = type
    @status = status
    @body = body
    @headers = headers
  end

  # @return [Result] a 302 redirect to +url+
  def self.redirect(url, status: 302)
    new(type: :redirect, status: status, body: url, headers: { "location" => url })
  end

  # @return [Result] an HTML response
  def self.html(body, status: 200)
    new(type: :html, status: status, body: body, headers: HTML_HEADERS.dup)
  end

  # @param object [Object] serialized to JSON for the body
  # @return [Result] a JSON response (body kept as the raw object too)
  def self.json(object, status: 200)
    new(type: :json, status: status, body: object, headers: JSON_HEADERS.dup)
  end

  def redirect?
    type == :redirect
  end

  def html?
    type == :html
  end

  def json?
    type == :json
  end

  # The location for redirect results.
  # @return [String, nil]
  def location
    headers["location"]
  end

  # The response body as a string, JSON-encoding json results.
  # @return [String]
  def body_string
    json? ? JSON.generate(body) : body.to_s
  end

  # Convert to a standard Rack response triple.
  #
  # @return [Array(Integer, Hash, Array<String>)]
  def to_rack
    [status, headers.dup, [body_string]]
  end
end

Class Method Details

.html(body, status: 200) ⇒ Result

Returns an HTML response.

Returns:

  • (Result)

    an HTML response



33
34
35
# File 'lib/linkio/result.rb', line 33

def self.html(body, status: 200)
  new(type: :html, status: status, body: body, headers: HTML_HEADERS.dup)
end

.json(object, status: 200) ⇒ Result

Returns a JSON response (body kept as the raw object too).

Parameters:

  • object (Object)

    serialized to JSON for the body

Returns:

  • (Result)

    a JSON response (body kept as the raw object too)



39
40
41
# File 'lib/linkio/result.rb', line 39

def self.json(object, status: 200)
  new(type: :json, status: status, body: object, headers: JSON_HEADERS.dup)
end

.redirect(url, status: 302) ⇒ Result

Returns a 302 redirect to url.

Returns:

  • (Result)

    a 302 redirect to url



28
29
30
# File 'lib/linkio/result.rb', line 28

def self.redirect(url, status: 302)
  new(type: :redirect, status: status, body: url, headers: { "location" => url })
end

Instance Method Details

#body_stringString

The response body as a string, JSON-encoding json results.

Returns:

  • (String)


63
64
65
# File 'lib/linkio/result.rb', line 63

def body_string
  json? ? JSON.generate(body) : body.to_s
end

#html?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/linkio/result.rb', line 47

def html?
  type == :html
end

#json?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/linkio/result.rb', line 51

def json?
  type == :json
end

#locationString?

The location for redirect results.

Returns:

  • (String, nil)


57
58
59
# File 'lib/linkio/result.rb', line 57

def location
  headers["location"]
end

#redirect?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/linkio/result.rb', line 43

def redirect?
  type == :redirect
end

#to_rackArray(Integer, Hash, Array<String>)

Convert to a standard Rack response triple.

Returns:

  • (Array(Integer, Hash, Array<String>))


70
71
72
# File 'lib/linkio/result.rb', line 70

def to_rack
  [status, headers.dup, [body_string]]
end