lib/relish/commands/base.rb in relish-0.0.5 vs lib/relish/commands/base.rb in relish-0.0.6
- old
+ new
@@ -1,55 +1,73 @@
require 'yaml'
+require 'relish/ui'
+require 'relish/options_file'
+require 'relish/commands/dsl'
module Relish
module Command
class Base
- DEFAULT_HOST = 'relishapp.com'
- GLOBAL_OPTIONS_FILE = File.join(File.expand_path('~'), '.relish')
- LOCAL_OPTIONS_FILE = '.relish'
+ extend Dsl
- attr_accessor :args
-
+ attr_writer :args
+ attr_reader :cli_options
+
def initialize(args = [])
- @args = args
+ @args = clean_args(args)
@param = get_param
- @options = get_options
+ @cli_options = Hash[*@args]
end
- def organization
- @options['--organization'] || @options['-o'] || parsed_options_file['organization']
+ def url
+ "http://#{host}/api"
end
- def project
- @options['--project'] || @options['-p'] || parsed_options_file['project']
+ def get_param
+ @args.shift if @args.size.odd?
end
+
+ private
- def url
- "http://#{@options['--host'] || DEFAULT_HOST}/api"
- end
+ option :organization
+ option :project
+ option :api_token, :default => lambda { get_and_store_api_token }
+ option :host, :default => lambda { Relish.default_host }
- def resource
- RestClient::Resource.new(url)
+ def get_and_store_api_token
+ api_token = get_api_token
+ global_options_file.store('api_token' => api_token)
+ api_token
end
- def api_token
- parsed_options_file['api_token']
+ 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 get_param
- args.shift if args.size.odd?
+ def resource(options = {})
+ RestClient::Resource.new(url, options)
end
+
+ def clean_args(args)
+ cleaned = []
+ args.each do |arg|
+ cleaned << arg.sub('--', '')
+ end
+ cleaned
+ end
- def get_options
- parsed_options_file.merge(Hash[*args])
+ 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 parsed_options_file
- @parsed_options_file ||= {}.tap do |parsed_options|
- [GLOBAL_OPTIONS_FILE, LOCAL_OPTIONS_FILE].each do |options_file|
- parsed_options.merge!(YAML.load_file(options_file)) if File.exist?(options_file)
- end
- end
+ def ui
+ @ui ||= Ui.new
end
end
end
end
\ No newline at end of file