Class: LinkShrink::CLI

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/link_shrink/cli.rb

Overview

A Simple class for the executable version of the gem

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

available_shrinkers

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • args (Array<String>)

    The command-line arguments



14
15
16
# File 'lib/link_shrink/cli.rb', line 14

def initialize(args)
  @args, @options = args, OpenStruct.new(default_options)
end

Instance Attribute Details

#optionsString (readonly)

Returns options for api settings.

Returns:

  • (String)

    options for api settings



12
13
14
# File 'lib/link_shrink/cli.rb', line 12

def options
  @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


18
19
20
21
22
23
24
25
26
# File 'lib/link_shrink/cli.rb', line 18

def banner
  <<MSG
Usage: link_shrink [OPTION] [URL]
Description:
  LinkShrink, Turn long and nasty links into short urls.

Options:
MSG
end

#default_optionsObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/link_shrink/cli.rb', line 28

def default_options
  apis = (available_shrinkers).reduce({}) do |opts, key|
    opts[key] = false
    opts
  end

  {}.merge({
    api: apis.merge(Google: true),
    banner: banner,
    version: LinkShrink::VERSION
  })
end

#parseString

Parses the command-line arguments and runs the executable

Returns:

  • (String)

    The short url or argument passed



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_urlObject



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_apiObject



91
92
93
# File 'lib/link_shrink/cli.rb', line 91

def select_api
  options.api.select { |_, v| v }.keys.last.to_s
end

#set_options(opts) ⇒ Object

Configures the arguments for the command

Parameters:

  • opts (OptionParser)


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 set_options(opts)
  opts.version, opts.banner = options.version, options.banner
  opts.set_program_name 'LinkShrink'

  options.api.map do |k, v|
    arg = k.to_s.downcase

    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.ver
    exit
  end

  opts.on_tail('-h', '--help', 'print this help') do
    puts opts.help
    exit
  end
end

#url_present?Boolean

Returns:

  • (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