Class: Sinatra::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/doraemon/utils/request.rb,
lib/doraemon/gateway_server.rb

Instance Method Summary collapse

Instance Method Details

#body_paramsObject

将 body 内的数据转为 json 对象



27
28
29
30
31
32
# File 'lib/doraemon/utils/request.rb', line 27

def body_params
  if !self.body.eof?
    $body_params = JSON.parse(self.body.read)
  end
  $body_params
end

#extract_header(*fields) ⇒ Object

抽取 Header



35
36
37
38
39
40
41
42
# File 'lib/doraemon/utils/request.rb', line 35

def extract_header(*fields)
  result = {}
  fields.each do |field|
    key = 'HTTP_' + field.gsub('-', '_').upcase
    result[field] = self.env[key]
  end
  result
end

#mock_api_hostObject

需要 MOCK 转发的主机地址,默认线上



20
21
22
23
24
25
26
# File 'lib/doraemon/gateway_server.rb', line 20

def mock_api_host
  api_host = self.env['HTTP_DORAEMON_API_HOST']
  if api_host.nil? || api_host.length == 0
    api_host = 'https://app.idongjia.cn/'
  end
  api_host
end

#mock_api_pathObject

获取需要 MOCK 的 API 路径



29
30
31
# File 'lib/doraemon/gateway_server.rb', line 29

def mock_api_path
  self.path[5, self.path.length]
end

#mock_usernameObject

MOCK 用户名,默认用户 admin



11
12
13
14
15
16
17
# File 'lib/doraemon/gateway_server.rb', line 11

def mock_username
  username = self.env['HTTP_DORAEMON_USERNAME']
  if username.nil? || username.length == 0
    username = 'admin'
  end
  username
end

#uid_from_tokenObject

从请求的 token 中获取 uid



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/doraemon/utils/request.rb', line 10

def uid_from_token
  uid = 0
  token_cipher = self.env['HTTP_USER_TOKEN']
  if !token_cipher.nil?
    token_cipher = token_cipher.strip
    if token_cipher.length > 0
      token_plain = Doraemon::AES.decrypt_token(token_cipher)
      if !token_plain.nil? && token_plain.length > 0
        token = JSON.parse(token_plain)
        uid = token['uid']
      end
    end
  end
  uid
end