Class: RubyAPI::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/fastrb/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, route) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fastrb/context.rb', line 9

def initialize(request, route)
  @request = request
  @route = route
  @params = extract_path_params
  @params.merge!(extract_query_params)
  @body = extract_body
  @files = extract_files
  @session = {}
  @response_headers = {}
  @store = {}
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/fastrb/context.rb', line 6

def body
  @body
end

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/fastrb/context.rb', line 6

def files
  @files
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/fastrb/context.rb', line 6

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/fastrb/context.rb', line 6

def request
  @request
end

#response_bodyObject

Returns the value of attribute response_body.



7
8
9
# File 'lib/fastrb/context.rb', line 7

def response_body
  @response_body
end

#response_headersObject

Returns the value of attribute response_headers.



7
8
9
# File 'lib/fastrb/context.rb', line 7

def response_headers
  @response_headers
end

#response_statusObject

Returns the value of attribute response_status.



7
8
9
# File 'lib/fastrb/context.rb', line 7

def response_status
  @response_status
end

#sessionObject

Returns the value of attribute session.



6
7
8
# File 'lib/fastrb/context.rb', line 6

def session
  @session
end

Instance Method Details

#apply_param_types!Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/fastrb/context.rb', line 66

def apply_param_types!
  type_map = @route[:params]
  type_map.each do |name, type|
    next unless @params.key?(name)
    @params[name] = RubyAPI::ParamConverters.convert(@params[name], type)
  end
rescue RubyAPI::ConversionError => e
  @response_status = 422
  @response_body = { error: e.message }
end

#get(key) ⇒ Object



25
26
27
# File 'lib/fastrb/context.rb', line 25

def get(key)
  @store[key]
end

#set(key, value) ⇒ Object



29
30
31
# File 'lib/fastrb/context.rb', line 29

def set(key, value)
  @store[key] = value
end

#sse(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fastrb/context.rb', line 33

def sse(&block)
  io = @request.env["rack.hijack"]&.call
  return [500, {}, ["rack.hijack not available"]] unless io

  stream = SSEStream.new
  sse_out = SSE.new(io)

  @response_headers["content-type"] = "text/event-stream"
  @response_headers["cache-control"] = "no-cache"
  @response_headers["connection"] = "keep-alive"
  @response_status = 200

  block.call(sse_out)
  sse_out.close
  @response_body = ""
end

#stream(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fastrb/context.rb', line 50

def stream(&block)
  body = StreamingBody.new
  @response_headers["content-type"] = "text/plain"
  @response_headers["transfer-encoding"] = "chunked"
  @response_status = 200
  @response_body = body

  Thread.new do
    begin
      block.call(body)
    ensure
      body.close
    end
  end
end

#validate_body!(schema_class) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/fastrb/context.rb', line 77

def validate_body!(schema_class)
  raw = @body ? (@body.is_a?(BodyProxy) ? @body.to_h : @body) : {}
  validated = schema_class.validate(raw)
  stringified = validated.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
  @body = BodyProxy.new(stringified)
rescue Schema::ValidationError => e
  @response_status = 422
  @response_body = { errors: e.errors }
end