Class: ShopifyAPI::Cli

Inherits:
Thor show all
Includes:
Thor::Actions
Defined in:
lib/shopify_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
40
41
42
43
# File 'lib/shopify_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}.myshopify.com)")
    config['domain']   = "#{connection}.myshopify.com" if config['domain'].blank?
    config['domain']   = "#{config['domain']}.myshopify.com" unless config['domain'].match(/[.:]/)
    puts "\nopen https://#{config['domain']}/admin/apps/private in your browser to create a private app and get API credentials\n"
    config['api_key']  = ask("API key?")
    config['password'] = ask("Password?")
    if ask("Would you like to use pry as your shell? (y/n)") === "y"
      config["shell"] = "pry"
    else
      config["shell"] = "irb"
    end
    create_file(file, config.to_yaml)
  end
  if available_connections.one?
    default(connection)
  end
end

#console(connection = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/shopify_api/cli.rb', line 101

def console(connection=nil)
  file = config_file(connection)

  config = YAML.load(File.read(file))
  puts "using #{config['domain']}"
  ShopifyAPI::Base.site = site_from_config(config)

  launch_shell(config)
end

#default(connection = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/shopify_api/cli.rb', line 83

def default(connection=nil)
  if connection
    target = config_file(connection)
    if File.exist?(target)
      remove_file(default_symlink)
      `ln -s #{target} #{default_symlink}`
    else
      no_config_file_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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shopify_api/cli.rb', line 57

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
    no_config_file_error(file)
  end
end

#listObject



13
14
15
16
17
18
# File 'lib/shopify_api/cli.rb', line 13

def list
  available_connections.each do |c|
    prefix = default?(c) ? " * " : "   "
    puts prefix + c
  end
end

#remove(connection) ⇒ Object



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

def remove(connection)
  file = config_file(connection)
  if File.exist?(file)
    remove_file(default_symlink) if default?(connection)
    remove_file(file)
  else
    no_config_file_error(file)
  end
end

#show(connection = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/shopify_api/cli.rb', line 71

def show(connection=nil)
  connection ||= default_connection
  file = config_file(connection)
  if File.exist?(file)
    puts file
    puts `cat #{file}`
  else
    no_config_file_error(file)
  end
end