Class: PUNK::App

Inherits:
Roda
  • Object
show all
Includes:
Loggable
Defined in:
lib/punk/core/app.rb

Constant Summary collapse

REMOTE =
PUNK.env.staging? || PUNK.env.production?
PUBLIC =
File.join(PUNK.get.app.path, "..", "www")
INDEX =
if File.exist?(index_path)
  File.read(index_path)
else
  <<~INDEX_HTML
    <!DOCTYPE html>
    <html>
      <head>
        <title>Let's Punk!</title>
      </head>
      <body>
        <h1>Let's Punk!</h1>
        <p>Are you <a href="https://github.com/kranzky/lets-punk">ready</a> to rock?</p>
      </body>
    </html>
  INDEX_HTML
end
PUNK_CONTENT_TYPE_LOOKUP =
{
  csv: "text/csv",
  html: "text/html",
  json: "application/json",
  xml: "application/xml"
}.freeze

Instance Method Summary collapse

Methods included from Loggable

#exception, #logger, #profile_debug, #profile_info, #profile_trace

Instance Method Details

#argsObject



117
118
119
# File 'lib/punk/core/app.rb', line 117

def args
  params.transform_keys(&:to_sym)
end

#current_identityObject



125
126
127
# File 'lib/punk/core/app.rb', line 125

def current_identity
  @_current_session&.identity
end

#current_sessionObject



121
122
123
# File 'lib/punk/core/app.rb', line 121

def current_session
  @_current_session
end

#current_tenantObject



133
134
135
# File 'lib/punk/core/app.rb', line 133

def current_tenant
  @_current_tenant
end

#current_userObject



129
130
131
# File 'lib/punk/core/app.rb', line 129

def current_user
  @_current_session&.user
end

#perform(action_class, **kwargs) ⇒ Object



137
138
139
140
# File 'lib/punk/core/app.rb', line 137

def perform(action_class, **kwargs)
  raise InternalServerError, "Not an action: #{action_class}" unless action_class < Action
  render action_class.perform(**kwargs)
end

#present(view_class, **kwargs) ⇒ Object



142
143
144
145
# File 'lib/punk/core/app.rb', line 142

def present(view_class, **kwargs)
  raise InternalServerError, "Not a view: #{view_class}" unless view_class < View
  render view_class.present(**kwargs)
end

#render(view) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/punk/core/app.rb', line 153

def render(view)
  raise InternalServerError, "Not a view: #{view}" unless view.is_a?(View)
  format = request.requested_type
  view.profile_info("render", format: format) do
    response.status = view.status if view.is_a?(Fail)
    response["Content-Type"] = PUNK_CONTENT_TYPE_LOOKUP[format]
    view.render(format)
  end
end

#require_anonymous!Object

Raises:



106
107
108
109
# File 'lib/punk/core/app.rb', line 106

def require_anonymous!
  raise BadRequest, "Session already exists" if request.session.present?
  PUNK.logger.info "require_anonymous!"
end

#require_session!Object

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/punk/core/app.rb', line 89

def require_session!
  begin
    @_current_session = Session[request.session["session_id"]]
    if @_current_session&.active?
      @_current_session.touch
    else
      clear_session
      @_current_session = nil
    end
  rescue => e
    exception(e)
    raise Unauthorized, e.message
  end
  raise Unauthorized, "Session does not exist" if @_current_session.nil?
  PUNK.logger.info "require_session!", {current_session: current_session.inspect, current_identity: current_identity.inspect, current_user: current_user.inspect}.inspect
end

#require_tenant!Object

Raises:



111
112
113
114
115
# File 'lib/punk/core/app.rb', line 111

def require_tenant!
  raise Unauthorized, "Session does not exist" if @_current_session.nil?
  @_current_tenant = current_user.tenants_dataset[id: params[:tenant_id]]
  PUNK.logger.info "require_tenant!", {current_tenant: @_current_tenant.inspect}.inspect
end