Class: Tootsie::Input

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Input

Returns a new instance of Input.



11
12
13
14
15
16
17
# File 'lib/tootsie/input.rb', line 11

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

#file_nameObject (readonly)

Returns the value of attribute file_name.



53
54
55
# File 'lib/tootsie/input.rb', line 53

def file_name
  @file_name
end

#urlObject (readonly)

Returns the value of attribute url.



52
53
54
# File 'lib/tootsie/input.rb', line 52

def url
  @url
end

Instance Method Details

#closeObject



48
49
50
# File 'lib/tootsie/input.rb', line 48

def close
  @temp_file.unlink
end

#get!Object



19
20
21
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
# File 'lib/tootsie/input.rb', line 19

def get!
  @logger.info("Fetching #{@url} as #{@temp_file.path}")
  case @url
    when /^file:(.*)/
      @file_name = $1
      raise InputNotFound, @url unless File.exist?(@file_name)
    when /^s3:.*$/
      s3_options = S3Utilities.parse_uri(@url)
      bucket_name, path = s3_options[:bucket], s3_options[:key]
      s3_service = Tootsie::Application.get.s3_service
      begin
        File.open(@temp_file.path, 'wb') do |f|
          object = s3_service.buckets.find(bucket_name).objects.find(path)
          object.send(:get_object) unless object.content  # Work around issue with s3 gem
          f << object.content
        end
      rescue ::S3::Error::NoSuchBucket, ::S3::Error::NoSuchKey
        raise InputNotFound, @url
      end
    when /http(s?):\/\//
      response = HTTPClient.new.get(@url)
      File.open(@temp_file.path, 'wb') do |f|
        f << response.body
      end
    else
      raise ArgumentError, "Don't know to handle URL: #{@url}"
  end
end