Skip to content

Commit

Permalink
cli fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiesiek Spyra committed Dec 1, 2015
1 parent 14cdc74 commit 32dcfb3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
12 changes: 11 additions & 1 deletion lib/td_tip/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class Cli < Thor

attr_reader :cli_options, :parameters, :response

# Main/default action
def calculate
set_cli_options
set_parameters

if parameters.valid?
@response = TdTip::Models::Response.new(parameters).get
set_response
display_response
else
display_validation_errors parameters
Expand All @@ -25,16 +26,24 @@ def calculate

private

# Set options from command line or ask user
def set_cli_options
@cli_options = options.dup
@cli_options[:amount] ||= ask('# Please provide amount:')
@cli_options[:tip] ||= ask('# Please provide tip (%):')
end

# Set parameters from options
def set_parameters
@parameters = TdTip::Models::Parameters.new cli_options
end

# Set response using parameters
def set_response
@response = TdTip::Models::Response.new(parameters).get
end

# Print response on screen
def display_response
if response.valid?
say "# Total amount with tip: #{response.amount_with_tip}" \
Expand All @@ -45,6 +54,7 @@ def display_response
end
end

# Print validation messages
def display_validation_errors(obj)
obj.errors.messages.each do |field, messages|
say "# #{field.capitalize}: #{messages.join ', '}\n"
Expand Down
22 changes: 19 additions & 3 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,28 @@

it 'should has a response' do
@cli.send(:options=, amount: amount_1, tip: tip_1)

allow(TdTip::Models::Response).to receive(:get) { {} }
resp = double(TdTip::Models::Response, amount_with_tip: amount_1, tip: tip_1, currency: 'EUR')
allow(resp).to receive(:get) { resp }
allow(resp).to receive(:valid?) { true }
allow(TdTip::Models::Response).to receive(:new) { resp }
allow(@cli).to receive(:say) { }

@cli.calculate

expect(@cli.response).to be_instance_of TdTip::Models::Response
expect(@cli.response).not_to be_nil
end

it 'should handle invalid response' do
@cli.send(:options=, amount: amount_1, tip: tip_1)
resp = double(TdTip::Models::Response, amount_with_tip: amount_1, tip: tip_1, currency: 'EUR')
allow(resp).to receive(:valid?) { false }
allow(@cli).to receive(:display_validation_errors) { 'test' }
allow(@cli).to receive(:say) { }
allow(@cli).to receive(:response) { resp }

out = @cli.send :display_response

expect(out).not_to be_nil
end
end
end
7 changes: 6 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require 'simplecov'
require 'coveralls'
require 'td_tip/config'

Coveralls.wear!
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
SimpleCov.start

RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
Expand Down

0 comments on commit 32dcfb3

Please sign in to comment.