Class: Tootsie::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/tootsie/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_typeObject

Returns the value of attribute content_type.



63
64
65
# File 'lib/tootsie/output.rb', line 63

def content_type
  @content_type
end

#file_nameObject (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_urlObject (readonly)

Returns the value of attribute result_url.



61
62
63
# File 'lib/tootsie/output.rb', line 61

def result_url
  @result_url
end

#urlObject (readonly)

Returns the value of attribute url.



60
61
62
# File 'lib/tootsie/output.rb', line 60

def url
  @url
end

Instance Method Details

#closeObject



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!(options = {})
  @logger.info("Storing #{@url}")
  case @url
    when /^file:(.*)/
      FileUtils.cp(@temp_file.path, $1)
      @result_url = @url
    when /^s3:.*/
      s3_options = S3Utilities.parse_uri(@url)
      bucket_name, path = s3_options[:bucket], s3_options[: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 = s3_options[:acl] || :private
          object.content_type = s3_options[:content_type]
          object.content_type ||= @content_type if @content_type
          object.storage_class = s3_options[: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