Class: Phantomblaster::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/phantomblaster/cli.rb

Instance Method Summary collapse

Instance Method Details

#accountObject



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

def 
  title = 'Phantombuster Account'
  headings = ['Email', 'API Key']
  rows = [[current_user.email, Phantomblaster.configuration.api_key]]
  table = Terminal::Table.new title: title, headings: headings, rows: rows
  say table
end

#agentsObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/phantomblaster/cli.rb', line 20

def agents
  title = 'Phantombuster Agents'
  headings = ['Agent Name', 'Agent ID', 'Script ID', 'Last Status']
  rows = []
  current_agents.each do |agent|
    rows << [agent['name'], agent['id'], agent['scriptId'], agent['lastEndStatus']]
  end
  table = Terminal::Table.new title: title, headings: headings, rows: rows
  say table
end

#download(name, force = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/phantomblaster/cli.rb', line 57

def download(name, force = false)
  script = Phantomblaster::Models::Script.find_by_name(name)
  folder_pathname = Pathname.new(Phantomblaster.configuration.scripts_dir)
  file_pathname = Pathname.new(name)
  full_pathname = folder_pathname.join(file_pathname)

  unless folder_pathname.directory?
    return unless yes?("Directory #{folder_pathname.realdirpath} does not exist. Create it?")

    folder_pathname.mkpath
  end

  if !force && full_pathname.exist?
    return unless yes?("File #{full_pathname.realpath} already exists. Overwrite?")
  end

  full_pathname.open('w') { |f| f << script.text }
  say "Wrote #{full_pathname.realpath}"
end

#generate(name) ⇒ Object



44
45
46
# File 'lib/phantomblaster/cli.rb', line 44

def generate(name)
  Phantomblaster::Generators::Script.start([name])
end

#pullObject



78
79
80
81
82
83
84
85
# File 'lib/phantomblaster/cli.rb', line 78

def pull
  return unless yes?("This will pull from Phantombuster and overwrite any existing scripts. " \
                     "Are you sure you want to continue?")

  current_scripts.each do |script|
    download(script.name, true)
  end
end

#pushObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/phantomblaster/cli.rb', line 88

def push
  return unless yes?("This will push all local scripts to Phantombuster and overwrite any " \
                     "existing scripts. Are you sure you want to continue?")

  folder_pathname = Pathname.new(Phantomblaster.configuration.scripts_dir)
  raise 'Scripts directory does not exist' unless folder_pathname.directory?

  Pathname.glob("#{folder_pathname.realdirpath}/**/*.js").each do |pn|
    _dir, file = pn.split
    upload(file)
  end
end

#scriptsObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/phantomblaster/cli.rb', line 32

def scripts
  title = 'Phantombuster Scripts'
  headings = ['Script Name', 'Script ID', 'Script Source', 'Script Last Saved At']
  rows = []
  current_scripts.each do |script|
    rows << [script.name, script.id, script.source, script.last_saved_at]
  end
  table = Terminal::Table.new title: title, headings: headings, rows: rows
  say table
end

#upload(name) ⇒ Object



49
50
51
52
53
54
# File 'lib/phantomblaster/cli.rb', line 49

def upload(name)
  return unless yes?("Upload #{name} to Phantombuster?")

  res = Phantomblaster::Models::Script.upload(name)
  say res
end