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 +4 -4
- data/lib/verm/client.rb +29 -1
- data/lib/verm/version.rb +1 -1
- data/test/verm_test.rb +14 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2424da2dedcc8777ce4750ca0222bf5784cc7b32
|
4
|
+
data.tar.gz: 7cf07cf76cdc95d372a90e6fd7d13f3acb401ff3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe7cd6811636171079bb872fa1fa392dbff10916271941a195af59199762e022c65dff4c1f42db6abc511ce4351a8e98a7669a440bc72e27831d4aaad74d5232
|
7
|
+
data.tar.gz: f0a02480c2bfcb26c92dd1d8fef7f90236dbbf6bab0fefae96126609fcf2066fc3babc02d4fb8d1b29ba83b045f412e269173aa317602652f90409ecb324e352
|
data/lib/verm/client.rb
CHANGED
@@ -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
|
data/lib/verm/version.rb
CHANGED
data/test/verm_test.rb
CHANGED
@@ -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.
|
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-
|
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.
|