Class: Supercast::Client::FaradaySupercastEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/supercast/client.rb

Overview

Used to workaround buggy behavior in Faraday: the library will try to reshape anything that we pass to ‘req.params` with one of its default encoders. I don’t think this process is supposed to be lossy, but it is – in particular when we send our integer-indexed maps (i.e. arrays), Faraday ends up stripping out the integer indexes.

We work around the problem by implementing our own simplified encoder and telling Faraday to use that.

The class also performs simple caching so that we don’t have to encode parameters twice for every request (once to build the request and once for logging).

When initialized with ‘multipart: true`, the encoder just inspects the hash instead to get a decent representation for logging. In the case of a multipart request, Faraday won’t use the result of this encoder.

Instance Method Summary collapse

Constructor Details

#initializeFaradaySupercastEncoder

Returns a new instance of FaradaySupercastEncoder.



212
213
214
# File 'lib/supercast/client.rb', line 212

def initialize
  @cache = {}
end

Instance Method Details

#decode(_str) ⇒ Object

We should never need to do this so it’s not implemented.

Raises:

  • (NotImplementedError)


225
226
227
228
# File 'lib/supercast/client.rb', line 225

def decode(_str)
  raise NotImplementedError,
        "#{self.class.name} does not implement #decode"
end

#encode(hash) ⇒ Object

This is quite subtle, but for a ‘multipart/form-data` request Faraday will throw away the result of this encoder and build its body.



218
219
220
221
222
# File 'lib/supercast/client.rb', line 218

def encode(hash)
  @cache.fetch(hash) do |k|
    @cache[k] = Util.encode_parameters(hash)
  end
end