Module: HaveAPI::Server::ServerHelpers
- Defined in:
- lib/haveapi/server.rb
Instance Method Summary collapse
- #access_control ⇒ 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
- #root ⇒ Object
- #setup_formatter ⇒ Object
- #sort_hash(hash) ⇒ Object
- #urlescape(v) ⇒ Object
- #version ⇒ Object
Instance Method Details
#access_control ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/haveapi/server.rb', line 111 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 |
#api_version ⇒ Object
213 214 215 |
# File 'lib/haveapi/server.rb', line 213 def api_version @v end |
#authenticate!(v) ⇒ Object
77 78 79 |
# File 'lib/haveapi/server.rb', line 77 def authenticate!(v) require_auth! unless authenticated?(v) end |
#authenticated?(v) ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/haveapi/server.rb', line 81 def authenticated?(v) return @current_user if @current_user 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]) @current_user end |
#authenticated_versions ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/haveapi/server.rb', line 98 def authenticated_versions settings.api_server.versions.each_with_object({}) do |v, ret| ret[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 end |
#base_url ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/haveapi/server.rb', line 190 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
123 124 125 |
# File 'lib/haveapi/server.rb', line 123 def current_user @current_user end |
#doc(file) ⇒ Object
186 187 188 |
# File 'lib/haveapi/server.rb', line 186 def doc(file) markdown :"../../../doc/#{file}" end |
#host ⇒ Object
201 202 203 |
# File 'lib/haveapi/server.rb', line 201 def host request.env['HTTP_HOST'].split(':').first end |
#logout_url ⇒ Object
181 182 183 184 |
# File 'lib/haveapi/server.rb', line 181 def logout_url ret = url("#{root}_logout") ret.insert(ret.index('//') + 2, '_log:out@') end |
#pretty_format(obj) ⇒ Object
127 128 129 130 |
# File 'lib/haveapi/server.rb', line 127 def pretty_format(obj) ret = '' PP.pp(obj, ret) end |
#report_error(code, headers, msg) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/haveapi/server.rb', line 140 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/haveapi/server.rb', line 151 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] || 'Server error occurred' ) end |
#require_auth! ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/haveapi/server.rb', line 132 def require_auth! report_error( 401, { 'www-authenticate' => 'Basic realm="Restricted Area"' }, 'Action requires user to authenticate' ) end |
#root ⇒ Object
177 178 179 |
# File 'lib/haveapi/server.rb', line 177 def root settings.api_server.root end |
#setup_formatter ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/haveapi/server.rb', line 60 def setup_formatter return if @formatter @formatter = OutputFormatter.new accept = request.accept rescue ArgumentError, EncodingError @formatter.supports?([]) report_error(400, {}, '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 |
#sort_hash(hash) ⇒ Object
209 210 211 |
# File 'lib/haveapi/server.rb', line 209 def sort_hash(hash) hash.sort { |a, b| a[0] <=> b[0] } end |
#urlescape(v) ⇒ Object
205 206 207 |
# File 'lib/haveapi/server.rb', line 205 def urlescape(v) CGI.escape(v) end |