td_tip 0.1.1
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 +7 -0
- data/.gitignore +3 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/bin/calculate-tip +5 -0
- data/lib/td_tip/cli.rb +54 -0
- data/lib/td_tip/config.rb +9 -0
- data/lib/td_tip/models/parameters.rb +34 -0
- data/lib/td_tip/models/response.rb +56 -0
- data/lib/td_tip/version.rb +4 -0
- data/lib/td_tip.rb +7 -0
- data/td_tip.gemspec +32 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 65f986dfa7ae51d5a449eebfe1c32c33f8ad5665
|
4
|
+
data.tar.gz: e3cebf77086a426483c5ea92c5618747fe02162d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 247587c85edd7950526e96d8e9394d85de55420b81681b005fd6b28858238696e920e14dd5bd226bcc003e4762976bab5afb6259d50b4f44cd8373f04728ead2
|
7
|
+
data.tar.gz: 8af604af23c14e18540df933b34cd2e3305d99b9cf792331f2d5e78f95f321192a4eb07bf1be9cf94a82df2e0aa160af5bcc5d56a68a26e6bf1e9cd4df3f9d6e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Wiesław Spyra
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# TdTip
|
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.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'td_tip'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install td_tip
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
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.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/calculate-tip
ADDED
data/lib/td_tip/cli.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module TdTip
|
2
|
+
# Command line interface
|
3
|
+
class Cli < Thor
|
4
|
+
default_task :calculate
|
5
|
+
|
6
|
+
desc 'calculate', 'Calculate tip amount'
|
7
|
+
long_desc '`calculate` will print out total amount and tip value itself.'
|
8
|
+
|
9
|
+
method_options amount: :string,
|
10
|
+
tip: :numeric
|
11
|
+
|
12
|
+
attr_reader :cli_options, :parameters, :response
|
13
|
+
|
14
|
+
def calculate
|
15
|
+
set_cli_options
|
16
|
+
set_parameters
|
17
|
+
|
18
|
+
if parameters.valid?
|
19
|
+
@response = TdTip::Models::Response.new(parameters).get
|
20
|
+
display_response
|
21
|
+
else
|
22
|
+
display_validation_errors parameters
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def set_cli_options
|
29
|
+
@cli_options = options.dup
|
30
|
+
@cli_options[:amount] ||= ask('# Please provide amount:')
|
31
|
+
@cli_options[:tip] ||= ask('# Please provide tip (%):')
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_parameters
|
35
|
+
@parameters = TdTip::Models::Parameters.new cli_options
|
36
|
+
end
|
37
|
+
|
38
|
+
def display_response
|
39
|
+
if response.valid?
|
40
|
+
say "# Total amount with tip: #{response.amount_with_tip}" \
|
41
|
+
" #{response.currency}\n"
|
42
|
+
say "# Tip amount: #{response.tip} #{response.currency}\n"
|
43
|
+
else
|
44
|
+
display_validation_errors response
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def display_validation_errors(obj)
|
49
|
+
obj.errors.messages.each do |field, messages|
|
50
|
+
say "# #{field.capitalize}: #{messages.join ', '}\n"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module TdTip
|
4
|
+
module Models
|
5
|
+
# Storing and processing parameters
|
6
|
+
class Parameters
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
attr_reader :tip, :currency, :amount
|
10
|
+
|
11
|
+
validates_presence_of :amount, :tip
|
12
|
+
validates_numericality_of :tip, greater_than_or_equal_to: 0
|
13
|
+
validates_numericality_of :amount, greater_than: 0
|
14
|
+
|
15
|
+
def initialize(options = {})
|
16
|
+
@tip = options[:tip].blank? ? DEFAULT_TIP : options[:tip]
|
17
|
+
parse_amount options[:amount]
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_params
|
21
|
+
{ amount: amount, tip: tip }
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def parse_amount(amount_raw)
|
27
|
+
matches = TdTip::AMOUNT_CURRENCY_REGEXP.match amount_raw
|
28
|
+
return unless matches
|
29
|
+
@amount = matches.captures[0].sub(',', '.').to_f
|
30
|
+
@currency = matches.captures[1].to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
module TdTip
|
4
|
+
module Models
|
5
|
+
# Response
|
6
|
+
class Response
|
7
|
+
include ActiveModel::Validations
|
8
|
+
include HTTParty
|
9
|
+
|
10
|
+
base_uri TdTip::WS_URL
|
11
|
+
|
12
|
+
attr_accessor :parameters
|
13
|
+
attr_reader :amount_with_tip, :tip, :currency, :error
|
14
|
+
|
15
|
+
validates_presence_of :amount_with_tip, :tip
|
16
|
+
validates_numericality_of :tip, greater_than_or_equal_to: 0
|
17
|
+
validates_numericality_of :amount_with_tip, greater_than: 0
|
18
|
+
validate :other_errors
|
19
|
+
|
20
|
+
def initialize(parameters)
|
21
|
+
@parameters = parameters
|
22
|
+
end
|
23
|
+
|
24
|
+
def get
|
25
|
+
result = parse_and_symbolize_json
|
26
|
+
@amount_with_tip = result[:amount_with_tip]
|
27
|
+
@tip = result[:tip]
|
28
|
+
@error = result[:error]
|
29
|
+
@currency = parameters.currency
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def other_errors
|
36
|
+
errors.add(:error, error) unless error.blank?
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_and_symbolize_json
|
40
|
+
with_error_handling { JSON.parse(calculate_request).symbolize_keys! }
|
41
|
+
end
|
42
|
+
|
43
|
+
def calculate_request
|
44
|
+
self.class.post WS_METHOD,
|
45
|
+
query: parameters.to_params,
|
46
|
+
timeout: WS_TIMEOUT
|
47
|
+
end
|
48
|
+
|
49
|
+
def with_error_handling
|
50
|
+
yield
|
51
|
+
rescue => e
|
52
|
+
{ error: e.message }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/td_tip.rb
ADDED
data/td_tip.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'td_tip/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'td_tip'
|
8
|
+
spec.version = TdTip::VERSION
|
9
|
+
spec.authors = ['Wiesław Spyra']
|
10
|
+
spec.email = ['wmms@o2.pl']
|
11
|
+
|
12
|
+
spec.summary = 'T+D Code challenge'
|
13
|
+
spec.description = 'T+D Code challenge solution'
|
14
|
+
spec.homepage = 'https://github.com/wspyra/td_tip'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.4'
|
26
|
+
spec.add_development_dependency 'simplecov', '~> 0.10'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.35.1'
|
28
|
+
|
29
|
+
spec.add_dependency 'thor', '~> 0.19.1'
|
30
|
+
spec.add_dependency 'httparty', '~> 0.13.7'
|
31
|
+
spec.add_dependency 'activemodel', '~> 4.2'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: td_tip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wiesław Spyra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.35.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.35.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: thor
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.19.1
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.19.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: httparty
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.13.7
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.13.7
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activemodel
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.2'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.2'
|
125
|
+
description: T+D Code challenge solution
|
126
|
+
email:
|
127
|
+
- wmms@o2.pl
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- bin/calculate-tip
|
140
|
+
- lib/td_tip.rb
|
141
|
+
- lib/td_tip/cli.rb
|
142
|
+
- lib/td_tip/config.rb
|
143
|
+
- lib/td_tip/models/parameters.rb
|
144
|
+
- lib/td_tip/models/response.rb
|
145
|
+
- lib/td_tip/version.rb
|
146
|
+
- td_tip.gemspec
|
147
|
+
homepage: https://github.com/wspyra/td_tip
|
148
|
+
licenses:
|
149
|
+
- MIT
|
150
|
+
metadata: {}
|
151
|
+
post_install_message:
|
152
|
+
rdoc_options: []
|
153
|
+
require_paths:
|
154
|
+
- lib
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
requirements: []
|
166
|
+
rubyforge_project:
|
167
|
+
rubygems_version: 2.4.8
|
168
|
+
signing_key:
|
169
|
+
specification_version: 4
|
170
|
+
summary: T+D Code challenge
|
171
|
+
test_files: []
|