Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiesiek Spyra committed Dec 1, 2015
1 parent 3687e13 commit 14cdc74
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
engines:
fixme:
enabled: true
rubocop:
enabled: true
ratings:
paths:
- "**.rb"
exclude_paths:
- spec/**/*
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Style/FileName:
Enabled: false
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions lib/td_tip/models/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/td_tip/models/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module TdTip
module Models
# Response
# Response - performs request and handle response
class Response
include ActiveModel::Validations
include HTTParty
Expand All @@ -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]
Expand All @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/td_tip/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Main gem's module
module TdTip
VERSION = '0.1.4'
VERSION = '0.1.5'
end
9 changes: 4 additions & 5 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +35,6 @@
end

context 'input from user' do

it 'should ask for amount when no parameter' do
@cli.send :set_cli_options

Expand All @@ -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

Expand All @@ -61,15 +60,15 @@

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 }

expect(@cli.calculate).to eq(error_1)
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) { {} }

Expand Down
14 changes: 11 additions & 3 deletions spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,29 @@
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
end

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?
Expand Down

0 comments on commit 14cdc74

Please sign in to comment.