class VersacommerceAPI::Cli
Public Instance Methods
add(connection)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 20 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['password'] = ask("Password:") create_file(file, config.to_yaml) end if available_connections.one? default(connection) end end
console(connection=nil)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 95 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)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 77 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)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 51 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()
click to toggle source
# File lib/versacommerce_api/cli.rb, line 12 def list available_connections.each do |c| prefix = default?(c) ? " * " : " " puts prefix + c end end
remove(connection)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 40 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)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 65 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
Private Instance Methods
available_connections()
click to toggle source
# File lib/versacommerce_api/cli.rb, line 141 def available_connections @available_connections ||= begin pattern = File.join(shop_config_dir, "*.yml") Dir.glob(pattern).map { |f| File.basename(f, ".yml") } end end
config_file(connection)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 124 def config_file(connection) if connection File.join(shop_config_dir, "#{connection}.yml") else default_symlink end end
config_file_not_found_error(filename)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 160 def config_file_not_found_error(filename) raise ConfigFileError, "Could not find config file at #{filename}" end
default?(connection)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 156 def default?(connection) default_connection == connection end
default_connection()
click to toggle source
# File lib/versacommerce_api/cli.rb, line 152 def default_connection @default_connection ||= File.basename(default_connection_target, ".yml") end
default_connection_target()
click to toggle source
# File lib/versacommerce_api/cli.rb, line 148 def default_connection_target @default_connection_target ||= File.readlink(default_symlink) end
default_symlink()
click to toggle source
# File lib/versacommerce_api/cli.rb, line 120 def default_symlink @default_symlink ||= File.join(shop_config_dir, 'default') end
shop_config_dir()
click to toggle source
# File lib/versacommerce_api/cli.rb, line 116 def shop_config_dir @shop_config_dir ||= File.join(ENV['HOME'], '.versacommerce', 'shops') end
site_from_config(config)
click to toggle source
# File lib/versacommerce_api/cli.rb, line 132 def site_from_config(config) protocol = config['protocol'] || 'https' api_key = config['api_key'] password = config['password'] domain = config['domain'] VersacommerceAPI::Base.site = "#{protocol}://#{api_key}:#{password}@#{domain}/api" end