verm-client 1.0.0 → 1.1.0

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: 3a616e96483beae73fd10062005d641d30fb5352
4
- data.tar.gz: 6b9fffb843d7d12a75c7cdb3658b193a740e5f32
3
+ metadata.gz: 2424da2dedcc8777ce4750ca0222bf5784cc7b32
4
+ data.tar.gz: 7cf07cf76cdc95d372a90e6fd7d13f3acb401ff3
5
5
  SHA512:
6
- metadata.gz: 0aa23231f3b808815b44a89bf9cd81adf3e919affded5249c58ed6486aa41c4891f6d02427672c87b90e760047aed62b5033ec2dd8f1c9efe922c27d15997793
7
- data.tar.gz: 9a333cbd52d2fc22b4e9389f0bb49f44592ffc26388bff151cb0795c9b54da62c89fd760f4556dbacb83ab57b0a3fedfdf974f1082bf75ffffe9bbd4ecf2ea4a
6
+ metadata.gz: fe7cd6811636171079bb872fa1fa392dbff10916271941a195af59199762e022c65dff4c1f42db6abc511ce4351a8e98a7669a440bc72e27831d4aaad74d5232
7
+ data.tar.gz: f0a02480c2bfcb26c92dd1d8fef7f90236dbbf6bab0fefae96126609fcf2066fc3babc02d4fb8d1b29ba83b045f412e269173aa317602652f90409ecb324e352
@@ -2,6 +2,10 @@ require 'net/http'
2
2
 
3
3
  module Verm
4
4
  class Client
5
+ def self.incompressible_types
6
+ @incompressible_types ||= %w(image/jpeg image/png image/gif)
7
+ end
8
+
5
9
  attr_reader :http_client
6
10
 
7
11
  def initialize(hostname, port: 3404, timeout: 15)
@@ -11,7 +15,15 @@ module Verm
11
15
  @http_client.ssl_timeout = timeout
12
16
  end
13
17
 
14
- def store(directory, io_or_data, content_type, encoding: nil)
18
+ def store(directory, io_or_data, content_type, encoding: nil, autocompress: true)
19
+ if %w(application/gzip application/x-gzip).include?(content_type) && encoding.nil?
20
+ raise ArgumentError, "Pass the real content-type and encoding: 'gzip' for gzipped uploads" # see 'File compression' in README.md
21
+ end
22
+
23
+ if autocompress && encoding.nil? && !self.class.incompressible_types.include?(content_type)
24
+ io_or_data, encoding = compress(io_or_data)
25
+ end
26
+
15
27
  directory = "/#{directory}" unless directory[0] == '/'
16
28
  request = Net::HTTP::Post.new(directory, 'Content-Type' => content_type)
17
29
  request['Content-Encoding'] = encoding.to_s if encoding
@@ -55,5 +67,21 @@ module Verm
55
67
  end
56
68
  end
57
69
  end
70
+
71
+ protected
72
+ def compress(io_or_data)
73
+ if io_or_data.respond_to?(:read) # we don't know how to implement compression for streaming because net/http wants readers not writers :(
74
+ return io_or_data
75
+ end
76
+
77
+ compressed = StringIO.new
78
+ compressed.set_encoding("BINARY")
79
+ gz = Zlib::GzipWriter.new(compressed)
80
+ gz.write(io_or_data)
81
+ gz.close
82
+ output = compressed.string
83
+ return output, "gzip" if output.bytesize < io_or_data.bytesize
84
+ io_or_data
85
+ end
58
86
  end
59
87
  end
@@ -1,3 +1,3 @@
1
1
  module Verm
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -52,6 +52,20 @@ class TestVerm < Minitest::Test
52
52
  end
53
53
  end
54
54
 
55
+ def test_stores_compressed_files_if_given_compressible_but_uncompressed_content
56
+ content, type = "repetitive auto-compressible data"*10, "text/plain"
57
+ location = @client.store("/autocompress/", content, type)
58
+ assert_equal [content, type], @client.load(location) # will get auto-decompressed by ruby; proves just that the content was stored
59
+ refute_equal [content, type], @client.load(location, 'Accept-Encoding' => 'gzip') # setting an explicit Accept-Encoding turns off auto-decompress; the result would not be useful for apps since the encoding info is lost, but it proves that the content was not stored uncompressed and therefore was stored compressed
60
+ end
61
+
62
+ def test_stores_uncompressed_files_if_given_incompressible_uncompressed_content
63
+ content, type = "non-repetitive data", "text/plain"
64
+ location = @client.store("/autocompress/", content, type)
65
+ assert_equal [content, type], @client.load(location) # will get auto-decompressed by ruby; proves just that the content was stored
66
+ assert_equal [content, type], @client.load(location, 'Accept-Encoding' => 'gzip') # reverse of above; implies the content was stored uncompressed
67
+ end
68
+
55
69
  def test_loads_content_and_type
56
70
  content, type = "this is a test", "text/plain"
57
71
  assert_equal [content, type],
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verm-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Bryant
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-11 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Adds one-line methods for storing files in Verm and retrieving them again.