Class: Runo

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

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Defined Under Namespace

Modules: Error, I18n, Meta, Parser, Path, REX, Set Classes: Checkbox, Field, File, Img, Password, Radio, Select, Storage, Text, Textarea, Workflow

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object



32
33
34
35
# File 'lib/runo.rb', line 32

def self.[](name)
  @config ||= {}
  @config[name]
end

.baseObject



61
62
63
# File 'lib/runo.rb', line 61

def self.base
  self.current[:base]
end

.clientObject



49
50
51
# File 'lib/runo.rb', line 49

def self.client
  self.session[:client] ||= 'nobody'
end

.client=(id) ⇒ Object



53
54
55
# File 'lib/runo.rb', line 53

def self.client=(id)
  self.session[:client] = id
end

.config(config) ⇒ Object



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

def self.config(config)
  @config = config
end

.currentObject



37
38
39
# File 'lib/runo.rb', line 37

def self.current
  Thread.current
end

.libdirObject



69
70
71
# File 'lib/runo.rb', line 69

def self.libdir
  ::File.dirname __FILE__
end

.sessionObject



41
42
43
# File 'lib/runo.rb', line 41

def self.session
  self.current[:session] || ($fake_session ||= {})
end

.static(env) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/runo.rb', line 73

def self.static(env)
  @static ||= Rack::Directory.new Runo['skin_dir']
  response = @static.call env

  if response.first == 404
    until ::File.readable? ::File.join(
      Runo['skin_dir'],
      env['PATH_INFO'].sub(%r{(/#{Runo::REX::DIR_STATIC}/).*}, '\\1')
    )
      env['PATH_INFO'].sub!(%r{/[^/]+(?=/#{Runo::REX::DIR_STATIC}/)}, '') || break
    end
    @static.call env
  else
    response
  end
end

.tokenObject



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

def self.token
  self.session[:token] ||= rand(36 ** 32).to_s(36)
end

.transactionObject



45
46
47
# File 'lib/runo.rb', line 45

def self.transaction
  self.session[:transaction] ||= {}
end

.uriObject



65
66
67
# File 'lib/runo.rb', line 65

def self.uri
  self.current[:uri]
end

Instance Method Details

#call(env) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/runo.rb', line 90

def call(env)
  uri    = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}"
  req    = Rack::Request.new env
  method = req.request_method.downcase
  params = params_from_request req
  path   = req.path_info
  tid    = Runo::Path.tid_of path

  static_path = ::File.expand_path path
  return Runo.static(env) if (
    static_path =~ %r{/#{Runo::REX::DIR_STATIC}/} &&
    static_path !~ %r{/#{Runo::REX::TID}/} &&
    static_path !~ %r{/#{Runo::REX::ID.to_s.sub('^','')}/}
  )

  Runo::I18n.lang = env['HTTP_ACCEPT_LANGUAGE']

  Runo.current[:env]     = env
  Runo.current[:uri]     = uri
  Runo.current[:req]     = req
  Runo.current[:session] = env['rack.session']

  if Runo.transaction[tid].is_a? Runo::Field
    base = Runo.transaction[tid].item Runo::Path.steps_of(path.sub(/\A.*#{Runo::REX::TID}/, ''))
  else
    base = Runo::Path.base_of path
  end
  return response_not_found unless base

  base[:tid] = tid
  Runo.current[:base] = base

  begin
    if params[:action] == :logout && params[:token] == Runo.token
      logout(base, params)
    elsif method == 'get'
      get(base, params)
    elsif params[:action] == :login
      (base, params)
    elsif params[:action] == :preview
      preview(base, params)
    elsif params[:token] != Runo.token
      response_forbidden(:body => 'invalid token')
    elsif Runo.transaction[tid] && !Runo.transaction[tid].is_a?(Runo::Field)
      response_unprocessable_entity(:body => 'transaction expired')
    else
      begin
        post(base, params)
      rescue Runo::Error::Forbidden
        response_forbidden
      end
    end
  rescue Runo::Error::Forbidden
    if params[:action] && Runo.client == 'nobody'
      params[:dest_action] = (method == 'post') ? :index : params[:action]
      params[:action] = :login
    end
    response_unprocessable_entity(:body => _get(base, params)) rescue response_forbidden
# TODO: rescue Error::System etc.
  end
end