bin/PinPress in pinpress-1.0.0 vs bin/PinPress in pinpress-1.0.1

- old
+ new

@@ -38,23 +38,23 @@ include CLIUtils::Configuration include CLIUtils::Messaging include GLI::App -# ====================================================== -# App Info -# ====================================================== +# ====================================================== +# App Info +# ====================================================== program_desc PinPress::DESCRIPTION version PinPress::VERSION -# ====================================================== -# Global Flags and Switches -# ====================================================== +# ====================================================== +# Global Flags and Switches +# ====================================================== -# ====================================================== -# Pre, Post, and Error -# ====================================================== +# ====================================================== +# Pre, Post, and Error +# ====================================================== pre do |global, command, options, args| # Load PinPress configuration module. load_configuration(PinPress::CONFIG_FILEPATH) file_logger = Logger.new(PinPress::LOG_FILEPATH) file_logger.level = LOG_LEVELS[configuration.pinpress[:log_level] || 'DEBUG'] @@ -87,19 +87,19 @@ messenger.error(exception.to_s) exit!(1) true end -# ====================================================== -# Commands -# ====================================================== -# ------------------------------------------------------ -# init command +# ====================================================== +# Commands +# ====================================================== +# ------------------------------------------------------ +# init command # -# Initializes the app by asking the user for information -# needed torun. -# ------------------------------------------------------ +# Initializes the app by asking the user for information +# needed to run. +# ------------------------------------------------------ desc 'Install and initialize dependencies' command :init do |c| c.switch([:s], desc: 'Run init from scratch (i.e., clear out all values from configuration)') c.action do |global_options, options, args| if options[:s] @@ -109,80 +109,118 @@ PinPress.init if messenger.prompt(long_message, 'N').downcase == 'y' end end end -# ------------------------------------------------------ -# pins command +# ------------------------------------------------------ +# pins command # -# Gets pins from Pinboard. -# ------------------------------------------------------ +# Gets pins from Pinboard. +# ------------------------------------------------------ desc 'Get posts from Pinboard' command :pins do |c| c.flag([:e], desc: 'The end date to pull pins to') - c.flag([:f], desc: 'Output the results to a file') c.flag([:n], desc: 'The number of results to return') c.flag([:s], desc: 'The start date to pull pins from') c.flag([:t], desc: 'The tags to use (e.g., "ruby,pinboard")') - c.flag([:template], desc: 'The template to use') c.action do |global_options, options, args| - # Determine the template to use. - # template = PinPress.assign_template(args[0]) + # Two scenarios covered here: + # 1. If the user passes a valid name, grab that template. + # 2. If no name is passed, grabbed the default template + # If both of these conditions fail, an error message is shown. + template_type = PinPress::Template::TEMPLATE_TYPE_PIN + template_name = args.empty? ? nil : args[0] - if args.empty? - template = PinPress.get_template - else - template = PinPress.get_template(args[0]) - end - - if !template.nil? + PinPress.execute_template(template_type, template_name) do |template, client| # Create a Pinboard client and set options based on # PinPress flags. opts = {} - client = Pinboard::Client.new(token: configuration.pinpress[:api_token]) opts.merge!(todt: Chronic.parse(options[:e])) if options[:e] opts.merge!(results: options[:n]) if options[:n] opts.merge!(fromdt: Chronic.parse(options[:s])) if options[:s] opts.merge!(tag: options[:t]) if options[:t] begin pins = client.posts(opts) - puts template[:opener] + print template.opener if template.opener pins.each do |p| href = p[:href] description = p[:description] extended = p[:extended] tag = p[:tag] time = p[:time] replace = p[:replace] shared = p[:shared] toread = p[:toread] + if template.item + item = template.item + item += template.item_separator unless p == pins.last + print ERB.new(item).result(binding) + end + end + print template.closer if template.closer + rescue StandardError => e + fail "Pinboard API failed; are you sure you've run " \ + " `pinpress init` (and that your API key is correct)?" + end + end + end +end - line = ERB.new <<-LINETEMPLATE - #{ template[:item] } - LINETEMPLATE - puts line.result(binding) +# ------------------------------------------------------ +# tags command +# +# Gets pins from Pinboard. +# ------------------------------------------------------ +desc 'Get tags from Pinboard' +command :tags do |c| + c.flag([:e], desc: 'The end date to pull pins to') + c.flag([:s], desc: 'The start date to pull pins from') + + c.desc 'Gets the unique tags based on the passed criteria' + c.action do |global_options, options, args| + template_type = PinPress::Template::TEMPLATE_TYPE_TAG + template_name = args.empty? ? nil : args[0] + + PinPress.execute_template(template_type, template_name) do |template, client| + opts = {} + opts.merge!(todt: Chronic.parse(options[:e])) if options[:e] + opts.merge!(fromdt: Chronic.parse(options[:s])) if options[:s] + + begin + tags = [] + pins = client.posts(opts) + pins.each { |p| tags = tags + p[:tag] } + tags = tags.uniq.map { |t| { tag: t, count: tags.count(t) } } + + tags.each do |t| + tag = t[:tag] + count = t[:count] + + if template.item + item = template.item + item += template.item_separator unless t == tags.last + print ERB.new(item).result(binding) + end end - puts template[:closer] rescue StandardError => e + p e.to_s fail "Pinboard API failed; are you sure you've run " \ " `pinpress init` (and that your API key is correct)?" end - else - fail 'Invalid template provided and/or no valid default template exists!' end end end -# ------------------------------------------------------ -# template command +# ------------------------------------------------------ +# templates command # -# Manages templates -# ------------------------------------------------------ +# Manages pin templates. +# ------------------------------------------------------ desc 'Work with templates for pin output' -command :template do |c| +command :templates do |c| c.desc 'Choose the default template' c.command :default do |default| default.action do |global_options, options, args| PinPress.choose_default_template end @@ -195,9 +233,9 @@ end end c.default_command :list end -# ====================================================== -# Run! -# ====================================================== +# ====================================================== +# Run! +# ====================================================== exit run(ARGV) \ No newline at end of file