Skip to content

Instantly share code, notes, and snippets.

Created November 9, 2017 22:22

Revisions

  1. @invalid-email-address Anonymous created this gist Nov 9, 2017.
    43 changes: 43 additions & 0 deletions encode.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    # lib/encode.rb

    class Encode
    def initialize(app)
    @app = app
    end

    def call(env)
    @request = Rack::Request.new(env)

    status, headers, response = @app.call(env)

    enc = env['intake']['path']['encodings']
    encoded = encode(enc, response)

    # @response = Rack::Response.new
    # @response.set_header('Content-Type', "application/#{enc}")
    # @response.write encoded
    # @response.status = status
    # @response.write response
    # @response.write "\n"
    # @response.finish

    encoded << "\n"
    [status, headers, encoded]
    end

    attr_reader :request

    private

    def encode(encoding, text)
    case encoding
    when 'json' then text.to_json
    end
    end

    def decode(encoding, code)
    case encoding
    when 'json' then JSON.parse(code)
    end
    end
    end