diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..53f2ffb --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,11 @@ +--- +engines: + fixme: + enabled: true + rubocop: + enabled: true +ratings: + paths: + - "**.rb" +exclude_paths: +- spec/**/* diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..e6adc42 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,2 @@ +Style/FileName: + Enabled: false \ No newline at end of file diff --git a/README.md b/README.md index 6763449..5457ab0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![Build Status](https://travis-ci.org/wspyra/td_tip.svg)](https://travis-ci.org/wspyra/td_tip) [![Coverage Status](https://coveralls.io/repos/wspyra/td_tip/badge.svg?branch=master&service=github)](https://coveralls.io/github/wspyra/td_tip?branch=master) [![Dependency Status](https://gemnasium.com/wspyra/td_tip.svg)](https://gemnasium.com/wspyra/td_tip) +[![Code Climate](https://codeclimate.com/github/wspyra/td_tip/badges/gpa.svg)](https://codeclimate.com/github/wspyra/td_tip) T+D Code challenge solution @@ -33,12 +34,6 @@ Commands: $ calculate-tip help calculate - -## Contributing - -Bug reports and pull requests are welcome on GitHub at https://github.com/wspyra/td_tip. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct. - - ## License The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). diff --git a/lib/td_tip/models/parameters.rb b/lib/td_tip/models/parameters.rb index 9a30dcb..d32ae78 100644 --- a/lib/td_tip/models/parameters.rb +++ b/lib/td_tip/models/parameters.rb @@ -17,12 +17,14 @@ def initialize(options = {}) parse_amount options[:amount] end + # Returns parameters for Web Service def to_params { amount: amount, tip: tip } end protected + # Parses raw amount into final amount and currency def parse_amount(amount_raw) matches = TdTip::AMOUNT_CURRENCY_REGEXP.match amount_raw return unless matches diff --git a/lib/td_tip/models/response.rb b/lib/td_tip/models/response.rb index c83aaca..62eab2b 100644 --- a/lib/td_tip/models/response.rb +++ b/lib/td_tip/models/response.rb @@ -2,7 +2,7 @@ module TdTip module Models - # Response + # Response - performs request and handle response class Response include ActiveModel::Validations include HTTParty @@ -21,6 +21,7 @@ def initialize(parameters) @parameters = parameters end + # Performs request and handle response def get result = parse_and_symbolize_json @amount_with_tip = result[:amount_with_tip] @@ -32,14 +33,19 @@ def get private + # Adds additional validation def other_errors errors.add(:error, error) unless error.blank? end + # Process request response def parse_and_symbolize_json - with_error_handling { JSON.parse(calculate_request.body).symbolize_keys! } + with_error_handling do + JSON.parse(calculate_request.body).symbolize_keys! + end end + # Performs post request to Web Service def calculate_request self.class.post WS_METHOD, query: parameters.to_params, @@ -48,7 +54,7 @@ def calculate_request def with_error_handling yield - rescue => e + rescue HTTParty::Error, JSON::ParserError => e { error: e.message } end end diff --git a/lib/td_tip/version.rb b/lib/td_tip/version.rb index e26d080..d109975 100644 --- a/lib/td_tip/version.rb +++ b/lib/td_tip/version.rb @@ -1,4 +1,4 @@ # Main gem's module module TdTip - VERSION = '0.1.4' + VERSION = '0.1.5' end diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index b995abb..b3b911b 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -18,7 +18,7 @@ context 'options from command line' do before(:each) do - @cli.send(:options=, { amount: amount_1, tip: tip_1 }) + @cli.send(:options=, amount: amount_1, tip: tip_1) end it 'should use amount from command line' do @@ -35,7 +35,6 @@ end context 'input from user' do - it 'should ask for amount when no parameter' do @cli.send :set_cli_options @@ -51,7 +50,7 @@ context 'parameters' do it 'should set parameters from options' do - @cli.send(:options=, { amount: amount_1, tip: tip_1 }) + @cli.send(:options=, amount: amount_1, tip: tip_1) @cli.send :set_cli_options @cli.send :set_parameters @@ -61,7 +60,7 @@ context 'calculation' do it 'should display parameters validation errors' do - @cli.send(:options=, { amount: amount_2, tip: tip_1 }) + @cli.send(:options=, amount: amount_2, tip: tip_1) allow(@cli).to receive(:display_validation_errors) { error_1 } @@ -69,7 +68,7 @@ end it 'should has a response' do - @cli.send(:options=, { amount: amount_1, tip: tip_1 }) + @cli.send(:options=, amount: amount_1, tip: tip_1) allow(TdTip::Models::Response).to receive(:get) { {} } diff --git a/spec/response_spec.rb b/spec/response_spec.rb index fd41da6..2a0c111 100644 --- a/spec/response_spec.rb +++ b/spec/response_spec.rb @@ -21,13 +21,19 @@ end it 'should parse valid json' do - allow(@response).to receive(:calculate_request) { OpenStruct.new body: json_2 } + allow(@response).to receive(:calculate_request) { + HTTParty::Response.new OpenStruct.new(options: {}), + OpenStruct.new(body: json_2), + -> {} + } expect(@response.send :parse_and_symbolize_json).to eq(result_1) end it 'should handle exceptions' do - allow(@response).to receive(:calculate_request) { fail 'Test' } + allow(@response).to receive(:calculate_request) { + fail HTTParty::Error, 'Test' + } expect(@response.send :parse_and_symbolize_json).to eq(error: 'Test') end @@ -35,7 +41,9 @@ context 'model' do it 'should add exceptions to validations' do - allow(@response).to receive(:calculate_request) { fail 'Test' } + allow(@response).to receive(:calculate_request) { + fail HTTParty::Error, 'Test' + } @response.get @response.valid?