Class: Worochi::Agent

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

Overview

The parent class for all service agents.

Direct Known Subclasses

Box, Dropbox, Example, Github, GoogleDrive

Defined Under Namespace

Classes: Box, Dropbox, Example, Github, GoogleDrive

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Agent

Returns a new instance of Agent.

Parameters:

  • opts (Hash) (defaults to: {})

    service options



11
12
13
14
# File 'lib/worochi/agent.rb', line 11

def initialize(opts={})
  set_options(opts)
  init_client
end

Instance Attribute Details

#optionsHashie::Mash

Service options.

Returns:

  • (Hashie::Mash)


8
9
10
# File 'lib/worochi/agent.rb', line 8

def options
  @options
end

Class Method Details

.new(opts = {}) ⇒ Agent

Creates a new service-specific Worochi::Agent based on :service.

Examples:

Worochi::Agent.new({ service: :github, token:'6st46setsybhd64' })

Parameters:

  • opts (Hash) (defaults to: {})

    service options; must contain :service key.

Returns:



223
224
225
226
227
228
229
230
231
# File 'lib/worochi/agent.rb', line 223

def new(opts={})
  service = opts[:service]
  if self.name == 'Worochi::Agent'
    raise Error, 'Invalid service' unless Config.services.include?(service)
    Agent.const_get(class_name(service)).new(opts)
  else
    super
  end
end

Instance Method Details

#files(details) ⇒ Array<String>, Array<Hash> #files(path, details = false) ⇒ Array<String>, Array<Hash>

Returns a list of files at the remote path specified by options[:dir]. Relies on the service-specific implementation of #list.

Examples:

agent = Worochi.create(:dropbox, 'sfsFj41na89cx', dir: '/abc')
agent.files # => ["k.jpg", "t.txt"]
agent.files(true)
# => [
#      { name: "k.jpg", type: "file", path: "/abc/k.jpg"},
#      { name: "t.txt", type: "file", path: "/abc/t.txt"}
#    ]

Overloads:

  • #files(details) ⇒ Array<String>, Array<Hash>

    Parameters:

    • details (Boolean)

      display more information

  • #files(path, details = false) ⇒ Array<String>, Array<Hash>

    Parameters:

    • path (String)

      remote path to list instead of current directory

    • details (Boolean) (defaults to: false)

      display more information

Returns:

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

    list of files



76
77
78
# File 'lib/worochi/agent.rb', line 76

def files(*args)
  list_helper(:files, args)
end

#files_and_folders(*args) ⇒ Array<String>, Array<Hash>

Returns a list of files and folders at the remote path specified by options[:dir]. Relies on the service-specific implementation of #list. Refer to #files for overloaded prototypes.

Returns:

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

    list of files and folders



98
99
100
# File 'lib/worochi/agent.rb', line 98

def files_and_folders(*args)
  list_helper(:both, args)
end

#folders(*args) ⇒ Array<String>, Array<Hash>

Returns a list of subdirectories at the remote path specified by options[:dir]. Relies on the service-specific implementation of #list. Refer to #files for overloaded prototypes.

Examples:

agent = Worochi.create(:dropbox, 'sfsFj41na89cx', dir: '/abc')
agent.folders # => ["folder1", "folder2"]

Returns:

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

    list of subdirectories

See Also:



89
90
91
# File 'lib/worochi/agent.rb', line 89

def folders(*args)
  list_helper(:folders, args)
end

#nameString

Returns the display name for the agent’s service.

Returns:

  • (String)

    display name



132
133
134
# File 'lib/worochi/agent.rb', line 132

def name
  Worochi::Config.service_display_name(options.service)
end

#push(origin, opts = nil) ⇒ nil

Push list of files to the service. Refer to Item.open for how to format the file list. An optional opts hash can be used to update the agent options before pushing.

Examples:

agent = Worochi.create(:github, 'sfsFj41na89cx')
agent.push({ source: 'http://a.com/file.jpg', path: 'folder/file.jpg' })

Parameters:

  • origin (Array<Hash>, Array<String>, Hash, String)
  • opts (Hash) (defaults to: nil)

    update agent options before pushing

Returns:

  • (nil)

See Also:



27
28
29
30
31
32
# File 'lib/worochi/agent.rb', line 27

def push(origin, opts=nil)
  set_options(opts) unless opts.nil?
  items = Item.open(origin)
  push_items(items)
  nil
end

#push_items(items) ⇒ nil

Push a list of Item to the service. Usually called by #push.

Parameters:

  • items (Array<Item>)

Returns:

  • (nil)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/worochi/agent.rb', line 38

def push_items(items)
  items.each { |item| item.content.rewind }
  Worochi::Log.info "Pushing #{items.size} items to #{type}"
  if respond_to?(:push_all)
    push_all(items)
  else
    items.each { |item| push_item(item) }
  end
  Worochi::Log.info "Push to #{type} completed"
  nil
end

#removenil

Remove the agent from the list of active agents responding to calls to Worochi.push.

Returns:

  • (nil)


54
55
56
57
# File 'lib/worochi/agent.rb', line 54

def remove
  Worochi.remove(self)
  nil
end

#set_dir(path) ⇒ Hashie::Mash

Sets the remote target directory path. This is the same as modifying options[:dir].

Parameters:

  • path (String)

    the new path

Returns:

  • (Hashie::Mash)

    the updated options



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

def set_dir(path)
  options.dir = path
  options
end

#set_options(opts = {}) ⇒ Hashie::Mash

Updates #options using opts.

Parameters:

  • opts (Hash) (defaults to: {})

    new options

Returns:

  • (Hashie::Mash)

    the updated options



106
107
108
109
110
# File 'lib/worochi/agent.rb', line 106

def set_options(opts={})
  self.options ||= default_options
  opts = Hashie::Mash.new(opts)
  options.merge!(opts)
end

#typeSymbol

Returns the service type for the agent.

Returns:

  • (Symbol)

    service type



125
126
127
# File 'lib/worochi/agent.rb', line 125

def type
  options.service
end