Sha256: 21611ed899796823cae4e7c97cc9e495263391951fbef69afd38e14fb4da9051

Contents?: true

Size: 857 Bytes

Versions: 17

Compression:

Stored size: 857 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 MD5 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)
    Digest::MD5.hexdigest(str)
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
web47core-0.6.1 lib/app/controllers/concerns/restful_controller.rb
web47core-0.6.0 lib/app/controllers/concerns/restful_controller.rb
web47core-0.5.5 lib/app/controllers/concerns/restful_controller.rb
web47core-0.5.4 lib/app/controllers/concerns/restful_controller.rb
web47core-0.5.3 lib/app/controllers/concerns/restful_controller.rb
web47core-0.5.2 lib/app/controllers/concerns/restful_controller.rb
web47core-0.4.5 lib/app/controllers/concerns/restful_controller.rb
web47core-0.4.4 lib/app/controllers/concerns/restful_controller.rb
web47core-0.4.3 lib/app/controllers/concerns/restful_controller.rb
web47core-0.4.2 lib/app/controllers/concerns/restful_controller.rb
web47core-0.4.0 lib/app/controllers/concerns/restful_controller.rb
web47core-0.3.4 lib/app/controllers/concerns/restful_controller.rb
web47core-0.3.3 lib/app/controllers/concerns/restful_controller.rb
web47core-0.3.2 lib/app/controllers/concerns/restful_controller.rb
web47core-0.3.1 lib/app/controllers/concerns/restful_controller.rb
web47core-0.3.0 lib/app/controllers/concerns/restful_controller.rb
web47core-0.1.11 lib/app/controllers/concerns/restful_controller.rb