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



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

def args
  params.transform_keys(&:to_sym)
end

#current_identityObject



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

def current_identity
  @_current_session&.identity
end

#current_sessionObject



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

def current_session
  @_current_session
end

#current_tenantObject



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

def current_tenant
  @_current_tenant
end

#current_userObject



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

def current_user
  @_current_session&.user
end

#perform(action_class, **kwargs) ⇒ Object



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

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



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

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



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

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:



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

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
105
# File 'lib/punk/core/app.rb', line 89

def require_session!
  begin
    # TODO
    @_current_session = nil # Session[request.session['session_id']]
    if @_current_session&.active?
      @_current_session.touch
    else
      clear_session
      @_current_session = nil
    end
  rescue StandardError => 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:



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

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