temperature_converter_bl 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23067772d2714f92842211ac1c48a3d66f1d4f69
4
+ data.tar.gz: eb40fe9379d79d9f336be88a8468703db00299ac
5
+ SHA512:
6
+ metadata.gz: a821c35880091e98326ae0aa1c5260931c16f931624704297395a532a9719fac0b442e72742c448c320174c05079698ebaeeb1aa1c8959714677a015ae22defd
7
+ data.tar.gz: 0bbe968259e1c0d60835bd183cde06fc43015280c3d348595a12d443668b699b33bacdaacbfb81de9ca4f559b42b6c1587e5cf290eae7cb3768a69235594365e
@@ -0,0 +1,5 @@
1
+ class AbstractMethodError < StandardError
2
+ def initialize(msg='Must implement this method.')
3
+ super
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class InvalidTemperatureError < StandardError
2
+ def initialize(msg='Temperature must be numeric.')
3
+ super
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class ScaleNotSupportedError < StandardError
2
+ def initialize(msg='This scale is currently not supported.')
3
+ super
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class StrategyNotSupportedError < StandardError
2
+ def initialize(msg='This strategy is currently not supported.')
3
+ super
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ require 'temperature_converter/strategy_interface'
2
+ class CelsiusStrategy < StrategyInterface
3
+
4
+ def convert_temperature(convert_to, temperature)
5
+ case convert_to
6
+ when 'fahrenheit'
7
+ temperature * 9.0 / 5.0 + 32
8
+ when 'kelvin'
9
+ temperature + 273.15
10
+ when 'celsius'
11
+ temperature
12
+ else
13
+ raise ScaleNotSupportedError
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,19 @@
1
+ require 'temperature_converter/strategy_interface'
2
+
3
+ class FahrenheitStrategy < StrategyInterface
4
+
5
+ def convert_temperature(convert_to, temperature)
6
+ base_calculation = (temperature - 32) * 5.0 / 9.0
7
+ case convert_to
8
+ when 'celsius'
9
+ base_calculation
10
+ when 'kelvin'
11
+ base_calculation + 273.15
12
+ when 'fahrenheit'
13
+ temperature
14
+ else
15
+ raise ScaleNotSupportedError
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'temperature_converter/strategy_interface'
2
+ class KelvinStrategy < StrategyInterface
3
+
4
+ def convert_temperature(convert_to, temperature)
5
+ case convert_to
6
+ when 'celsius'
7
+ temperature - 273.15
8
+ when 'fahrenheit'
9
+ temperature * 9/5 - 459.67
10
+ when 'kelvin'
11
+ temperature
12
+ else
13
+ raise ScaleNotSupportedError
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'custom_exceptions/scale_exception'
2
+ require 'custom_exceptions/abstract_method_exception'
3
+ class StrategyInterface
4
+
5
+ #In ruby we really don't need this interface at all. Ruby will throw
6
+ #a NoMethod error if a method doesn't exist which is essentially what
7
+ #I am doing here. For sake of sticking to the GOF implementation I will
8
+ #use this class and method as an interface.
9
+ def convert_temperature(convert_to, temperature)
10
+ raise AbstractMethodError
11
+ end
12
+
13
+ end
@@ -0,0 +1,18 @@
1
+ require 'custom_exceptions/strategy_exception'
2
+ require 'custom_exceptions/invalid_temperature_exception'
3
+ class TemperatureConverter
4
+ attr_reader :convert_strategy
5
+ ACCETPTED_STRATEGY = ['celsius', 'fahrenheit', 'kelvin'].map(&:freeze).freeze
6
+
7
+ def initialize(convert_from)
8
+ raise StrategyNotSupportedError unless ACCETPTED_STRATEGY.include?(convert_from)
9
+ @convert_strategy = Object.const_get("#{convert_from.capitalize}Strategy").new
10
+ end
11
+
12
+ def convert_temperature(convert_to, temperature)
13
+ raise InvalidTemperatureError unless temperature.to_s.match(/\A[-+]?\d*\.?\d+\z/)
14
+ @convert_strategy.convert_temperature(convert_to, temperature.to_f)
15
+ end
16
+ end
17
+
18
+ Gem.find_files("temperature_converter/*.rb").each { |path| require path }
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: temperature_converter_bl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Ludwig, Jr.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A temperature converter that takes a temperature from one scale and converts
14
+ it to another.
15
+ email: bsludwig@svsu.edu
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/custom_exceptions/abstract_method_exception.rb
21
+ - lib/custom_exceptions/invalid_temperature_exception.rb
22
+ - lib/custom_exceptions/scale_exception.rb
23
+ - lib/custom_exceptions/strategy_exception.rb
24
+ - lib/temperature_converter/celsius_strategy.rb
25
+ - lib/temperature_converter/fahrenheit_strategy.rb
26
+ - lib/temperature_converter/kelvin_strategy.rb
27
+ - lib/temperature_converter/strategy_interface.rb
28
+ - lib/temperature_converter_bl.rb
29
+ homepage: http://rubygems.org/gems/temperature_converter_bl
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ source_code_uri: https://github.com/hcbl1212/temperature_converter
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.4.8
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Converts temperatures from one scale to another.
54
+ test_files: []