braintreehttp 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 599e49054f1dc90ce4af2229aba6e6bb6140143e
4
- data.tar.gz: d9582efa699f71babf7f411744ca33efb34f2f46
3
+ metadata.gz: c27ebe667c362f44516138044f39c78650e219d0
4
+ data.tar.gz: 1c28e5cfaccc55dbd25aebd705c5c85ed5a732c8
5
5
  SHA512:
6
- metadata.gz: 2e0fca1e69a0bf2bac1e6445bfc67d433c8323d8cdeeabe3b08437ec5c3fe1a0ce1225b0f68e98d0e72495c0e63905509f57af407c04a6ca2788994abb686e16
7
- data.tar.gz: e1c67247bcf30af276952d34601a868d905b99e25cdcb1725ed6d7f66c95a1d5c08075a0b888cbd55212f153ee73dc510a67826302a8769f06613f1cb8ef5357
6
+ metadata.gz: f54685a7c757914557ba87bfff0676d10dbd4fbf5e90d2e9b1f8570c1317dd39bf6c62438eb935ceefbd146b898f7f4dec5a76cfe65ec21b347f0a19cc09845b
7
+ data.tar.gz: 7dd8391649bcf0f8a6797d4ed0cfadff17f0741886664018980e56af4b2a150cae3473d26b32577bd605a9b52e8ad18a4a997b8c9a87dd45648a76bbd1366436
@@ -4,7 +4,7 @@ Gem::Specification.new do |s|
4
4
  s.name = "braintreehttp"
5
5
  s.summary = "BraintreeHttp Client Library"
6
6
  s.description = "Used for generated API clients"
7
- s.version = "0.1.3"
7
+ s.version = "0.1.4"
8
8
  s.license = "MIT"
9
9
  s.author = "Braintree"
10
10
  s.email = "code@getbraintree.com"
@@ -3,31 +3,72 @@ require 'json'
3
3
  module BraintreeHttp
4
4
 
5
5
  class Encoder
6
+ def initialize
7
+ @encoders = [Json.new, Text.new]
8
+ end
6
9
 
7
10
  def serialize_request(req)
8
11
  raise UnsupportedEncodingError.new('HttpRequest did not have Content-Type header set') unless req.headers && (req.headers['content-type'] || req.headers['Content-Type'])
9
12
 
10
13
  content_type = req.headers['content-type'] || req.headers['Content-Type']
11
- raise UnsupportedEncodingError.new("Unable to serialize request with Content-Type #{content_type}. Supported encodings are #{supported_encodings}") unless content_type == 'application/json'
14
+ content_type = content_type.first if content_type.kind_of?(Array)
15
+
16
+ enc = _encoder(content_type)
17
+ raise UnsupportedEncodingError.new("Unable to serialize request with Content-Type #{content_type}. Supported encodings are #{supported_encodings}") unless enc
12
18
 
13
- JSON.generate(req.body)
19
+ enc.encode(req.body)
14
20
  end
15
21
 
16
22
  def deserialize_response(resp, headers)
17
23
  raise UnsupportedEncodingError.new('HttpResponse did not have Content-Type header set') unless headers && (headers['content-type'] || headers['Content-Type'])
18
24
 
19
25
  content_type = headers['content-type'] || headers['Content-Type']
20
- raise UnsupportedEncodingError.new("Unable to deserialize response with Content-Type #{content_type}. Supported decodings are #{supported_decodings}") unless content_type.include? 'application/json'
26
+ content_type = content_type.first if content_type.kind_of?(Array)
27
+
28
+ enc = _encoder(content_type)
29
+ raise UnsupportedEncodingError.new("Unable to deserialize response with Content-Type #{content_type}. Supported decodings are #{supported_encodings}") unless enc
21
30
 
22
- JSON.parse(resp)
31
+ enc.decode(resp)
23
32
  end
24
33
 
25
34
  def supported_encodings
26
- ['application/json']
35
+ @encoders.map { |enc| enc.content_type.inspect }
36
+ end
37
+
38
+ def _encoder(content_type)
39
+ idx = @encoders.index { |enc| enc.content_type.match(content_type) }
40
+
41
+ @encoders[idx] if idx
42
+ end
43
+ end
44
+
45
+ class Json
46
+
47
+ def encode(body)
48
+ JSON.generate(body)
49
+ end
50
+
51
+ def decode(body)
52
+ JSON.parse(body)
53
+ end
54
+
55
+ def content_type
56
+ /^application\/json$/
57
+ end
58
+ end
59
+
60
+ class Text
61
+
62
+ def encode(body)
63
+ body.to_s
64
+ end
65
+
66
+ def decode(body)
67
+ body.to_s
27
68
  end
28
69
 
29
- def supported_decodings
30
- ['application/json']
70
+ def content_type
71
+ /^text\/.*/
31
72
  end
32
73
  end
33
74
  end
@@ -3,7 +3,7 @@ require 'ostruct'
3
3
  describe Encoder do
4
4
 
5
5
  describe 'serialize_request' do
6
- it 'serializes the request' do
6
+ it 'serializes the request when content-type == application/json' do
7
7
  req = OpenStruct.new({
8
8
  :headers => {
9
9
  "content-type" => "application/json"
@@ -25,6 +25,17 @@ describe Encoder do
25
25
  expect(Encoder.new.serialize_request(req)).to eq(expected)
26
26
  end
27
27
 
28
+ it 'serializes the request when content-type == text/*' do
29
+ req = OpenStruct.new({
30
+ :headers => {
31
+ "content-type" => "text/plain"
32
+ },
33
+ :body => "some text"
34
+ })
35
+
36
+ expect(Encoder.new.serialize_request(req)).to eq("some text")
37
+ end
38
+
28
39
  it 'throws when content-type is not application/json' do
29
40
  req = OpenStruct.new({
30
41
  :headers => {
@@ -33,7 +44,7 @@ describe Encoder do
33
44
  :body => { :string => "value" }
34
45
  })
35
46
 
36
- expect{Encoder.new.serialize_request(req)}.to raise_error(UnsupportedEncodingError, /Unable to serialize request with Content-Type .*\. Supported encodings are \["application\/json"\]/)
47
+ expect{Encoder.new.serialize_request(req)}.to raise_error(UnsupportedEncodingError, /Unable to serialize request with Content-Type .*\. Supported encodings are .*/)
37
48
  end
38
49
 
39
50
  it 'throws when headers undefined' do
@@ -52,7 +63,7 @@ describe Encoder do
52
63
  "content-type" => ["application/xml"]
53
64
  }
54
65
 
55
- expect{Encoder.new.deserialize_response(body, headers)}.to raise_error(UnsupportedEncodingError, /Unable to deserialize response with Content-Type .*\. Supported decodings are \["application\/json"\]/)
66
+ expect{Encoder.new.deserialize_response(body, headers)}.to raise_error(UnsupportedEncodingError, /Unable to deserialize response with Content-Type .*\. Supported decodings are .*/)
56
67
  end
57
68
 
58
69
  it 'throws when headers undefined' do
@@ -61,7 +72,7 @@ describe Encoder do
61
72
  expect{Encoder.new.deserialize_response(body, nil)}.to raise_error(UnsupportedEncodingError, 'HttpResponse did not have Content-Type header set')
62
73
  end
63
74
 
64
- it 'deserializes the response' do
75
+ it 'deserializes the response when content-type == application/json' do
65
76
  expected = {
66
77
  "string" => "value",
67
78
  "number" => 1.23,
@@ -78,5 +89,12 @@ describe Encoder do
78
89
 
79
90
  expect(Encoder.new.deserialize_response(body, headers)).to eq(expected)
80
91
  end
92
+
93
+ it 'deserializes the response when content-type == text/*' do
94
+ headers = {"content-type" => ["text/plain"]}
95
+ body = 'some text'
96
+
97
+ expect(Encoder.new.deserialize_response(body, headers)).to eq('some text')
98
+ end
81
99
  end
82
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: braintreehttp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braintree
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-15 00:00:00.000000000 Z
11
+ date: 2017-08-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Used for generated API clients
14
14
  email: code@getbraintree.com
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  requirements: []
47
47
  rubyforge_project: braintreehttp
48
- rubygems_version: 2.2.2
48
+ rubygems_version: 2.6.12
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: BraintreeHttp Client Library