Class: VersacommerceAPI::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/versacommerce_api/cli.rb

Defined Under Namespace

Classes: ConfigFileError

Instance Method Summary collapse

Instance Method Details

#add(connection) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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' => 'https'}
    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 https://#{config['domain']}/admin/settings/apps in your browser to get API credentials\n"
    config['api_key']  = ask("API key :")
    config['secret'] = ask("Secret:")
    config['password'] = get_password(config['domain'], config['api_key'], config['secret'])
    create_file(file, config.to_yaml)
  end
  if available_connections.one?
    default(connection)
  end
end

#console(connection = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/versacommerce_api/cli.rb', line 97

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/versacommerce_api/cli.rb', line 79

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/versacommerce_api/cli.rb', line 53

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

#listObject



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



42
43
44
45
46
47
48
49
50
# File 'lib/versacommerce_api/cli.rb', line 42

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



67
68
69
70
71
72
73
74
75
76
# File 'lib/versacommerce_api/cli.rb', line 67

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