Class: Mirakl::MiraklClient::FaradayMiraklEncoder
- Inherits:
-
Object
- Object
- Mirakl::MiraklClient::FaradayMiraklEncoder
- Defined in:
- lib/mirakl/mirakl_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
-
#decode(_str) ⇒ Object
We should never need to do this so it’s not implemented.
-
#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.
-
#initialize ⇒ FaradayMiraklEncoder
constructor
A new instance of FaradayMiraklEncoder.
Constructor Details
#initialize ⇒ FaradayMiraklEncoder
Returns a new instance of FaradayMiraklEncoder.
228 229 230 |
# File 'lib/mirakl/mirakl_client.rb', line 228 def initialize @cache = {} end |
Instance Method Details
#decode(_str) ⇒ Object
We should never need to do this so it’s not implemented.
241 242 243 244 |
# File 'lib/mirakl/mirakl_client.rb', line 241 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.
234 235 236 237 238 |
# File 'lib/mirakl/mirakl_client.rb', line 234 def encode(hash) @cache.fetch(hash) do |k| @cache[k] = Util.encode_parameters(hash) end end |