Skip to content

An encoding/decoding library for Ruby using decorator pattern.

Notifications You must be signed in to change notification settings

tonytonyjan/coder_decorator

Repository files navigation

CoderDecorator Build Status Yard Docs

It's a encoding/decoding library for Ruby designed with decorator pattern, which makes it more flexible, and can be wrapped infinitely using Ruby instantiation.

This gem can refers to this pull request.

Install

gem install 'coder_decorator'

Usage

Encode data with Marshal and Base64:

require 'coder_decorator'
include CoderDecorator
coder = Coders::Base64.new(Coders::Marshal.new)
encoded_data = coder.encode(data)
coder.decode(encoded_data)

Encode data with JSON and Zip:

require 'coder_decorator'
include CoderDecorator
coder = Coders::Zip.new(Coders::JSON.new)
encoded_data = coder.encode(data)
coder.decode(encoded_data)

Coders are listed in lib/coder_decorator/coders.

Integration with Rack

require 'rack'
require 'coder_decorator'

include CoderDecorator

app = lambda do |env|
  session = env['rack.session']
  session[:count] ||= 0
  session[:count] += 1
  [200, {}, [session[:count].to_s]]
end

coder = Coders::Rescue.new(
  Coders::HMAC.new(
    Coders::Cipher.new(
      Coders::JSON.new,
      secret: 'x' * 32
    ),
    secret: 'y' * 32
  )
)

app = Rack::Builder.app(app) do
  use Rack::Session::Cookie, coder: coder, let_coder_handle_secure_encoding: true
end

Rack::Handler::WEBrick.run app

About

An encoding/decoding library for Ruby using decorator pattern.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages