class Embedly::CommandLine::Parser

Attributes

options[RW]

Public Class Methods

new(args) click to toggle source
# File lib/embedly/command_line.rb, line 9
def initialize(args)
  @options, @args = default, args
end
parse!(args) click to toggle source
# File lib/embedly/command_line.rb, line 24
def self.parse!(args)
  new(args).parse!
end

Public Instance Methods

parse!() click to toggle source
# File lib/embedly/command_line.rb, line 13
def parse!
  parser.parse!(@args)
  set_urls!
  reject_nil!
  options
rescue OptionParser::InvalidOption => error
  puts "ERROR: #{error.message}"
  puts parser.on_tail
  exit
end

Private Instance Methods

default() click to toggle source
# File lib/embedly/command_line.rb, line 30
def default
  {
    :key => ENV['EMBEDLY_KEY'],
    :secret => ENV['EMBEDLY_SECRET'],
    :timeout => nil,
    :headers => {},
    :query => {}
  }
end
parser() click to toggle source
# File lib/embedly/command_line.rb, line 49
      def parser
        OptionParser.new do |parser|
          parser.banner = %{
Fetch JSON from the embedly service.
Usage [OPTIONS] <url> [url] ..
}

          parser.separator ""
          parser.separator "Options:"

          parser.on('-H', '--hostname ENDPOINT', 'Embedly host. Default is api.embed.ly.') do |hostname|
            options[:hostname] = hostname
          end

          parser.on("--header NAME=VALUE", "HTTP header to send with requests.") do |hash|
            header, value = hash.split '='
            options[:headers][header] = value
          end

          parser.on("-k", "--key KEY", "Embedly key [default: EMBEDLY_KEY environmental variable]") do |key|
            options[:key] = key
          end

          parser.on("-N", "--no-key", "Ignore EMBEDLY_KEY environmental variable") do |key|
            options[:key] = nil
          end

          parser.on("-s", "--secret SECRET", "Embedly secret [default: EMBEDLY_SECRET environmental variable]") do |secret|
            options[:secret] = secret
          end

          parser.on("--no-secret", "Ignore EMBEDLY_SECRET environmental variable") do
            options[:secret] = nil
          end

          parser.on("--timeout TIMEOUT", "Request timeout") do |timeout|
            options[:timeout] = timeout.to_i
          end

          parser.on("-o", "--option NAME=VALUE", "Set option to be passed as query param.") do |option|
            key, value = option.split('=')
            options[:query][key.to_sym] = value
          end

          parser.on("--no-typhoeus", "Don't use typhoeus.") do
            Embedly.configuration.request_with :net_http
          end

          parser.separator ""
          parser.separator "Common Options:"

          parser.on("-v", "--[no-]verbose", "Run verbosely") do |verbose|
            Embedly.configuration.debug = verbose
          end

          parser.on("-h", "--help", "Display this message") do
            puts parser
            exit
          end

          parser.separator ""
          parser.separator "Bob Corsaro <bob@embed.ly>"
        end
      end
reject_nil!() click to toggle source
# File lib/embedly/command_line.rb, line 40
def reject_nil!
  options.reject! { |_, opt| opt.nil? }
end
set_urls!() click to toggle source
# File lib/embedly/command_line.rb, line 44
def set_urls!
  raise(OptionParser::InvalidOption, "url required") if @args.empty?
  options[:query][:urls] = @args
end