Class: BraintreeHttp::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/braintreehttp/encoder.rb

Instance Method Summary collapse

Constructor Details

#initializeEncoder

Returns a new instance of Encoder.



5
6
7
# File 'lib/braintreehttp/encoder.rb', line 5

def initialize
  @encoders = [Json.new, Text.new, Multipart.new]
end

Instance Method Details

#_encoder(content_type) ⇒ Object



37
38
39
40
41
# File 'lib/braintreehttp/encoder.rb', line 37

def _encoder(content_type)
  idx = @encoders.index { |enc| enc.content_type.match(content_type) }

  @encoders[idx] if idx
end

#deserialize_response(resp, headers) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/braintreehttp/encoder.rb', line 21

def deserialize_response(resp, headers)
  raise UnsupportedEncodingError.new('HttpResponse did not have Content-Type header set') unless headers && (headers['content-type'] || headers['Content-Type'])

  content_type = headers['content-type'] || headers['Content-Type']
  content_type = content_type.first if content_type.kind_of?(Array)

  enc = _encoder(content_type)
  raise UnsupportedEncodingError.new("Unable to deserialize response with Content-Type #{content_type}. Supported decodings are #{supported_encodings}") unless enc

  enc.decode(resp)
end

#serialize_request(req) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/braintreehttp/encoder.rb', line 9

def serialize_request(req)
  raise UnsupportedEncodingError.new('HttpRequest did not have Content-Type header set') unless req.headers && (req.headers['content-type'] || req.headers['Content-Type'])

  content_type = req.headers['content-type'] || req.headers['Content-Type']
  content_type = content_type.first if content_type.kind_of?(Array)

  enc = _encoder(content_type)
  raise UnsupportedEncodingError.new("Unable to serialize request with Content-Type #{content_type}. Supported encodings are #{supported_encodings}") unless enc

  enc.encode(req)
end

#supported_encodingsObject



33
34
35
# File 'lib/braintreehttp/encoder.rb', line 33

def supported_encodings
  @encoders.map { |enc| enc.content_type.inspect }
end