Module: Gotta::Run::Ruby::Loader::Component

Included in:
Runtime::Component
Defined in:
lib/gotta/run/ruby/loader/component.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_handlerObject



32
33
34
# File 'lib/gotta/run/ruby/loader/component.rb', line 32

def self.load_handler
  module_eval("require 'handler'")
end

.require(req) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/gotta/run/ruby/loader/component.rb', line 23

def self.require(req)
  file = "#{req}.rb"
  if File.file?(file)
    module_eval(File.read(file), file)
  else
    super(req)
  end
end

Instance Method Details

#redirect_to(location = nil, function: nil, url: nil, status: 303) ⇒ Object



40
41
42
43
# File 'lib/gotta/run/ruby/loader/component.rb', line 40

def redirect_to(location = nil, function: nil, url: nil, status: 303)
  headers = { 'Location' => location || function || url }
  Runtime::Response.new(body: nil, status: status, headers: headers)
end

#render(js: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, data: nil, png: nil, jpeg: nil, gif: nil, icon: nil, svg: nil, css: nil, status: 200, headers: {}, content_type: nil) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gotta/run/ruby/loader/component.rb', line 45

def render(js: nil,
           body: nil,
           inline: nil,
           html: nil,
           json: nil,
           yaml: nil,
           text: nil,
           data: nil,
           png: nil,
           jpeg: nil,
           gif: nil,
           icon: nil,
           svg: nil,
           css: nil,
           status: 200,
           headers: {},
           content_type: nil)

  headers['Content-Type'] = content_type if content_type
  content_type, resp_body =
    case
    when json
      ['application/json',
       json.is_a?(String) ? json : Oj.dump(json)]
    when text
      ['text/plain',
       text]
    when yaml
      ['text/yaml',
       yaml.is_a?(String) ? yaml : yaml.to_yaml]
    when html, inline
      ['text/html',
       html || inline]
    when body
      ['application/octet-stream',
       Base64.urlsafe_encode64(body, padding: false)]
    when data
      ['application/octet-stream',
       Base64.urlsafe_encode64(data, padding: false)]
    when js
      ['text/javascript',
       js]
    when png
      ['image/png',
       Base64.urlsafe_encode64(File.binread(png), padding: false)]
    when svg
      ['image/svg+xml',
       svg]
    when jpeg
      ['image/jpeg',
       Base64.urlsafe_encode64(File.binread(jpeg), padding: false)]
    when gif
      ['image/gif',
       Base64.urlsafe_encode64(File.binread(gif), padding: false)]
    when icon
      ['image/x-icon',
       Base64.urlsafe_encode64(File.binread(icon), padding: false)]
    when css
      ['text/css',
       css]
    else
      headers.delete('Content-Type')
      return Runtime::Response.new(nil, status, headers)
    end
  headers['Content-Type'] ||= content_type
  Runtime::Response.new(resp_body, status, headers)
end

#render_nothing(status: 200, headers: {}) ⇒ Object



113
114
115
# File 'lib/gotta/run/ruby/loader/component.rb', line 113

def render_nothing(status: 200, headers: {})
  Runtime::Response.new(nil, status, headers)
end

#render_template(template_path, status: 200, headers: {}, variables: {}) ⇒ Object



117
118
119
120
121
# File 'lib/gotta/run/ruby/loader/component.rb', line 117

def render_template(template_path, status: 200, headers: {}, variables: {})
  headers['Content-Type'] ||= 'text/html'
  template = Runtime::Template.new(variables: variables).render(template_path)
  Runtime::Response.new(template, status, headers)
end

#respond_with(body, status: 200, headers: {}) ⇒ Object



36
37
38
# File 'lib/gotta/run/ruby/loader/component.rb', line 36

def respond_with(body, status: 200, headers: {})
  Runtime::Response.new(body: body, status: status, headers: headers)
end