braintreehttp 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 573b80fe852147baed1c042d799fce690204e54c
4
- data.tar.gz: bfe5f2c242d571a4d3b980e06d01187982218bff
3
+ metadata.gz: deacd36541edfd3d5dc656f1ce53da0b4aaac4ef
4
+ data.tar.gz: ce4c58630cd4da813c5a2915b241bf55cfcaf5b4
5
5
  SHA512:
6
- metadata.gz: b9fa99e9a24984d30667ee1e4e7455b119bfdab4a1e2e56b12d3c3e375caf709cdd728f4c8c7c5e554a42313913f2c2a2ef75a7d721cba58d7d4bdfea84a6dc3
7
- data.tar.gz: 192deb14605d03670a59f5cf2457fbc4b3b50478061068803f4dfa4bf461893f74ffe3af26bc1a27a3fd0f077a4034b0f80dcb145ff3e8803445cae49f5239a2
6
+ metadata.gz: 8c76d6da6018a67c292a27afc6d68de3919a7a6c739655da8c2f5c675a6ce6addb8d5c8e2cd130dddb79312b16f92caf60b037d9931e505148b6c802afef184e
7
+ data.tar.gz: 31dcd34ad1bad733e186d52a637034cc9914833f0a819d8a13b30fb3663fdfe3dfc83e8c645e0a44f360a2c56d021d9211755cbea4e1f82d5a334f9d2036eb57
@@ -3,4 +3,5 @@ module BraintreeHttp
3
3
  require_relative "braintreehttp/http_client"
4
4
  require_relative "braintreehttp/errors"
5
5
  require_relative "braintreehttp/encoder"
6
+ require_relative "braintreehttp/serializers/form_part"
6
7
  end
@@ -0,0 +1,14 @@
1
+ module BraintreeHttp
2
+ class FormPart
3
+ attr_accessor :value, :headers
4
+
5
+ def initialize(value, headers)
6
+ @value = value
7
+ @headers = {}
8
+
9
+ headers.each do |key, value|
10
+ @headers[key.to_s.downcase.split('-').map { |word| "#{word[0].upcase}#{word[1..-1]}" }.join("-")] = value
11
+ end
12
+ end
13
+ end
14
+ end
@@ -8,14 +8,20 @@ module BraintreeHttp
8
8
  request.headers["Content-Type"] = "#{request.headers['Content-Type']}; boundary=#{boundary}"
9
9
 
10
10
  form_params = []
11
+ value_params = []
12
+ file_params = []
13
+
11
14
  request.body.each do |k, v|
12
15
  if v.is_a? File
13
- form_params.push(_add_file_part(k, v))
16
+ file_params.push(_add_file_part(k, v))
17
+ elsif v.is_a? FormPart
18
+ value_params.push(_add_form_part(k, v))
14
19
  else
15
- form_params.push(_add_form_field(k, v))
20
+ value_params.push(_add_form_field(k, v))
16
21
  end
17
22
  end
18
23
 
24
+ form_params = value_params + file_params
19
25
  form_params.collect {|p| "--" + boundary + "#{LINE_FEED}" + p}.join("") + "--" + boundary + "--"
20
26
  end
21
27
 
@@ -31,6 +37,35 @@ module BraintreeHttp
31
37
  return "Content-Disposition: form-data; name=\"#{key}\"#{LINE_FEED}#{LINE_FEED}#{value}#{LINE_FEED}"
32
38
  end
33
39
 
40
+ def _add_form_part(key, form_part)
41
+ retValue = "Content-Disposition: form-data; name=\"#{key}\""
42
+ if form_part.headers["Content-Type"] == "application/json"
43
+ retValue += "; filename=\"#{key}.json\""
44
+ end
45
+ retValue += "#{LINE_FEED}"
46
+
47
+ form_part.headers.each do |key, value|
48
+ retValue += "#{key}: #{value}#{LINE_FEED}"
49
+ end
50
+
51
+ retValue += "#{LINE_FEED}"
52
+
53
+ if form_part.headers["Content-Type"]
54
+ retValue += Encoder.new().serialize_request(OpenStruct.new({
55
+ :verb => 'POST',
56
+ :path => '/',
57
+ :headers => form_part.headers,
58
+ :body => form_part.value
59
+ }))
60
+ else
61
+ retValue += form_part.value
62
+ end
63
+
64
+ retValue += "#{LINE_FEED}"
65
+
66
+ return retValue
67
+ end
68
+
34
69
  def _add_file_part(key, file)
35
70
  mime_type = _mime_type_for_file_name(file.path)
36
71
  return "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(file.path)}\"#{LINE_FEED}" +
@@ -41,6 +76,8 @@ module BraintreeHttp
41
76
  file_extension = File.extname(filename).strip.downcase[1..-1]
42
77
  if file_extension == "jpeg" || file_extension == "jpg"
43
78
  return "image/jpeg"
79
+ elsif file_extension == "gif"
80
+ return "image/gif"
44
81
  elsif file_extension == "png"
45
82
  return "image/png"
46
83
  elsif file_extension == "pdf"
@@ -1 +1 @@
1
- VERSION = "0.4.4"
1
+ VERSION = "0.5.0"
@@ -58,7 +58,6 @@ describe Encoder do
58
58
 
59
59
  expect(req.headers['Content-Type']).to include('multipart/form-data; charset=utf8; boundary=')
60
60
 
61
- expect(serialized).to include("Content-Disposition: form-data; name=\"readme\"; filename=\"README.md\"")
62
61
  expect(serialized).to include("Content-Disposition: form-data; name=\"readme\"; filename=\"README.md\"")
63
62
  expect(serialized).to include("Content-Disposition: form-data; name=\"key\"")
64
63
  expect(serialized).to include("value")
@@ -66,6 +65,32 @@ describe Encoder do
66
65
  expect(serialized).to include("1013")
67
66
  end
68
67
 
68
+ it 'serializes the request when Json is provided inside multipart/form-data' do
69
+ file = File.new("README.md", "r")
70
+ req = OpenStruct.new({
71
+ :verb => "POST",
72
+ :path => "/v1/api",
73
+ :headers => {
74
+ "Content-Type" => "multipart/form-data; charset=utf8"
75
+ },
76
+ :body => {
77
+ :readme => file,
78
+ :input=> FormPart.new({:key => 'val'}, {'content-type': 'application/json'}),
79
+ }
80
+ })
81
+
82
+ serialized = Encoder.new.serialize_request(req)
83
+
84
+ expect(req.headers['Content-Type']).to include('multipart/form-data; charset=utf8; boundary=')
85
+
86
+ expect(serialized).to include("Content-Disposition: form-data; name=\"readme\"; filename=\"README.md\"")
87
+ expect(serialized).to include("Content-Type: application/octet-stream")
88
+ expect(serialized).to include("Content-Disposition: form-data; name=\"input\"; filename=\"input.json\"")
89
+ expect(serialized).to include("Content-Type: application/json")
90
+ expect(serialized).to include("{\"key\":\"val\"}")
91
+ expect(serialized).to match(/.*Content-Disposition: form-data; name=\"input\"; filename=\"input.json\".*Content-Disposition: form-data; name=\"readme\"; filename=\"README.md\".*/m)
92
+ end
93
+
69
94
  it 'serializes the request when content-type == application/x-www-form-urlencoded' do
70
95
  req = OpenStruct.new({
71
96
  :verb => "POST",
@@ -0,0 +1,33 @@
1
+ describe FormPart do
2
+ describe 'initialize' do
3
+ it 'Header-Cases lower-case headers' do
4
+ lowerCaseFormPart = FormPart.new({:key => 'val'}, {'content-type': 'application/json'});
5
+
6
+ expect(lowerCaseFormPart.headers.keys).to include('Content-Type')
7
+ expect(lowerCaseFormPart.headers.keys.length).to eq(1)
8
+ end
9
+
10
+ it 'Header-Cases single char headers' do
11
+ singleCharFormPart = FormPart.new({:key => 'val'}, {'x': 'application/json'});
12
+
13
+ expect(singleCharFormPart.headers.keys).to include('X')
14
+ expect(singleCharFormPart.headers.keys.length).to eq(1)
15
+ end
16
+
17
+ it 'Header-Cases keeps single header if collision' do
18
+ multiHeaderFormPart = FormPart.new({:key => 'val'}, {'content-type': 'application/json', 'CONTENT-TYPE': 'application/pdf'});
19
+
20
+ expect(multiHeaderFormPart.headers.keys).to include('Content-Type')
21
+ expect(multiHeaderFormPart.headers.keys.length).to eq(1)
22
+ end
23
+
24
+ it 'Header-Cases multiple headers when supplied' do
25
+ multiHeaderFormPart = FormPart.new({:key => 'val'}, {'header-one': 'application/json', 'header-Two': 'application/pdf', 'HEADER-THREE': 'img/jpg'});
26
+
27
+ expect(multiHeaderFormPart.headers.keys).to include('Header-One')
28
+ expect(multiHeaderFormPart.headers.keys).to include('Header-Two')
29
+ expect(multiHeaderFormPart.headers.keys).to include('Header-Three')
30
+ expect(multiHeaderFormPart.headers.keys.length).to eq(3)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ describe Multipart do
2
+ describe 'mime_type_for_filename' do
3
+ it 'supports gif' do
4
+ expect(Multipart.new()._mime_type_for_file_name("test.gif")).to eq('image/gif')
5
+ end
6
+
7
+ it 'supports jpeg' do
8
+ expect(Multipart.new()._mime_type_for_file_name("test.jpeg")).to eq('image/jpeg')
9
+ end
10
+
11
+ it 'supports jpg' do
12
+ expect(Multipart.new()._mime_type_for_file_name("test.jpg")).to eq('image/jpeg')
13
+ end
14
+
15
+ it 'supports pdf' do
16
+ expect(Multipart.new()._mime_type_for_file_name("test.pdf")).to eq('application/pdf')
17
+ end
18
+
19
+ it 'supports appication/octet-stream' do
20
+ expect(Multipart.new()._mime_type_for_file_name("test.random")).to eq('application/octet-stream')
21
+ end
22
+ end
23
+ 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.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braintree
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-02 00:00:00.000000000 Z
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Used for generated API clients
14
14
  email: code@getbraintree.com
@@ -16,20 +16,23 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - braintreehttp.gemspec
20
+ - lib/braintreehttp.rb
19
21
  - lib/braintreehttp/encoder.rb
20
22
  - lib/braintreehttp/environment.rb
21
23
  - lib/braintreehttp/errors.rb
22
24
  - lib/braintreehttp/http_client.rb
23
25
  - lib/braintreehttp/serializers/form_encoded.rb
26
+ - lib/braintreehttp/serializers/form_part.rb
24
27
  - lib/braintreehttp/serializers/json.rb
25
28
  - lib/braintreehttp/serializers/multipart.rb
26
29
  - lib/braintreehttp/serializers/text.rb
27
30
  - lib/braintreehttp/version.rb
28
- - lib/braintreehttp.rb
29
31
  - spec/braintreehttp/encoder_spec.rb
30
32
  - spec/braintreehttp/http_client_spec.rb
33
+ - spec/braintreehttp/serializers/form_part_spec.rb
34
+ - spec/braintreehttp/serializers/multipart_spec.rb
31
35
  - spec/spec_helper.rb
32
- - braintreehttp.gemspec
33
36
  homepage: http://www.braintreepayments.com/
34
37
  licenses:
35
38
  - MIT
@@ -40,17 +43,17 @@ require_paths:
40
43
  - lib
41
44
  required_ruby_version: !ruby/object:Gem::Requirement
42
45
  requirements:
43
- - - '>='
46
+ - - ">="
44
47
  - !ruby/object:Gem::Version
45
48
  version: '0'
46
49
  required_rubygems_version: !ruby/object:Gem::Requirement
47
50
  requirements:
48
- - - '>='
51
+ - - ">="
49
52
  - !ruby/object:Gem::Version
50
53
  version: '0'
51
54
  requirements: []
52
55
  rubyforge_project: braintreehttp
53
- rubygems_version: 2.0.14.1
56
+ rubygems_version: 2.6.11
54
57
  signing_key:
55
58
  specification_version: 4
56
59
  summary: BraintreeHttp Client Library