Class: Poncho::Method
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Params
included
Methods included from Filters
included
#errors, included, #invalid?, #valid?
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
42
43
44
|
# File 'lib/poncho/method.rb', line 42
def env
@env
end
|
#request ⇒ Object
Returns the value of attribute request.
42
43
44
|
# File 'lib/poncho/method.rb', line 42
def request
@request
end
|
#response ⇒ Object
Returns the value of attribute response.
42
43
44
|
# File 'lib/poncho/method.rb', line 42
def response
@response
end
|
Class Method Details
.before(options = {}, &block) ⇒ Object
21
22
23
|
# File 'lib/poncho/method.rb', line 21
def self.before(options = {}, &block)
add_filter(:before, options, &block)
end
|
.before_validation(options = {}, &block) ⇒ Object
25
26
27
|
# File 'lib/poncho/method.rb', line 25
def self.before_validation(options = {}, &block)
add_filter(:before_validation, options, &block)
end
|
.call(env, params = {}) ⇒ Object
7
8
9
|
# File 'lib/poncho/method.rb', line 7
def self.call(env, params = {})
self.new.call(env, params)
end
|
.error(type = :base, &block) ⇒ Object
33
34
35
|
# File 'lib/poncho/method.rb', line 33
def self.error(type = :base, &block)
errors[type] = block
end
|
.errors ⇒ Object
29
30
31
|
# File 'lib/poncho/method.rb', line 29
def self.errors
@errors ||= {}
end
|
.helpers(*extensions, &block) ⇒ Object
37
38
39
40
|
# File 'lib/poncho/method.rb', line 37
def self.helpers(*extensions, &block)
class_eval(&block) if block_given?
include(*extensions) if extensions.any?
end
|
.to_proc ⇒ Object
Some magic so you can do one-line Sinatra routes. For example:
get '/charges', &ChargesListMethod
14
15
16
17
|
# File 'lib/poncho/method.rb', line 14
def self.to_proc
this = self
Proc.new { this.call(env, params) }
end
|
Instance Method Details
#body(value = nil, &block) ⇒ Object
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/poncho/method.rb', line 117
def body(value = nil, &block)
if block_given?
def block.each; yield(call) end
response.body = block
elsif value
response.body = value
else
response.body
end
end
|
#call(env, params = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/poncho/method.rb', line 44
def call(env, params = {})
@env = env
@request = Request.new(env)
@response = Response.new
@request.params.merge!(params)
wrap {
validate!
dispatch!
}
unless @response['Content-Type']
if Array === body and body[0].respond_to? :content_type
content_type body[0].content_type
else
content_type :html
end
end
@response.finish
end
|
#client_error? ⇒ Boolean
whether or not the status is set to 4xx
141
142
143
|
# File 'lib/poncho/method.rb', line 141
def client_error?
status.between? 400, 499
end
|
#content_type(type = nil, params = {}) ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/poncho/method.rb', line 109
def content_type(type = nil, params = {})
return response['Content-Type'] unless type
default = params.delete :default
mime_type = mime_type(type) || default
fail "Unknown media type: %p" % type if mime_type.nil?
response['Content-Type'] = mime_type.dup
end
|
#error(code, body = nil) ⇒ Object
162
163
164
165
166
|
# File 'lib/poncho/method.rb', line 162
def error(code, body=nil)
code, body = 500, code.to_str if code.respond_to? :to_str
self.body(body) unless body.nil?
halt code
end
|
#halt(*response) ⇒ Object
157
158
159
160
|
# File 'lib/poncho/method.rb', line 157
def halt(*response)
response = response.first if response.length == 1
throw :halt, response
end
|
68
69
70
71
|
# File 'lib/poncho/method.rb', line 68
def (hash=nil)
response..merge! hash if hash
response.
end
|
#invoke ⇒ Object
174
175
|
# File 'lib/poncho/method.rb', line 174
def invoke
end
|
#not_found(body = nil) ⇒ Object
168
169
170
|
# File 'lib/poncho/method.rb', line 168
def not_found(body=nil)
error 404, body
end
|
#not_found? ⇒ Boolean
whether or not the status is set to 404
151
152
153
|
# File 'lib/poncho/method.rb', line 151
def not_found?
status == 404
end
|
#param(name) ⇒ Object
80
81
82
83
84
|
# File 'lib/poncho/method.rb', line 80
def param(name)
value = param_before_type_cast(name)
param = self.class.params[name.to_sym]
param ? param.convert(value) : value
end
|
#param?(name) ⇒ Boolean
Also known as:
param_for_validation?
86
87
88
|
# File 'lib/poncho/method.rb', line 86
def param?(name)
request.params.has_key?(name.to_s)
end
|
#param_before_type_cast(name) ⇒ Object
Also known as:
read_attribute_for_validation
90
91
92
|
# File 'lib/poncho/method.rb', line 90
def param_before_type_cast(name)
request.params[name.to_s]
end
|
#params ⇒ Object
73
74
75
76
77
78
|
# File 'lib/poncho/method.rb', line 73
def params
request.params.inject({}) do |hash, (key, _)|
hash[key.to_sym] = param(key)
hash
end
end
|
#redirect(uri, *args) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/poncho/method.rb', line 99
def redirect(uri, *args)
if env['HTTP_VERSION'] == 'HTTP/1.1' and env['REQUEST_METHOD'] != 'GET'
status 303
else
status 302
end
response['Location'] = uri
halt(*args)
end
|
#redirect? ⇒ Boolean
whether or not the status is set to 3xx
136
137
138
|
# File 'lib/poncho/method.rb', line 136
def redirect?
status.between? 300, 399
end
|
#server_error? ⇒ Boolean
whether or not the status is set to 5xx
146
147
148
|
# File 'lib/poncho/method.rb', line 146
def server_error?
status.between? 500, 599
end
|
#status(value = nil) ⇒ Object
94
95
96
97
|
# File 'lib/poncho/method.rb', line 94
def status(value=nil)
response.status = value if value
response.status
end
|
#success? ⇒ Boolean
whether or not the status is set to 2xx
131
132
133
|
# File 'lib/poncho/method.rb', line 131
def success?
status.between? 200, 299
end
|