rafaeldx7/ruby-bitly

View on GitHub
bin/bitly

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')

require 'rubygems'
require 'ruby-bitly'
require 'optparse'
require 'ostruct'
require 'date'

class App
  ACCOUNT_FILE_PATH = "#{ENV['HOME']}/.bitly"

  attr_accessor :arguments, :options

  def initialize(arguments)
    self.arguments = arguments
    self.options = OpenStruct.new
  end

  def run
    load_account_data
    process_options
    puts process_command
  end

  protected

    def load_account_data
      account_data = YAML.load(File.read(ACCOUNT_FILE_PATH)) rescue {}
      options.login = account_data['login']
      options.api_key = account_data['api_key']
    rescue
    end

    def process_options
      OptionParser.new do |opts|
        opts.banner = "Usage: bitly [options] URL"

        opts.on('-l', '--login LOGIN', 'You need a free Bitly login and api key. Sign up here: http://bit.ly/a/sign_up.') do |login|
          options.login = login
        end

        opts.on('-k', '--api-key KEY', 'You can find your api key here: http://bit.ly/a/your_api_key.', ' ') do |api_key|
          options.api_key = api_key
        end

        opts.on('-d', '--domain DOMAIN',
                'The short domain to use: either bit.ly, j.mp, bitly.com or a custom short domain.',
                'This option will override the default short domain selected in your Bitly account settings.', ' ') do |domain|
          options.domain = domain
        end

        opts.on('-s', '--shorten', 'Given a long URL, returns a Bitlink.') { options.command = :shorten }
        opts.on('-e', '--expand', 'Given a Bitlink, hash or custom path, returns the target (long) URL.') { options.command = :expand }
        opts.on('-u', '--user-clicks', 'The total count of clicks to this user\'s Bitlink.')   { options.command = :user_clicks }
        opts.on('-g', '--global-clicks', 'The total count of the corresponding Bitly aggregate hash.', ' ') { options.command = :global_clicks }

        opts.on('-h', '--help', 'Print this help.') { puts opts.help; exit 0 }
        opts.on('-v', '--version', 'Print version.') { output_version; exit 0 }
      end.parse!
    end

    def output_version
      puts "bitly version #{BitlyGem::VERSION} [gem ruby-bitly]"
    end

    def process_command
      case options.command
      when :shorten
        Bitly.shorten(long_url: arguments.first, login: options.login, api_key: options.api_key, domain: options.domain).short_url
      when :expand
        Bitly.expand(short_url: arguments.first, login: options.login, api_key: options.api_key).long_url
      when :user_clicks
        Bitly.get_clicks(short_url: arguments.first, login: options.login, api_key: options.api_key).user_clicks.to_s
      when :global_clicks
        Bitly.get_clicks(short_url: arguments.first, login: options.login, api_key: options.api_key).global_clicks.to_s
      else
        "No command given"
      end
    end
end

App.new(ARGV).run