Class: Tootsie::Output
- Inherits:
-
Object
- Object
- Tootsie::Output
- Defined in:
- lib/tootsie/output.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#result_url ⇒ Object
readonly
Returns the value of attribute result_url.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url) ⇒ Output
constructor
A new instance of Output.
-
#put!(options = {}) ⇒ Object
Put data into the output.
Constructor Details
#initialize(url) ⇒ Output
Returns a new instance of Output.
10 11 12 13 14 15 16 |
# File 'lib/tootsie/output.rb', line 10 def initialize(url) @url = url @temp_file = Tempfile.new('tootsie') @temp_file.close @file_name = @temp_file.path @logger = Application.get.logger end |
Instance Attribute Details
#content_type ⇒ Object
Returns the value of attribute content_type.
63 64 65 |
# File 'lib/tootsie/output.rb', line 63 def content_type @content_type end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
62 63 64 |
# File 'lib/tootsie/output.rb', line 62 def file_name @file_name end |
#result_url ⇒ Object (readonly)
Returns the value of attribute result_url.
61 62 63 |
# File 'lib/tootsie/output.rb', line 61 def result_url @result_url end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
60 61 62 |
# File 'lib/tootsie/output.rb', line 60 def url @url end |
Instance Method Details
#close ⇒ Object
56 57 58 |
# File 'lib/tootsie/output.rb', line 56 def close @temp_file.unlink end |
#put!(options = {}) ⇒ Object
Put data into the output. Options:
-
:content_type
- content type of the stored data.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tootsie/output.rb', line 22 def put!( = {}) @logger.info("Storing #{@url}") case @url when /^file:(.*)/ FileUtils.cp(@temp_file.path, $1) @result_url = @url when /^s3:.*/ = S3Utilities.parse_uri(@url) bucket_name, path = [:bucket], [:key] File.open(@temp_file.path, 'r') do |file| s3_service = Tootsie::Application.get.s3_service begin object = s3_service.buckets.find(bucket_name).objects.build(path) object.acl = [:acl] || :private object.content_type = [:content_type] object.content_type ||= @content_type if @content_type object.storage_class = [:storage_class] || :standard object.content = file object.save @result_url = object.url rescue ::S3::Error::NoSuchBucket raise IncompatibleOutputError, "Bucket #{bucket_name} not found" end end when /^http(s?):\/\// File.open(@temp_file.path, 'wb') do |file| HTTPClient.new.post(@url, [], {'Content-Type' => @content_type, :data => file}) end else raise IncompatibleOutputError, "Don't know to store output URL: #{@url}" end end |