lib/link_shrink/cli.rb in link_shrink-0.0.6 vs lib/link_shrink/cli.rb in link_shrink-0.0.7

- old
+ new

@@ -1,99 +1,95 @@ require 'optparse' +require 'ostruct' module LinkShrink # @author Jonah Ruiz <jonah@pixelhipsters.com> # A Simple class for the executable version of the gem class CLI + include LinkShrink::Util + # @!attribute [r] options + # @return [String] options for api settings + attr_reader :options # @param args [Array<String>] The command-line arguments def initialize(args) - @args = args + @args, @options = args, OpenStruct.new(default_options) end - # Configures the arguments for the command - # @param opts [OptionParser] - def set_options(opts) - %w(@json @qr_code @tiny_url) - @json = false - @qr_code = false - @tiny_url = false - @google = false - @is_gd = false - @owly = false - opts.version = LinkShrink::VERSION - opts.banner = <<MSG + def banner + <<MSG Usage: link_shrink [OPTION] [URL] Description: LinkShrink, Turn long and nasty links into short urls. Options: MSG - opts.set_program_name 'LinkShrink' - opts.on_head('-t', '--tinyurl', 'use TinyURL') do - @tiny_url = :true - end + end - opts.on_head('-i', '--isgd', 'use Is.gd') do - @is_gd = :true + def default_options + apis = (available_shrinkers).reduce({}) do |opts, key| + opts[key] = false + opts end - opts.on_head('-o', '--owly', 'use Owly') do - @owly = :true - end + {}.merge({ + api: apis.merge(Google: true), + banner: banner, + version: LinkShrink::VERSION + }) + end - opts.on_head('-g', '--google', 'use Google (Default)') do - @google = :true - end + # Configures the arguments for the command + # @param opts [OptionParser] + def set_options(opts) + opts.version, opts.banner = options.version, options.banner + opts.set_program_name 'LinkShrink' - opts.on_head('-j', '--json', 'return JSON response') do - @json = :true - end + options.api.map do |k, v| + arg = k.to_s.downcase - opts.on_head('-q', '--qrcode', 'return QR Code') do - @qr_code = :true unless @tiny_url + opts.on_head("-#{arg[0]}", "--#{arg}", argument_text_for(k)) do + options.api[k] = true + end end - opts.on_tail('-v', '--version', 'display the version of LinkShrink and exit') do - puts opts.version + opts.on_tail('-v', '--version', + 'display the version of LinkShrink and exit') do + puts opts.ver exit end opts.on_tail('-h', '--help', 'print this help') do puts opts.help exit end end + def argument_text_for(option) + "use #{option}".concat option.eql?(:Google) ? ' (Default)' : '' + end + # Parses the command-line arguments and runs the executable # @return [String] The short url or argument passed def parse opts = OptionParser.new(&method(:set_options)) opts.parse!(@args) return process_url if url_present? opts.help end def process_url - api = select_api - LinkShrink.configure { |c| c.api = api } - LinkShrink.shrink_url(@args.last, { json: @json, qr_code: @qr_code }) + LinkShrink.configure { |config| config.api = select_api } + + LinkShrink.shrink_url(@args.last) end def url_present? - !!(@args.last =~ /^(http?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/) + regexp = /^(http?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ + !!(@args.last =~ regexp) end def select_api - case - when @tiny_url - 'TinyUrl' - when @is_gd - 'IsGd' - when @owly - 'Owly' - else - 'Google' - end + options.api.select { |_, v| v }.keys.last.to_s end end end