Class: VersacommerceAPI::Cli
- Inherits:
-
Thor
- Object
- Thor
- VersacommerceAPI::Cli
- Includes:
- Thor::Actions
- Defined in:
- lib/versacommerce_api/cli.rb
Defined Under Namespace
Classes: ConfigFileError
Instance Method Summary collapse
- #add(connection) ⇒ Object
- #console(connection = nil) ⇒ Object
- #default(connection = nil) ⇒ Object
- #edit(connection = nil) ⇒ Object
- #list ⇒ Object
- #remove(connection) ⇒ Object
- #show(connection = nil) ⇒ Object
Instance Method Details
#add(connection) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/versacommerce_api/cli.rb', line 21 def add(connection) file = config_file(connection) if File.exist?(file) raise ConfigFileError, "There is already a config file at #{file}" else config = {'protocol' => 'http'} config['domain'] = ask("Domain (leave blank for #{connection}.versacommerce.de):") config['domain'] = "#{connection}.versacommerce.de" if config['domain'].blank? config['domain'] = "#{config['domain']}.versacommerce.de" unless config['domain'].match(/[.:]/) puts "\nopen http://#{config['domain']}/admin/settings/apps in your browser to get API credentials\n" config['api_key'] = ask("API key :") config['password'] = ask("Password:") create_file(file, config.to_yaml) end if available_connections.one? default(connection) end end |
#console(connection = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/versacommerce_api/cli.rb', line 96 def console(connection=nil) file = config_file(connection) config = YAML.load(File.read(file)) puts "" puts "--> Starting interactive API console for #{config['domain']} - #{file}" puts "" VersacommerceAPI::Base.site = site_from_config(config) require 'irb' require 'irb/completion' ARGV.clear IRB.start end |
#default(connection = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/versacommerce_api/cli.rb', line 78 def default(connection=nil) if connection target = config_file(connection) if File.exist?(target) remove_file(default_symlink) `ln -s #{target} #{default_symlink}` else config_file_not_found_error(target) end end if File.exist?(default_symlink) puts "Default connection is #{default_connection}" else puts "There is no default connection set" end end |
#edit(connection = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/versacommerce_api/cli.rb', line 52 def edit(connection=nil) file = config_file(connection) if File.exist?(file) if ENV['EDITOR'].present? system(ENV['EDITOR'], file) else puts "Please set an editor in the EDITOR environment variable" end else config_file_not_found_error(file) end end |
#list ⇒ Object
13 14 15 16 17 18 |
# File 'lib/versacommerce_api/cli.rb', line 13 def list available_connections.each do |c| prefix = default?(c) ? " * " : " " puts prefix + c end end |
#remove(connection) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/versacommerce_api/cli.rb', line 41 def remove(connection) file = config_file(connection) if File.exist?(file) remove_file(default_symlink) if default?(connection) remove_file(file) else config_file_not_found_error(file) end end |
#show(connection = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/versacommerce_api/cli.rb', line 66 def show(connection=nil) connection ||= default_connection file = config_file(connection) if File.exist?(file) puts file puts `cat #{file}` else config_file_not_found_error(file) end end |