Module: RestfulController

Defined in:
lib/app/controllers/concerns/restful_controller.rb

Overview

Reusable API methods

Instance Method Summary collapse

Instance Method Details

#hash(str) ⇒ Object

Create a hash of the string contents



31
32
33
34
35
36
37
# File 'lib/app/controllers/concerns/restful_controller.rb', line 31

def hash(str)
  if SystemConfiguration.fips_mode?
    Digest::SHA2.hexdigest(str)
  else
    Digest::MD5.hexdigest(str)
  end
end

#render_json_response(success = true, results = {}, status_code = :ok) ⇒ Object

Render a JSON response with the results and status passed in. The payload will include an SHA1 hash along with the response for validation.



11
12
13
14
15
16
17
# File 'lib/app/controllers/concerns/restful_controller.rb', line 11

def render_json_response(success = true, results = {}, status_code = :ok)
  results[:success] = success
  render status: status_code,
         json: { results: results,
                 md5: hash(unescape_unicode(results.to_json)) }.to_json,
         layout: false
end

#unescape_unicode(str) ⇒ Object

Convert UTF-8 unicode/escaped back to actual double byte character



22
23
24
25
26
# File 'lib/app/controllers/concerns/restful_controller.rb', line 22

def unescape_unicode(str)
  str.gsub(/\\u([\da-fA-F]{4})/) do |_m|
    [Regexp.last_match[1]].pack('H*').unpack('n*').pack('U*')
  end
end