tempfile_for 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +18 -9
- data/lib/tempfile_for/version.rb +1 -1
- data/lib/tempfile_for.rb +12 -2
- data/test/tempfile_for_test.rb +7 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -7,11 +7,21 @@ Easily create temporary files for in-memory data, modify the file, and get the f
|
|
7
7
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
|
-
|
10
|
+
```
|
11
|
+
gem "tempfile_for"
|
12
|
+
```
|
11
13
|
|
12
14
|
And then execute:
|
13
15
|
|
14
|
-
|
16
|
+
```
|
17
|
+
$ bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
Alternatively, you can of course install it without using bundler via:
|
21
|
+
|
22
|
+
```
|
23
|
+
$ gem install tempfile_for
|
24
|
+
```
|
15
25
|
|
16
26
|
## Usage
|
17
27
|
|
@@ -20,10 +30,10 @@ However, it can save you lines of ugly code.
|
|
20
30
|
|
21
31
|
To get a quick introduction into what TempfileFor does, check this out:
|
22
32
|
|
23
|
-
|
33
|
+
```ruby
|
24
34
|
Tempfile.for("string1") { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
|
25
|
-
=> "string1, string2"
|
26
|
-
|
35
|
+
# => "string1, string2"
|
36
|
+
```
|
27
37
|
|
28
38
|
Say, you have some in-memory data, like an image you fetch from an URL - and you want to somehow modify it partially
|
29
39
|
like e.g., add or remove IPTC tags, or scale it, etc. Often, the gems used to modify images or other media files
|
@@ -31,15 +41,14 @@ require a path to the file to be able to modify it.
|
|
31
41
|
|
32
42
|
This is easy thanks to TempfileFor:
|
33
43
|
|
34
|
-
|
35
|
-
|
44
|
+
```ruby
|
36
45
|
data = RestClient.get("http://example.com/image.jpg")
|
37
46
|
|
38
47
|
image = Tempfile.for(data) do |tempfile|
|
39
|
-
# Modify the tempfile and get the modified content returned.
|
48
|
+
# Modify the tempfile directly or using tempfile.path and get the modified content returned.
|
40
49
|
# Tempfile takes care about flushing the file's modifications and rewinding, etc.
|
41
50
|
end
|
42
|
-
|
51
|
+
```
|
43
52
|
|
44
53
|
## Contributing
|
45
54
|
|
data/lib/tempfile_for/version.rb
CHANGED
data/lib/tempfile_for.rb
CHANGED
@@ -4,7 +4,13 @@ require "tempfile"
|
|
4
4
|
|
5
5
|
class Tempfile
|
6
6
|
def self.for(data)
|
7
|
-
tempfile =
|
7
|
+
tempfile = nil
|
8
|
+
|
9
|
+
if RUBY_VERSION < "1.9"
|
10
|
+
tempfile = open("tempfile")
|
11
|
+
else
|
12
|
+
tempfile = open("tempfile", :encoding => data.encoding)
|
13
|
+
end
|
8
14
|
|
9
15
|
begin
|
10
16
|
tempfile.write data
|
@@ -14,7 +20,11 @@ class Tempfile
|
|
14
20
|
|
15
21
|
tempfile.flush
|
16
22
|
|
17
|
-
|
23
|
+
if RUBY_VERSION < "1.9"
|
24
|
+
File.read tempfile.path
|
25
|
+
else
|
26
|
+
File.read tempfile.path, :encoding => data.encoding
|
27
|
+
end
|
18
28
|
ensure
|
19
29
|
tempfile.close!
|
20
30
|
end
|
data/test/tempfile_for_test.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
|
2
3
|
require File.expand_path("../../lib/tempfile_for", __FILE__)
|
3
4
|
require "test/unit"
|
@@ -5,6 +6,12 @@ require "test/unit"
|
|
5
6
|
class TempfileForTest < Test::Unit::TestCase
|
6
7
|
def test_tempfile_for
|
7
8
|
assert_equal "string1, string2", Tempfile.for("string1") { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
|
9
|
+
|
10
|
+
if RUBY_VERSION >= "1.9"
|
11
|
+
string = "string1".force_encoding("ISO-8859-1")
|
12
|
+
res = Tempfile.for(string) { |tempfile| `echo -n ', string2' >> #{tempfile.path}` }
|
13
|
+
assert_equal "ISO-8859-1", res.encoding.name
|
14
|
+
end
|
8
15
|
end
|
9
16
|
end
|
10
17
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tempfile_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-04-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|