flexirest 1.8.3 → 1.8.4

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
  SHA256:
3
- metadata.gz: f91ffbe647c15fa55c5d9ec36a5ae23b9008cedda2779d208a4f0214793197c5
4
- data.tar.gz: 755abe793f393deb621ad88ada2f8a2758f61e6ca9ea4da34ec8b683b53d7163
3
+ metadata.gz: ec81f48590a19e143fb865b92c76c4784f1a559380543419d137eb62efc7e1d4
4
+ data.tar.gz: 7c76f888375d9a989da6b2facdf73176e136cd3b87ae4f54d2b75c182df094c0
5
5
  SHA512:
6
- metadata.gz: bdc44703759885c555b55ee80aa5bc5ca38cc4fba25cf9b449b4a0e438dd713dc7c716fdae7795bf76754a65c1a07bfc70cf25b9f973f9158a4ada2d6aaa61b2
7
- data.tar.gz: adcdc81fc9ad6252d3948a41e8500f802ff48713325ab96c056a106a15a39fc5dd3d2b92159df1f96fed4cafa5d890a340b41d0ab83a5b16524561d5dc676d08
6
+ metadata.gz: dc6a8378e3486a4f829e4d240395de3312325ba2bac0ab77dd9e730171c208e1109d6106d9109e0aa95586727c0508a22a8d6bbcc5bd8b4ebb5835fea0e80703
7
+ data.tar.gz: 573e84668b742b1aad8f0678aab3f4cc58e52f2a1b5df5ec2196346df1843e2e3324927d905b3aa7884c9fd9908284785f158a93bedef94e97187cf5a5c1d325
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.8.4
4
+
5
+ Fix:
6
+
7
+ - Array and Hash parameters should be accepted in `:form_multipart`
8
+
3
9
  ## 1.8.3
4
10
 
5
11
  Fix:
@@ -9,15 +9,10 @@ require 'rubygems'
9
9
  require 'mime/types'
10
10
  require 'cgi'
11
11
 
12
-
13
12
  module Flexirest
14
13
  module Multipart
15
14
  VERSION = "1.0.0"
16
15
 
17
- # Formats a given hash as a multipart form post
18
- # If a hash value responds to :string or :read messages, then it is
19
- # interpreted as a file and processed accordingly; otherwise, it is assumed
20
- # to be a string
21
16
  class Post
22
17
  BOUNDARY = "FLEXIRESTBOUNDARY-20190918-FLEXIRESTBOUNDARY"
23
18
  CONTENT_TYPE = "multipart/form-data; boundary=#{BOUNDARY}"
@@ -27,24 +22,33 @@ module Flexirest
27
22
  fp = []
28
23
 
29
24
  params.stringify_keys.each do |k, v|
30
- # Are we trying to make a file parameter?
31
- if v.respond_to?(:path) and v.respond_to?(:read) then
32
- fp.push(FileParam.new(k, v.path, v.read))
33
- # We must be trying to make a regular parameter
34
- else
35
- fp.push(StringParam.new(k, v))
36
- end
25
+ append_parameter(fp, k, v)
37
26
  end
38
27
 
39
- # Assemble the request body using the special multipart format
40
28
  query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
41
29
  return query, HEADER
42
30
  end
31
+
32
+ def self.append_parameter(fp, key, value)
33
+ if value.is_a?(Array)
34
+ value.each do |i|
35
+ append_parameter(fp, "#{key}[]", i)
36
+ end
37
+ elsif value.is_a?(Hash)
38
+ value.stringify_keys.each do |k, i|
39
+ append_parameter(fp, "#{key}[#{k}]", i)
40
+ end
41
+ elsif value.respond_to?(:path) and value.respond_to?(:read) then
42
+ fp.push(FileParam.new(key, value.path, value.read))
43
+ else
44
+ fp.push(StringParam.new(key, value))
45
+ end
46
+
47
+ end
43
48
  end
44
49
 
45
50
  private
46
51
 
47
- # Formats a basic string key/value pair for inclusion with a multipart post
48
52
  class StringParam
49
53
  attr_accessor :k, :v
50
54
 
@@ -58,8 +62,6 @@ module Flexirest
58
62
  end
59
63
  end
60
64
 
61
- # Formats the contents of a file or string for inclusion with a multipart
62
- # form post
63
65
  class FileParam
64
66
  attr_accessor :k, :filename, :content
65
67
 
@@ -70,8 +72,6 @@ module Flexirest
70
72
  end
71
73
 
72
74
  def to_multipart
73
- # If we can tell the possible mime-type from the filename, use the
74
- # first in the list; otherwise, use "application/octet-stream"
75
75
  mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
76
76
  return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" +
77
77
  "Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.8.3"
2
+ VERSION = "1.8.4"
3
3
  end
@@ -358,6 +358,18 @@ describe Flexirest::Request do
358
358
  "Content-Disposition: form-data; name=\"debug\"\r\n" +
359
359
  "\r\n" +
360
360
  "true\r\n" +
361
+ "--FLEXIRESTBOUNDARY-20190918-FLEXIRESTBOUNDARY\r\n"+
362
+ "Content-Disposition: form-data; name=\"arrayz%5B%5D\"\r\n"+
363
+ "\r\n"+
364
+ "1\r\n"+
365
+ "--FLEXIRESTBOUNDARY-20190918-FLEXIRESTBOUNDARY\r\n"+
366
+ "Content-Disposition: form-data; name=\"arrayz%5B%5D\"\r\n"+
367
+ "\r\n"+
368
+ "2\r\n"+
369
+ "--FLEXIRESTBOUNDARY-20190918-FLEXIRESTBOUNDARY\r\n"+
370
+ "Content-Disposition: form-data; name=\"hazh%5Bsomething%5D\"\r\n"+
371
+ "\r\n"+
372
+ "bar\r\n"+
361
373
  "--FLEXIRESTBOUNDARY-20190918-FLEXIRESTBOUNDARY\r\n" +
362
374
  "Content-Disposition: form-data; name=\"test\"\r\n" +
363
375
  "\r\n" +
@@ -372,7 +384,7 @@ describe Flexirest::Request do
372
384
  "/put/1234", body, hash_including(headers: hash_including("Content-Type"=>"multipart/form-data; boundary=FLEXIRESTBOUNDARY-20190918-FLEXIRESTBOUNDARY"))
373
385
  ).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", response_headers:{})))
374
386
  ExampleClient.request_body_type :form_multipart
375
- ExampleClient.update id:1234, debug:true, test:'foo', file: File.open("#{File.dirname(__FILE__)}/../../spec/samples/file.txt")
387
+ ExampleClient.update id:1234, debug:true, arrayz: [1, 2], hazh: {something: "bar"}, test:'foo', file: File.open("#{File.dirname(__FILE__)}/../../spec/samples/file.txt")
376
388
  end
377
389
 
378
390
  it "should encode the body in a JSON format if specified" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexirest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.3
4
+ version: 1.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-18 00:00:00.000000000 Z
11
+ date: 2019-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler