Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonytonyjan/coder_decorator
An encoding/decoding library for Ruby using decorator pattern.
https://github.com/tonytonyjan/coder_decorator
coder cookie decorator-pattern encoding rack ruby
Last synced: about 1 month ago
JSON representation
An encoding/decoding library for Ruby using decorator pattern.
- Host: GitHub
- URL: https://github.com/tonytonyjan/coder_decorator
- Owner: tonytonyjan
- Created: 2016-12-10T18:50:20.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-09T12:33:10.000Z (almost 7 years ago)
- Last Synced: 2024-10-31T13:58:13.325Z (about 2 months ago)
- Topics: coder, cookie, decorator-pattern, encoding, rack, ruby
- Language: Ruby
- Size: 40 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CoderDecorator [![Build Status](https://travis-ci.org/tonytonyjan/coder_decorator.svg?branch=master)](https://travis-ci.org/tonytonyjan/coder_decorator) [![Yard Docs](https://img.shields.io/badge/yard-doc-blue.svg)](http://www.rubydoc.info/gems/coder_decorator/)
It's a encoding/decoding library for Ruby designed with [decorator pattern](https://en.wikipedia.org/wiki/Decorator_pattern), which makes it more flexible, and can be wrapped infinitely using Ruby instantiation.
This gem can refers to [this pull request](https://github.com/rack/rack/pull/1134).
# Install
```
gem install 'coder_decorator'
```# Usage
Encode data with Marshal and Base64:
```ruby
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:
```ruby
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](lib/coder_decorator/coders).
## Integration with Rack
```ruby
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]]
endcoder = 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
endRack::Handler::WEBrick.run app
```