tmp_file 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 700139ffe9e157afec5bc9154441e54b22afeb5d
4
+ data.tar.gz: 234380a9e5a67efc74237ff9651681c6e1bb97da
5
+ SHA512:
6
+ metadata.gz: c1256ea2963d949f64a5735e0dec52296940adc5f3c8416746cd1bc1940a84e3b38161ef7742b97d3857e6cdb2e711e17df9b741feda2a41cb26a062ac22dc17
7
+ data.tar.gz: 27f8c09dd1acf7a249d697cce9a80621d54f306ae55bd0758899718f000bde06f119a1c290fcc59b025f9b49d43b0927bc66cf8635524fbaf4e61766ad63b63a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Topspin Media Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ What is TmpFile
2
+ ================
3
+
4
+ A ruby gem to temporarily store data in a file with an appropriate extension
5
+
6
+ Why TmpFile was born
7
+ =====================
8
+
9
+ At [Topspin](http://topspinmedia.com) we provide [ArtistLink](http://artistlink.com), a platform for musician to upload and share their songs.
10
+ When a song contains cover art in the ID3 tag, we extract it to an image and process it to create a nice page background.
11
+ We use TmpFile to temporarily extract the cover art data from the song into a file store its processed version.
12
+
13
+ Requirements
14
+ ============
15
+
16
+ TmpFile has been tested on Ruby 1.8.7, 1.9.2, 1.9.3 and 2.0.0.
17
+ No external libraries are required.
18
+
19
+ How to use from other programs
20
+ ==============================
21
+
22
+ * Include `tmp_file` in the Gemfile of your bundled project and `bundle install`
23
+ * To store data in a temporary file and yield the file handle, run:
24
+
25
+ TmpFile.open(data, mime_type) do |tmp_file|
26
+ # do something with tmp_file
27
+ end
28
+
29
+
30
+ For more details, check the [specs](http://github.com/topspin/tmp_file/tree/master/spec/lib).
31
+
32
+
33
+ How to contribute
34
+ =================
35
+
36
+ Make sure tests pass with `rake spec`, then submit a Pull Request.
37
+
38
+ A list of [nice TODOs](http://github.com/topspin/tmp_file/tree/master/TODO.md) is provided.
data/lib/tmp_file.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'tmpdir'
2
+ require 'securerandom'
3
+
4
+ # Provides a method to store data in a temporary file and access that file
5
+ module TmpFile
6
+
7
+ # Stores +data+ in a temporary file and yields the file.
8
+ # The file is destroyed after the yielded block ends.
9
+ # The extension of the temporary file matches the passed +mime_type+.
10
+ #
11
+ # @param [String] data Data to store in the temporary file
12
+ # @param [String] mime_type MIME type of the temporary file
13
+ # @yield [File] handle of an open read-only file with the given +data+
14
+ #
15
+ # @example Yield a temporary PNG file with the .png extension
16
+ # ==== Examples
17
+ #
18
+ # TmpFile.open("\x89PNG\r\n\x1A[...]", 'image/png') do |tmp_file|
19
+ # # do something with tmp_file
20
+ # end
21
+ def self.open(data, mime_type)
22
+ path = self.unique_path_for_temporary_file mime_type
23
+ File.open(path, 'wb') {|file| file.write data}
24
+ File.open(path, 'rb') {|file| yield file}
25
+ ensure
26
+ FileUtils.rm_f path
27
+ end
28
+
29
+ private
30
+
31
+ # Returns a unique temporary path to store a file of the given +mime_type+.
32
+ #
33
+ # @param [String] mime_type MIME type of the temporary file
34
+ # @return [String] local path that can be opened for writing
35
+ #
36
+ # @example Find a unique path to store a temporary PNG file
37
+ # TmpFile.unique_path_for_temporary_file('image/png')
38
+ #
39
+ # # => '/var/folders/zs/xtcy1d/T/517bf6608c1314441285d4797ebcb3c2.png'
40
+ def self.unique_path_for_temporary_file(mime_type)
41
+ extension = case mime_type
42
+ when 'image/jpg' then '.jpg'
43
+ when 'image/jpeg' then '.jpg'
44
+ when 'image/png' then '.png'
45
+ when 'image/gif' then '.gif'
46
+ else '.unknown'
47
+ end
48
+ File.join Dir.tmpdir, (SecureRandom.hex + extension)
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module TmpFile
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tmp_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Claudio Baccigalupo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Yields a temporary file with given data and MIME type
84
+ email:
85
+ - claudio@topspinmedia.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/tmp_file/version.rb
91
+ - lib/tmp_file.rb
92
+ - MIT-LICENSE
93
+ - README.md
94
+ homepage: https://github.com/topspin/tmp_file
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: 1.8.7
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: ! 'TmpFile provides one method: * open, which creates a temporary file with
118
+ the provided data and the appropriate extension for the MIME type, yields the file
119
+ handler to a block, and deletes the file once the block is completed'
120
+ test_files: []
121
+ has_rdoc: