Module: HaveAPI::Server::ServerHelpers
- Defined in:
- lib/haveapi/server.rb
Instance Method Summary collapse
- #access_control ⇒ Object
- #add_vary_header(name) ⇒ Object
- #api_version ⇒ Object
- #authenticate!(v) ⇒ Object
- #authenticated?(v) ⇒ Boolean
- #authenticated_versions ⇒ Object
- #base_url ⇒ Object
- #current_user ⇒ Object
- #doc(file) ⇒ Object
- #host ⇒ Object
- #logout_url ⇒ Object
- #pretty_format(obj) ⇒ Object
- #report_error(code, headers, msg) ⇒ Object
- #report_exception(exception, context = nil) ⇒ Object
- #require_auth! ⇒ Object
- #resolve_request_locale(user = current_user) ⇒ Object
- #restore_request_locale ⇒ Object
- #root ⇒ Object
- #setup_formatter ⇒ Object
- #setup_request_locale ⇒ Object
- #sort_hash(hash) ⇒ Object
- #urlescape(v) ⇒ Object
- #version ⇒ Object
Instance Method Details
#access_control ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/haveapi/server.rb', line 163 def access_control return unless request.env['HTTP_ORIGIN'] && request.env['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] halt 200, { 'access-control-allow-origin' => '*', 'access-control-allow-methods' => 'GET,POST,OPTIONS,PATCH,PUT,DELETE', 'access-control-allow-credentials' => 'false', 'access-control-allow-headers' => settings.api_server.allowed_headers, 'access-control-max-age' => (60 * 60).to_s }, '' end |
#add_vary_header(name) ⇒ Object
99 100 101 102 103 |
# File 'lib/haveapi/server.rb', line 99 def add_vary_header(name) values = response['Vary'].to_s.split(/\s*,\s*/).reject(&:empty?) values << name unless values.include?(name) headers 'Vary' => values.join(', ') end |
#api_version ⇒ Object
265 266 267 |
# File 'lib/haveapi/server.rb', line 265 def api_version @v end |
#authenticate!(v) ⇒ Object
122 123 124 |
# File 'lib/haveapi/server.rb', line 122 def authenticate!(v) require_auth! unless authenticated?(v) end |
#authenticated?(v) ⇒ Boolean
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/haveapi/server.rb', line 126 def authenticated?(v) if @current_user resolve_request_locale return @current_user end begin @current_user = settings.api_server.send(:do_authenticate, v, request) rescue HaveAPI::Authentication::TokenConflict => e unless @formatter @formatter = OutputFormatter.new @formatter.supports?([]) end report_error(400, {}, e.) end settings.api_server.call_hooks_for(:post_authenticated, args: [@current_user]) resolve_request_locale @current_user end |
#authenticated_versions ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/haveapi/server.rb', line 147 def authenticated_versions ret = settings.api_server.versions.each_with_object({}) do |v, users| users[v] = settings.api_server.send(:do_authenticate, v, request) rescue HaveAPI::Authentication::TokenConflict => e unless @formatter @formatter = OutputFormatter.new @formatter.supports?([]) end report_error(400, {}, e.) end resolve_request_locale(ret[settings.api_server.default_version] || ret.values.compact.first) ret end |
#base_url ⇒ Object
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/haveapi/server.rb', line 242 def base_url scheme = if request.env['HTTP_X_FORWARDED_SSL'] == 'on' 'https' else request.env['rack.url_scheme'] end "#{scheme}://#{request.env['HTTP_HOST']}" end |
#current_user ⇒ Object
175 176 177 |
# File 'lib/haveapi/server.rb', line 175 def current_user @current_user end |
#doc(file) ⇒ Object
238 239 240 |
# File 'lib/haveapi/server.rb', line 238 def doc(file) markdown :"../../../doc/#{file}" end |
#host ⇒ Object
253 254 255 |
# File 'lib/haveapi/server.rb', line 253 def host request.env['HTTP_HOST'].split(':').first end |
#logout_url ⇒ Object
233 234 235 236 |
# File 'lib/haveapi/server.rb', line 233 def logout_url ret = url("#{root}_logout") ret.insert(ret.index('//') + 2, '_log:out@') end |
#pretty_format(obj) ⇒ Object
179 180 181 182 |
# File 'lib/haveapi/server.rb', line 179 def pretty_format(obj) ret = '' PP.pp(obj, ret) end |
#report_error(code, headers, msg) ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/haveapi/server.rb', line 192 def report_error(code, headers, msg) @halted = true unless @formatter @formatter = OutputFormatter.new @formatter.supports?([]) end content_type @formatter.content_type, charset: 'utf-8' halt code, headers, @formatter.format(false, nil, msg, version: false) end |
#report_exception(exception, context = nil) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/haveapi/server.rb', line 203 def report_exception(exception, context = nil) context ||= Context.new( settings.api_server, request: self, params:, endpoint: true ) tmp = begin settings.api_server.call_hooks_for( :request_exception, args: [context, exception] ) rescue StandardError => e warn "HaveAPI request exception hook failed: #{e.class}: #{e.}" {} end report_error( tmp[:http_status] || 500, {}, tmp[:message] || HaveAPI.('haveapi.errors.server_error') ) end |
#require_auth! ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/haveapi/server.rb', line 184 def require_auth! report_error( 401, { 'www-authenticate' => 'Basic realm="Restricted Area"' }, HaveAPI.('haveapi.authentication.required') ) end |
#resolve_request_locale(user = current_user) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/haveapi/server.rb', line 82 def resolve_request_locale(user = current_user) setup_request_locale return @haveapi_locale if @haveapi_locale_requested @haveapi_locale = settings.api_server.resolved_locale( request:, current_user: user ) settings.api_server.activate_locale(@haveapi_locale) end |
#restore_request_locale ⇒ Object
93 94 95 96 97 |
# File 'lib/haveapi/server.rb', line 93 def restore_request_locale return unless @haveapi_locale_setup settings.api_server.activate_locale(@haveapi_previous_locale) end |
#root ⇒ Object
229 230 231 |
# File 'lib/haveapi/server.rb', line 229 def root settings.api_server.root end |
#setup_formatter ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/haveapi/server.rb', line 105 def setup_formatter return if @formatter @formatter = OutputFormatter.new accept = request.accept rescue ArgumentError, EncodingError @formatter.supports?([]) report_error(400, {}, HaveAPI.('haveapi.errors.bad_accept_header')) else unless @formatter.supports?(accept) @halted = true halt 406, "Not Acceptable\n" end content_type @formatter.content_type, charset: 'utf-8' end |
#setup_request_locale ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/haveapi/server.rb', line 61 def setup_request_locale return if @haveapi_locale_setup server = settings.api_server @haveapi_previous_locale = ::I18n.locale locale_header_value = server.locale_header_value(request) @haveapi_locale_requested = !locale_header_value.nil? @haveapi_explicit_locale = server.request_locale(request) @haveapi_locale = if @haveapi_locale_requested @haveapi_explicit_locale || server.default_locale else server.resolved_locale( request:, current_user: nil ) end server.activate_locale(@haveapi_locale) add_vary_header(server.locale_header) if server.locale_header @haveapi_locale_setup = true end |
#sort_hash(hash) ⇒ Object
261 262 263 |
# File 'lib/haveapi/server.rb', line 261 def sort_hash(hash) hash.sort { |a, b| a[0] <=> b[0] } end |
#urlescape(v) ⇒ Object
257 258 259 |
# File 'lib/haveapi/server.rb', line 257 def urlescape(v) CGI.escape(v) end |