Class: Stripe::StripeClient::FaradayStripeEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/stripe/stripe_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

#initializeFaradayStripeEncoder

Returns a new instance of FaradayStripeEncoder.



221
222
223
# File 'lib/stripe/stripe_client.rb', line 221

def initialize
  @cache = {}
end

Instance Method Details

#decode(_str) ⇒ Object

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

Raises:

  • (NotImplementedError)


234
235
236
237
# File 'lib/stripe/stripe_client.rb', line 234

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.



227
228
229
230
231
# File 'lib/stripe/stripe_client.rb', line 227

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