Class: LinkShrink::CLI
Overview
A Simple class for the executable version of the gem
Instance Attribute Summary collapse
-
#options ⇒ String
readonly
Options for api settings.
Instance Method Summary collapse
- #argument_text_for(option) ⇒ Object
- #banner ⇒ Object
- #default_options ⇒ Object
-
#initialize(args) ⇒ CLI
constructor
A new instance of CLI.
-
#parse ⇒ String
Parses the command-line arguments and runs the executable.
- #process_url ⇒ Object
- #select_api ⇒ Object
-
#set_options(opts) ⇒ Object
Configures the arguments for the command.
- #url_present? ⇒ Boolean
Methods included from Util
Constructor Details
#initialize(args) ⇒ CLI
Returns a new instance of CLI.
14 15 16 |
# File 'lib/link_shrink/cli.rb', line 14 def initialize(args) @args, @options = args, OpenStruct.new() end |
Instance Attribute Details
#options ⇒ String (readonly)
Returns options for api settings.
12 13 14 |
# File 'lib/link_shrink/cli.rb', line 12 def @options end |
Instance Method Details
#argument_text_for(option) ⇒ Object
67 68 69 |
# File 'lib/link_shrink/cli.rb', line 67 def argument_text_for(option) "use #{option}".concat option.eql?(:Google) ? ' (Default)' : '' end |
#banner ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/link_shrink/cli.rb', line 18 def <<MSG Usage: link_shrink [OPTION] [URL] Description: LinkShrink, Turn long and nasty links into short urls. Options: MSG end |
#default_options ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/link_shrink/cli.rb', line 28 def apis = (available_shrinkers).reduce({}) do |opts, key| opts[key] = false opts end {}.merge({ api: apis.merge(Google: true), banner: , version: LinkShrink::VERSION }) end |
#parse ⇒ String
Parses the command-line arguments and runs the executable
73 74 75 76 77 78 |
# File 'lib/link_shrink/cli.rb', line 73 def parse opts = OptionParser.new(&method(:set_options)) opts.parse!(@args) return process_url if url_present? opts.help end |
#process_url ⇒ Object
80 81 82 83 84 |
# File 'lib/link_shrink/cli.rb', line 80 def process_url LinkShrink.configure { |config| config.api = select_api } LinkShrink.shrink_url(@args.last) end |
#select_api ⇒ Object
91 92 93 |
# File 'lib/link_shrink/cli.rb', line 91 def select_api .api.select { |_, v| v }.keys.last.to_s end |
#set_options(opts) ⇒ Object
Configures the arguments for the command
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/link_shrink/cli.rb', line 43 def (opts) opts.version, opts. = .version, . opts.set_program_name 'LinkShrink' .api.map do |k, v| arg = k.to_s.downcase opts.on_head("-#{arg[0]}", "--#{arg}", argument_text_for(k)) do .api[k] = true end end 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 |
#url_present? ⇒ Boolean
86 87 88 89 |
# File 'lib/link_shrink/cli.rb', line 86 def url_present? regexp = /^(http?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ !!(@args.last =~ regexp) end |