Module: HaveAPI::Server::ServerHelpers

Defined in:
lib/haveapi/server.rb

Instance Method Summary collapse

Instance Method Details

#access_controlObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/haveapi/server.rb', line 82

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_versionObject



154
155
156
# File 'lib/haveapi/server.rb', line 154

def api_version
  @v
end

#authenticate!(v) ⇒ Object



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

def authenticate!(v)
  require_auth! unless authenticated?(v)
end

#authenticated?(v) ⇒ Boolean

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/haveapi/server.rb', line 65

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.message)
  end
  settings.api_server.call_hooks_for(:post_authenticated, args: [@current_user])
  @current_user
end

#base_urlObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/haveapi/server.rb', line 131

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_userObject



94
95
96
# File 'lib/haveapi/server.rb', line 94

def current_user
  @current_user
end

#doc(file) ⇒ Object



127
128
129
# File 'lib/haveapi/server.rb', line 127

def doc(file)
  markdown :"../../../doc/#{file}"
end

#hostObject



142
143
144
# File 'lib/haveapi/server.rb', line 142

def host
  request.env['HTTP_HOST'].split(':').first
end

#logout_urlObject



122
123
124
125
# File 'lib/haveapi/server.rb', line 122

def logout_url
  ret = url("#{root}_logout")
  ret.insert(ret.index('//') + 2, '_log:out@')
end

#pretty_format(obj) ⇒ Object



98
99
100
101
# File 'lib/haveapi/server.rb', line 98

def pretty_format(obj)
  ret = ''
  PP.pp(obj, ret)
end

#report_error(code, headers, msg) ⇒ Object



111
112
113
114
115
116
# File 'lib/haveapi/server.rb', line 111

def report_error(code, headers, msg)
  @halted = true

  content_type @formatter.content_type, charset: 'utf-8'
  halt code, headers, @formatter.format(false, nil, msg, version: false)
end

#require_auth!Object



103
104
105
106
107
108
109
# File 'lib/haveapi/server.rb', line 103

def require_auth!
  report_error(
    401,
    { 'www-authenticate' => 'Basic realm="Restricted Area"' },
    'Action requires user to authenticate'
  )
end

#rootObject



118
119
120
# File 'lib/haveapi/server.rb', line 118

def root
  settings.api_server.root
end

#setup_formatterObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/haveapi/server.rb', line 48

def setup_formatter
  return if @formatter

  @formatter = OutputFormatter.new

  unless @formatter.supports?(request.accept)
    @halted = true
    halt 406, "Not Acceptable\n"
  end

  content_type @formatter.content_type, charset: 'utf-8'
end

#sort_hash(hash) ⇒ Object



150
151
152
# File 'lib/haveapi/server.rb', line 150

def sort_hash(hash)
  hash.sort { |a, b| a[0] <=> b[0] }
end

#urlescape(v) ⇒ Object



146
147
148
# File 'lib/haveapi/server.rb', line 146

def urlescape(v)
  CGI.escape(v)
end

#versionObject



158
159
160
# File 'lib/haveapi/server.rb', line 158

def version
  HaveAPI::VERSION
end