Sha256: d34cb8f651a95a5a28000e4c504d4a9a0070aa74948bbbec7a9608ae3edbc180

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'yaml'
require 'relish/ui'
require 'relish/options_file'
require 'relish/commands/dsl'

module Relish
  module Command
    class Base
      extend Dsl
      
      option :project
      option :api_token, :default => lambda { get_and_store_api_token }
      option :host,      :default => lambda { Relish.default_host }, :display => false
      
      attr_writer :args
      attr_reader :cli_options
            
      def initialize(args = [])
        @args = clean_args(args)
        @param = get_param
        @cli_options = Hash[*@args]

        validate_cli_options
      end
      
      def url
        "http://#{host}/api"
      end
      
      def get_param
        @args.shift if @args.size.odd?
      end

    private

      def get_and_store_api_token
        api_token = get_api_token
        global_options_file.store('api_token' => api_token)
        api_token
      end
      
      def get_api_token
        email, password = ui.get_credentials
        
        raw_response = resource(:user => email, :password => password)['token'].get
        String.new(raw_response.to_s)
      end
      
      def resource(options = {})
        options[:user] ||= api_token
        options[:password] ||= 'X'
        RestClient::Resource.new(url, options)
      end
      
      def clean_args(args)
        args.inject([]) {|cleaned, arg| cleaned << arg.sub('--', '') }
      end
      
      def valid_option_names
        Dsl::Option.names
      end
      
      def validate_cli_options
        @cli_options.keys.each do |option|
          unless valid_option_names.include?(option.to_s)
            puts "#{option} is not a valid option."
            exit 1
          end
        end
      end
      
      def global_options_file
        @global_options ||= OptionsFile.new(Relish.global_options_file)
      end

      def local_options_file
        @local_options ||= OptionsFile.new(Relish.local_options_file)
      end
      
      def ui
        @ui ||= Ui.new
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
relish-0.0.9 lib/relish/commands/base.rb