lib/guard/shopify.rb in guard-shopify-0.0.2 vs lib/guard/shopify.rb in guard-shopify-0.0.3

- old
+ new

@@ -1,36 +1,61 @@ require 'guard' +require 'yaml' require 'guard/guard' require 'shopify_api' require 'ptools' module Guard class Shopify < Guard def start - set_config unless File.exists?("#{ENV['HOME']}/.guard_shopify") + set_config unless File.exists?(config_file_path) get_credentials_from_config authenticate_with_shopify get_main_theme_id end + def config_file_path + "#{ENV['HOME']}/.guard_shopify" + end + def get_credentials_from_config - credentials = File.read("#{ENV['HOME']}/.guard_shopify").split("\n") + config = YAML.load_file(config_file_path) + # Check if config file is old format or new YAML-based one + unless config['secret'] + config = upgrade_config_file + end + @url = config['url'] + @api_key = config['api_key'] + @password = config['password'] + end - @api_key = credentials[0] - @password = credentials[1] - @url = credentials[2] + # Old line-based config file format + def upgrade_config_file + puts "Old config file found, upgrading..." + + credentials = File.read(config_file_path).split("\n") + config = {} + config['api_key'] = credentials[0] + config['password'] = credentials[1] + config['url'] = credentials[2] + + config['secret'] = prompt "Please enter your API Shared Secret" + + write_config config + + puts 'Upgraded old config file to new format' + + config end def authenticate_with_shopify ShopifyAPI::Base.site = "https://#{@api_key}:#{@password}@#{@url}/admin" end def get_main_theme_id themes = ShopifyAPI::Theme.find(:all) - main_theme = themes.select {|t| t.role == 'main'}.first - @theme_id = main_theme.id end def run_on_change(paths) paths.each do |p| @@ -65,22 +90,33 @@ puts "#{path} uploaded to #{@url}" end private + def prompt msg=nil + print "#{msg || 'Please type'}: " + gets.chomp + end + + def write_config config + File.open(config_file_path, 'w') {|f| f.write(YAML.dump(config)) } + end + def set_config + config = {} puts "This guard needs your API credentials to access your Shopify store." - puts "Please enter your API key:" - api_key = gets.chomp - puts "Please enter your password:" - password = gets.chomp - puts "Please enter your store's url (e.g. awesomesauce.myshopify.com):" - url = gets.chomp - url = url + '.myshopify.com' unless url.include?('myshopify.com') - config = [api_key, password, url].join("\n") - - File.open("#{ENV['HOME']}/.guard_shopify", 'w') {|f| f.write(config) } - puts "Credentials saved to #{ENV['HOME']}/.guard_shopify" + config['api_key'] = prompt 'Please enter your API key' + + config['secret'] = prompt 'Please enter your API Shared Secret' + + config['url'] = prompt "Please enter your store's url (e.g. awesomesauce.myshopify.com)" + + unless config['url'].include?('myshopify.com') + config['url'] = "#{config['url']}.myshopify.com" + end + + write_config config + puts "Credentials saved to #{config_file_path}" end end end