Class: MyOwnAdverts::CLI::Command

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

Class Method Summary collapse

Class Method Details



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/myownadverts/cli.rb', line 44

def banner
  puts <<-BANNER
USAGE:
Setup:
  myownadverts setup username password

Create advert for a siteid:
  myownadverts add siteid "some message"
BANNER
  exit
end

.config_fileObject



61
62
63
64
# File 'lib/myownadverts/cli.rb', line 61

def config_file
  home = ENV["HOME"] || ENV["HOMEPATH"] || File::expand_path("~")
  File::join home, ".myownadverts"
end

.create_advert(siteid, text) ⇒ Object



38
39
40
41
42
# File 'lib/myownadverts/cli.rb', line 38

def create_advert(siteid, text)
  MyOwnAdverts::Advert.(*username_password)
  MyOwnAdverts::Advert.siteid = siteid
  MyOwnAdverts::Advert.create :text => text, :disabled => false
end

.execute!Object



12
13
14
15
16
# File 'lib/myownadverts/cli.rb', line 12

def execute!
  command  = (ARGV[0] || "").downcase
  validate_command  command
  process_command command, ARGV[1..-1]
end

.exitObject

seperate method so can be mocked



57
58
59
# File 'lib/myownadverts/cli.rb', line 57

def exit
  Kernel.exit
end

.process_command(command, args) ⇒ Object



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

def process_command(command, args)
  case command.to_sym
  when :setup
     *args
  when :create, :add
    create_advert *args
  end
end

.setup_account(username, password) ⇒ Object



31
32
33
34
35
36
# File 'lib/myownadverts/cli.rb', line 31

def (username, password)
  File.open(config_file, 'w') do |f|
    f << {:username => username, :password => password}.to_yaml
  end
  File.chmod 0600, config_file
end

.username_passwordObject



66
67
68
69
# File 'lib/myownadverts/cli.rb', line 66

def username_password
  config = YAML.load_file(config_file)
  [config[:username], config[:password]]
end

.validate_command(command) ⇒ Object



18
19
20
# File 'lib/myownadverts/cli.rb', line 18

def validate_command(command)
  banner unless %w[setup add create].include?(command)
end