Class: Sneakers::ContentEncoding

Inherits:
Object
  • Object
show all
Defined in:
lib/sneakers/content_encoding.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoder, decoder) ⇒ ContentEncoding

Returns a new instance of ContentEncoding.



38
39
40
41
# File 'lib/sneakers/content_encoding.rb', line 38

def initialize(encoder, decoder)
  @encoder = encoder
  @decoder = decoder
end

Instance Attribute Details

#decoderObject (readonly)

Returns the value of attribute decoder.



43
44
45
# File 'lib/sneakers/content_encoding.rb', line 43

def decoder
  @decoder
end

#encoderObject (readonly)

Returns the value of attribute encoder.



43
44
45
# File 'lib/sneakers/content_encoding.rb', line 43

def encoder
  @encoder
end

Class Method Details

.decode(payload, content_encoding) ⇒ Object



23
24
25
26
# File 'lib/sneakers/content_encoding.rb', line 23

def self.decode(payload, content_encoding)
  return payload unless content_encoding
  @_encodings[content_encoding].decoder.(payload)
end

.encode(payload, content_encoding) ⇒ Object



18
19
20
21
# File 'lib/sneakers/content_encoding.rb', line 18

def self.encode(payload, content_encoding)
  return payload unless content_encoding
  @_encodings[content_encoding].encoder.(payload)
end

.passthroughObject



34
35
36
# File 'lib/sneakers/content_encoding.rb', line 34

def self.passthrough
  ->(payload) { payload }
end

.register(content_encoding: nil, encoder: nil, decoder: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sneakers/content_encoding.rb', line 3

def self.register(content_encoding: nil, encoder: nil, decoder: nil)
  # This can be removed when support is dropped for ruby 2.0 and replaced
  # by a keyword arg with no default value
  fail ArgumentError, 'missing keyword: content_encoding' if content_encoding.nil?
  fail ArgumentError, 'missing keyword: encoder' if encoder.nil?
  fail ArgumentError, 'missing keyword: decoder' if decoder.nil?

  fail ArgumentError, "#{content_encoding} encoder must be a proc" unless encoder.is_a? Proc
  fail ArgumentError, "#{content_encoding} decoder must be a proc" unless decoder.is_a? Proc

  fail ArgumentError, "#{content_encoding} encoder must accept one argument, the payload" unless encoder.arity == 1
  fail ArgumentError, "#{content_encoding} decoder must accept one argument, the payload" unless decoder.arity == 1
  @_encodings[content_encoding] = new(encoder, decoder)
end

.reset!Object



28
29
30
31
32
# File 'lib/sneakers/content_encoding.rb', line 28

def self.reset!
  @_encodings = Hash.new(
    new(passthrough, passthrough)
  )
end