td_tip 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6202e736e2806b13bbe320c9a7b43d52fd70d7c
4
- data.tar.gz: 0a0d8e6f5e8ba6e570856b28bcb11eeb5eb1b001
3
+ metadata.gz: dcb4ecc27a9584d91fe993eac68c8efbe065f102
4
+ data.tar.gz: d7434cd3c289cb43207969b4ed3b40c33aad6cbf
5
5
  SHA512:
6
- metadata.gz: dfadc8f83b7458d2d8145790845c9eaa12638144338589ae4b69dcec62459719479ac2a0e295fe5c1ef651860a2efc710e825896630c3f686f559c48b4ac5abc
7
- data.tar.gz: 0743ee2619f366acd3da49b8e41ddc1bf9ae78ec92ab954a3d063c1b3e8d4df04f5be3ef2b1677c971abb7a8313ae25287bb9e090ad46639793412192440f0ef
6
+ metadata.gz: a45f5d132b756d79e1de561fa707c1f7d47f4ad4fbb1d546b946660d4cc49896f4e85c5c5fd8da043c15cce33151b9e594a4e2f1bd84292127638edf85f4300a
7
+ data.tar.gz: 0bee6a80beb4aac1ce25418fefebad8fbfeafa8666460ad3fbdd81de2ae4a1dba977853d5fce006307aa143b436286e68a5d476fa8105a7b391fa22a02155e7d
data/.codeclimate.yml ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ engines:
3
+ fixme:
4
+ enabled: false
5
+ rubocop:
6
+ enabled: true
7
+ ratings:
8
+ paths:
9
+ - "**.rb"
10
+ exclude_paths:
11
+ - spec/**/*
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Style/FileName:
2
+ Enabled: false
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # TdTip
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/td_tip`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/td_tip.svg)](https://badge.fury.io/rb/td_tip)
4
+ [![Build Status](https://travis-ci.org/wspyra/td_tip.svg)](https://travis-ci.org/wspyra/td_tip)
5
+ [![Coverage Status](https://coveralls.io/repos/wspyra/td_tip/badge.svg?branch=master&service=github)](https://coveralls.io/github/wspyra/td_tip?branch=master)
6
+ [![Dependency Status](https://gemnasium.com/wspyra/td_tip.svg)](https://gemnasium.com/wspyra/td_tip)
7
+ [![Code Climate](https://codeclimate.com/github/wspyra/td_tip/badges/gpa.svg)](https://codeclimate.com/github/wspyra/td_tip)
4
8
 
5
- TODO: Delete this and the text above, and describe your gem
9
+ T+D Code challenge solution
6
10
 
7
11
  ## Installation
8
12
 
@@ -22,18 +26,13 @@ Or install it yourself as:
22
26
 
23
27
  ## Usage
24
28
 
25
- TODO: Write usage instructions here
29
+ Commands:
26
30
 
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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.
31
+ $ calculate-tip
32
+
33
+ $ calculate-tip --amount AMOUNT --tip NUMBER
36
34
 
35
+ $ calculate-tip help calculate
37
36
 
38
37
  ## License
39
38
 
data/lib/td_tip/cli.rb CHANGED
@@ -11,12 +11,13 @@ module TdTip
11
11
 
12
12
  attr_reader :cli_options, :parameters, :response
13
13
 
14
+ # Main/default action
14
15
  def calculate
15
16
  set_cli_options
16
17
  set_parameters
17
18
 
18
19
  if parameters.valid?
19
- @response = TdTip::Models::Response.new(parameters).get
20
+ set_response
20
21
  display_response
21
22
  else
22
23
  display_validation_errors parameters
@@ -25,16 +26,24 @@ module TdTip
25
26
 
26
27
  private
27
28
 
29
+ # Set options from command line or ask user
28
30
  def set_cli_options
29
31
  @cli_options = options.dup
30
32
  @cli_options[:amount] ||= ask('# Please provide amount:')
31
33
  @cli_options[:tip] ||= ask('# Please provide tip (%):')
32
34
  end
33
35
 
36
+ # Set parameters from options
34
37
  def set_parameters
35
38
  @parameters = TdTip::Models::Parameters.new cli_options
36
39
  end
37
40
 
41
+ # Set response using parameters
42
+ def set_response
43
+ @response = TdTip::Models::Response.new(parameters).get
44
+ end
45
+
46
+ # Print response on screen
38
47
  def display_response
39
48
  if response.valid?
40
49
  say "# Total amount with tip: #{response.amount_with_tip}" \
@@ -45,6 +54,7 @@ module TdTip
45
54
  end
46
55
  end
47
56
 
57
+ # Print validation messages
48
58
  def display_validation_errors(obj)
49
59
  obj.errors.messages.each do |field, messages|
50
60
  say "# #{field.capitalize}: #{messages.join ', '}\n"
@@ -17,12 +17,14 @@ module TdTip
17
17
  parse_amount options[:amount]
18
18
  end
19
19
 
20
+ # Returns parameters for Web Service
20
21
  def to_params
21
22
  { amount: amount, tip: tip }
22
23
  end
23
24
 
24
25
  protected
25
26
 
27
+ # Parses raw amount into final amount and currency
26
28
  def parse_amount(amount_raw)
27
29
  matches = TdTip::AMOUNT_CURRENCY_REGEXP.match amount_raw
28
30
  return unless matches
@@ -2,7 +2,7 @@ require 'httparty'
2
2
 
3
3
  module TdTip
4
4
  module Models
5
- # Response
5
+ # Response - performs request and handle response
6
6
  class Response
7
7
  include ActiveModel::Validations
8
8
  include HTTParty
@@ -21,6 +21,7 @@ module TdTip
21
21
  @parameters = parameters
22
22
  end
23
23
 
24
+ # Performs request and handle response
24
25
  def get
25
26
  result = parse_and_symbolize_json
26
27
  @amount_with_tip = result[:amount_with_tip]
@@ -32,14 +33,19 @@ module TdTip
32
33
 
33
34
  private
34
35
 
36
+ # Adds additional validation
35
37
  def other_errors
36
38
  errors.add(:error, error) unless error.blank?
37
39
  end
38
40
 
41
+ # Process request response
39
42
  def parse_and_symbolize_json
40
- with_error_handling { JSON.parse(calculate_request.body).symbolize_keys! }
43
+ with_error_handling do
44
+ JSON.parse(calculate_request.body).symbolize_keys!
45
+ end
41
46
  end
42
47
 
48
+ # Performs post request to Web Service
43
49
  def calculate_request
44
50
  self.class.post WS_METHOD,
45
51
  query: parameters.to_params,
@@ -48,7 +54,7 @@ module TdTip
48
54
 
49
55
  def with_error_handling
50
56
  yield
51
- rescue => e
57
+ rescue HTTParty::Error, JSON::ParserError => e
52
58
  { error: e.message }
53
59
  end
54
60
  end
@@ -1,4 +1,4 @@
1
1
  # Main gem's module
2
2
  module TdTip
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
data/td_tip.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'bundler', '~> 1.10'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
26
26
  spec.add_development_dependency 'rspec', '~> 3.4'
27
- spec.add_development_dependency 'coveralls', '~> 0.8.10'
27
+ spec.add_development_dependency 'coveralls', '~> 0.8.9'
28
28
  spec.add_development_dependency 'rubocop', '~> 0.35.1'
29
29
 
30
30
  spec.add_dependency 'thor', '~> 0.19.1'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td_tip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wiesław Spyra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-30 00:00:00.000000000 Z
11
+ date: 2015-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.8.10
61
+ version: 0.8.9
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.8.10
68
+ version: 0.8.9
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -130,9 +130,11 @@ executables:
130
130
  extensions: []
131
131
  extra_rdoc_files: []
132
132
  files:
133
+ - ".codeclimate.yml"
133
134
  - ".coveralls.yml"
134
135
  - ".gitignore"
135
136
  - ".rspec"
137
+ - ".rubocop.yml"
136
138
  - ".travis.yml"
137
139
  - Gemfile
138
140
  - LICENSE.txt