Sha256: 6a5460d013a74cacdf7c628ef1862a68606fbc9f20cfcc447cc11bb9136f00a3

Contents?: true

Size: 949 Bytes

Versions: 89

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

#
# Reusable API methods
#
module RestfulController
  #
  # Render a JSON response with the results and status passed in. The payload will include
  # an SHA1 hash along with the response for validation.
  #
  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

  #
  # Convert UTF-8 unicode/escaped back to actual double byte character
  #
  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

  #
  # Create a hash of the string contents
  #
  def hash(str)
    if SystemConfiguration.fips_mode?
      Digest::SHA2.hexdigest(str)
    else
      Digest::MD5.hexdigest(str)
    end
  end
end

Version data entries

89 entries across 89 versions & 1 rubygems

Version Path
web47core-3.2.20 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.19 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.18 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.17 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.16 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.15 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.14 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.13 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.12 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.9 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.8 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.7 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.6 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.5 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.4 lib/app/controllers/concerns/restful_controller.rb
web47core-2.2.20 lib/app/controllers/concerns/restful_controller.rb
web47core-2.2.19 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.3 lib/app/controllers/concerns/restful_controller.rb
web47core-3.2.2 lib/app/controllers/concerns/restful_controller.rb
web47core-2.2.15 lib/app/controllers/concerns/restful_controller.rb