Class: MyOwnAdverts::CLI::Command
- Inherits:
-
Object
- Object
- MyOwnAdverts::CLI::Command
- Defined in:
- lib/myownadverts/cli.rb
Class Method Summary collapse
- .banner ⇒ Object
- .config_file ⇒ Object
- .create_advert(siteid, text) ⇒ Object
- .execute! ⇒ Object
-
.exit ⇒ Object
seperate method so can be mocked.
- .process_command(command, args) ⇒ Object
- .setup_account(username, password) ⇒ Object
- .username_password ⇒ Object
- .validate_command(command) ⇒ Object
Class Method Details
.banner ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/myownadverts/cli.rb', line 44 def puts <<-BANNER USAGE: Setup: myownadverts setup username password Create advert for a siteid: myownadverts add siteid "some message" BANNER exit end |
.config_file ⇒ Object
61 62 63 64 |
# File 'lib/myownadverts/cli.rb', line 61 def config_file home = ENV["HOME"] || ENV["HOMEPATH"] || File::("~") 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.login(*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 |
.exit ⇒ Object
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 setup_account *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 setup_account(username, password) File.open(config_file, 'w') do |f| f << {:username => username, :password => password}.to_yaml end File.chmod 0600, config_file end |
.username_password ⇒ Object
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) unless %w[setup add create].include?(command) end |