Class: Worochi::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/worochi/item.rb

Overview

This represents a single file that is being pushed. The #content attribute holds an IO object that is read by the push agent and the #path attribute is the relative path to the remote target directory that the file will be pushed to.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, content) ⇒ Item

Returns a new instance of Item.

Parameters:

  • path (String)

    relative destination path including file name

  • content (IO)

    file content


28
29
30
31
32
33
# File 'lib/worochi/item.rb', line 28

def initialize(path, content)
  @path = path
  @content = content
  detect_type
  @content.rewind
end

Instance Attribute Details

#contentIO

An IO object containing the content of the file being pushed.

Returns:

  • (IO)

24
25
26
# File 'lib/worochi/item.rb', line 24

def content
  @content
end

#pathString

The relative path of the object from the target root directory. If the Worochi::Item was initialized without a `:path` option, this attribute defaults to the file name.

Returns:

  • (String)

20
21
22
# File 'lib/worochi/item.rb', line 20

def path
  @path
end

Class Method Details

.open(origin) ⇒ Array<Item>

Takes in either a single file entry or a list of file entries and parses them into a list of Worochi::Item objects. Each entry can be either a String specifying the source location of the file, or a Hash specifying both the `:source` location and the remote `:path` to push the file to.

Examples:

Single file

Item.open('folder/file.txt')

Multiple files

Item.open(['folder/file1.txt', 'folder/file2.txt'])

Remote path

Item.open({
  source: 'http://a.com/file.jpg',
  path: 'folder/file.jpg'
})

Remote path with mixed origins

a = { source: 'http://a.com/file.jpg', path: 'folder/file1.jpg' }
b = { source: 'folder/file.jpg', path: 'folder/file2.jpg' }
c = { source: 's3:folder/file.jpg', path: 'folder/file3.jpg' }
Item.open([a, b, c])
# c is an example of retrieving files using an AWS S3 path

Parameters:

  • origin (Array<Hash>, Array<String>, Hash, String)

Returns:


117
118
119
120
# File 'lib/worochi/item.rb', line 117

def open(origin)
  file_list = origin.kind_of?(Array) ? origin : [origin]
  file_list.map { |entry| open_single(entry) }
end

.open_single(entry) ⇒ Item

Takes in a single entry from open and creates an Worochi::Item.

Parameters:

  • entry (Hash, String)

    the file metadata

Returns:

  • (Item)

    the file item


126
127
128
129
130
131
132
133
134
135
# File 'lib/worochi/item.rb', line 126

def open_single(entry)
  if entry.kind_of?(Hash)
    source = entry[:source] || entry['source']
    path = entry[:path] || entry['path']
  else
    source = entry
  end

  Item.new(path || File.basename(source), retrieve(source))
end

Instance Method Details

#content_typeString

Returns mime-type of the content.

Returns:

  • (String)

    mime-type of the content.


64
65
66
# File 'lib/worochi/item.rb', line 64

def content_type
  @type
end

#filenameString

Returns the filename.

Returns:

  • (String)

    the filename


36
37
38
# File 'lib/worochi/item.rb', line 36

def filename
  File.basename(path)
end

#read(*args) ⇒ String

Read from the content. This is just a wrapper for the `#read` method on #content and any arguments will be passed on to the IO object.

Returns:

  • (String)

51
52
53
# File 'lib/worochi/item.rb', line 51

def read(*args)
  content.read(*args)
end

#rewind0

Rewinds the content. This is just a wrapper for the `#rewind` method on #content.

Returns:

  • (0)

59
60
61
# File 'lib/worochi/item.rb', line 59

def rewind
  content.rewind
end

#sizeInteger

The total size of the content in bytes.

Returns:

  • (Integer)

43
44
45
# File 'lib/worochi/item.rb', line 43

def size
  content.size
end