r_odds 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +6 -0
  7. data/Gemfile.lock +43 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +50 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/lib/r_odds.rb +28 -0
  14. data/lib/r_odds/convert_odd.rb +40 -0
  15. data/lib/r_odds/infer_odd_format.rb +30 -0
  16. data/lib/r_odds/odd_converters/american_to_implied_probability.rb +39 -0
  17. data/lib/r_odds/odd_converters/class_factory.rb +30 -0
  18. data/lib/r_odds/odd_converters/decimal_to_implied_probability.rb +26 -0
  19. data/lib/r_odds/odd_converters/fractional_to_implied_probability.rb +27 -0
  20. data/lib/r_odds/odd_converters/implied_probability_to_american.rb +37 -0
  21. data/lib/r_odds/odd_converters/implied_probability_to_decimal.rb +23 -0
  22. data/lib/r_odds/odd_converters/implied_probability_to_fractional.rb +24 -0
  23. data/lib/r_odds/odd_converters/implied_probability_to_implied_probability.rb +21 -0
  24. data/lib/r_odds/odd_standardizers/american.rb +23 -0
  25. data/lib/r_odds/odd_standardizers/class_factory.rb +32 -0
  26. data/lib/r_odds/odd_standardizers/decimal.rb +23 -0
  27. data/lib/r_odds/odd_standardizers/fractional.rb +33 -0
  28. data/lib/r_odds/odd_standardizers/implied_probability.rb +43 -0
  29. data/lib/r_odds/odds/american.rb +27 -0
  30. data/lib/r_odds/odds/base.rb +13 -0
  31. data/lib/r_odds/odds/decimal.rb +30 -0
  32. data/lib/r_odds/odds/fractional.rb +24 -0
  33. data/lib/r_odds/odds/implied_probability.rb +32 -0
  34. data/lib/r_odds/standardizer.rb +24 -0
  35. data/lib/r_odds/version.rb +3 -0
  36. data/r_odds.gemspec +37 -0
  37. metadata +135 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2ca0bcfc66295acd8241062d6e4d9cf69189da75ec32eed54ab57363fb54062e
4
+ data.tar.gz: f3c80f76b9489ad53748cd1cc3371c4647373d1c09c09634624bea8525181d8b
5
+ SHA512:
6
+ metadata.gz: c603d57b51ebeb2b402b10c9af7dc4de309725069da4b054ba06efe0cbaf9e60cc83a4ad35b5f8568c9e1075a0f6c5afd74c5f5067985fb9584e886eb9e879d3
7
+ data.tar.gz: c45c8321c58a1db2c4170a66800aea2a07e6f51257b9ec8e28ecab8cfbc9b54c9b866ebca61bc1a8c5171668761a71d0fe4fa747945362c35bf8b5dad52f3d7c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at georgewambold@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in r_odds.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ r_odds (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.8.2)
12
+ pry (0.10.4)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.8.1)
15
+ slop (~> 3.4)
16
+ rake (10.5.0)
17
+ rspec (3.7.0)
18
+ rspec-core (~> 3.7.0)
19
+ rspec-expectations (~> 3.7.0)
20
+ rspec-mocks (~> 3.7.0)
21
+ rspec-core (3.7.1)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-expectations (3.7.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.7.0)
26
+ rspec-mocks (3.7.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.7.0)
29
+ rspec-support (3.7.1)
30
+ slop (3.6.0)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler (~> 1.16)
37
+ pry (~> 0.10.3)
38
+ r_odds!
39
+ rake (~> 10.0)
40
+ rspec (~> 3.7)
41
+
42
+ BUNDLED WITH
43
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 George Wambold
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,50 @@
1
+ # Installing r_odds
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ ```ruby
6
+ gem 'r_odds'
7
+ ```
8
+ or
9
+ ```
10
+ $ gem install r_odds
11
+ ```
12
+ then
13
+ ```
14
+ require 'r_odds'
15
+ ```
16
+
17
+ ## How to use the gem
18
+ Use the `:convert` method to convert between any of the following formats: `:american`, `:decimal`, `:fractional` or `:implied probability`
19
+
20
+ ```ruby
21
+ ROdds.convert('+150', to: :decimal)
22
+ #=> 2.50
23
+ ```
24
+
25
+ ```ruby
26
+ ROdds.convert('25%', to: :american)
27
+ #=> +300
28
+ ```
29
+ You can also specify the format you're passing in for ambiguous formats like `'1/2'` (which could be an implied probability or a fractional odd).
30
+
31
+ ```ruby
32
+ ROdds.convert('1/4', to: :decimal)
33
+ #=> 1.25
34
+
35
+ ROdds.convert('1/4', from: :probability, to: :american)
36
+ #=> +300
37
+ ```
38
+
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/georgewambold/r_odds. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
47
+
48
+ ## Code of Conduct
49
+
50
+ Everyone interacting in the ROdds project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/r_odds/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "r_odds"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/r_odds.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "r_odds/version"
2
+ require 'r_odds/convert_odd'
3
+ require 'r_odds/infer_odd_format'
4
+ require 'r_odds/odd_converters/american_to_implied_probability'
5
+ require 'r_odds/odd_converters/class_factory'
6
+ require 'r_odds/odd_converters/decimal_to_implied_probability'
7
+ require 'r_odds/odd_converters/fractional_to_implied_probability'
8
+ require 'r_odds/odd_converters/implied_probability_to_american'
9
+ require 'r_odds/odd_converters/implied_probability_to_decimal'
10
+ require 'r_odds/odd_converters/implied_probability_to_fractional'
11
+ require 'r_odds/odd_converters/implied_probability_to_implied_probability'
12
+ require 'r_odds/odd_standardizers/american'
13
+ require 'r_odds/odd_standardizers/class_factory'
14
+ require 'r_odds/odd_standardizers/decimal'
15
+ require 'r_odds/odd_standardizers/fractional'
16
+ require 'r_odds/odd_standardizers/implied_probability'
17
+ require 'r_odds/odds/base'
18
+ require 'r_odds/odds/american'
19
+ require 'r_odds/odds/decimal'
20
+ require 'r_odds/odds/fractional'
21
+ require 'r_odds/odds/implied_probability'
22
+ require 'r_odds/standardizer'
23
+
24
+ module ROdds
25
+ def self.convert(odd, from: nil, to:)
26
+ ROdds::ConvertOdd.call(raw_odd: odd, from: from, to: to)
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ module ROdds
2
+ class ConvertOdd
3
+ DEFAULT_FORMAT = :implied_probability
4
+
5
+ def self.call(params)
6
+ new(params).call
7
+ end
8
+
9
+ def initialize(params)
10
+ @raw_odd = params[:raw_odd]
11
+ @from = params[:from] || infered_format
12
+ @to = params[:to]
13
+ end
14
+
15
+ def call
16
+ standardized_odd = ROdds::Odd::Standardizer.call(odd: raw_odd, format: from)
17
+
18
+ implied_probability = default_conversion_class.call(standardized_odd)
19
+
20
+ converted_odd = final_conversion_class.call(implied_probability)
21
+
22
+ converted_odd.to_s
23
+ end
24
+
25
+ private
26
+ attr_reader :raw_odd, :from, :to
27
+
28
+ def infered_format
29
+ ROdds::InferOddFormat.of(odd: raw_odd)
30
+ end
31
+
32
+ def default_conversion_class
33
+ ROdds::OddConverter::ClassFactory.call(from: from, to: DEFAULT_FORMAT)
34
+ end
35
+
36
+ def final_conversion_class
37
+ ROdds::OddConverter::ClassFactory.call(from: DEFAULT_FORMAT, to: to)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ module ROdds
2
+ class InferOddFormat
3
+
4
+ def self.of(params)
5
+ new(params).call
6
+ end
7
+
8
+ def initialize(params)
9
+ @odd = params[:odd]
10
+ end
11
+
12
+ def call
13
+ case odd
14
+ when /^(?:[1-9]\d*)+(\/|:)(?:[1-9]\d*)+$/
15
+ :fractional
16
+ when /^[+-][1-9]\d{2,}\.?\d*$/
17
+ :american
18
+ when /\d*.?\d++%/
19
+ :implied_probability
20
+ when /^[1-9]\d*(\.\d+)$/
21
+ :decimal
22
+ else
23
+ raise ArgumentError, "Odd given in unknown format. See docs for acceptable formats"
24
+ end
25
+ end
26
+
27
+ private
28
+ attr_reader :odd
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class AmericanToImpliedProbability
4
+
5
+ def self.call(american_odd)
6
+ new(american_odd).call
7
+ end
8
+
9
+ def initialize(american_odd)
10
+ @american_odd = american_odd
11
+ end
12
+
13
+ def call
14
+ if american_odd.negative?
15
+ derive_implied_probability_from_negative_american_odd
16
+ else
17
+ derive_implied_probability_from_positive_american_odd
18
+ end
19
+ end
20
+
21
+ private
22
+ attr_reader :american_odd
23
+
24
+ def derive_implied_probability_from_negative_american_odd
25
+ numerator = american_odd.value.abs
26
+ denominator = (american_odd.value.abs + 100)
27
+
28
+ ROdds::Odd::ImpliedProbability.new(numerator: numerator, denominator: denominator)
29
+ end
30
+
31
+ def derive_implied_probability_from_positive_american_odd
32
+ numerator = 100
33
+ denominator = (american_odd.value.abs + 100)
34
+
35
+ ROdds::Odd::ImpliedProbability.new(numerator: numerator, denominator: denominator)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class ClassFactory
4
+ def self.call(params)
5
+ new(params).call
6
+ end
7
+
8
+ def initialize(params)
9
+ @from = params[:from]
10
+ @to = params[:to]
11
+ end
12
+
13
+ def call
14
+ begin
15
+ Object.const_get("ROdds::OddConverter::#{capitalize(from)}To#{capitalize(to)}")
16
+ rescue NameError
17
+ raise ArgumentError,
18
+ "No known conversion class to get from format :#{from}, to format :#{to}"
19
+ end
20
+ end
21
+
22
+ private
23
+ attr_reader :from, :to
24
+
25
+ def capitalize(symbol)
26
+ symbol.to_s.split('_').map(&:capitalize).join
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class DecimalToImpliedProbability
4
+
5
+ def self.call(decimal_odd)
6
+ new(decimal_odd).call
7
+ end
8
+
9
+ def initialize(decimal_odd)
10
+ @decimal_odd = decimal_odd
11
+ end
12
+
13
+ def call
14
+ rational = 1 / decimal_odd.as_rational
15
+
16
+ ROdds::Odd::ImpliedProbability.new(
17
+ numerator: rational.numerator,
18
+ denominator: rational.denominator
19
+ )
20
+ end
21
+
22
+ private
23
+ attr_reader :decimal_odd
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class FractionalToImpliedProbability
4
+
5
+ def self.call(fractional_odd)
6
+ new(fractional_odd).call
7
+ end
8
+
9
+ def initialize(fractional_odd)
10
+ @fractional_odd = fractional_odd
11
+ end
12
+
13
+ def call
14
+ numerator_for_implied_prob = fractional_odd.denominator
15
+ denominator_for_implied_prob = fractional_odd.denominator + fractional_odd.numerator
16
+
17
+ ROdds::Odd::ImpliedProbability.new(
18
+ numerator: numerator_for_implied_prob,
19
+ denominator: denominator_for_implied_prob
20
+ )
21
+ end
22
+
23
+ private
24
+ attr_reader :fractional_odd
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class ImpliedProbabilityToAmerican
4
+
5
+ def self.call(implied_probability)
6
+ new(implied_probability).call
7
+ end
8
+
9
+ def initialize(implied_probability)
10
+ @implied_probability = implied_probability
11
+ end
12
+
13
+ def call
14
+ if implied_probability.greater_than_50_percent?
15
+ convert_to_negative_american_odd
16
+ else
17
+ convert_to_positive_american_odd
18
+ end
19
+ end
20
+
21
+ private
22
+ attr_reader :implied_probability
23
+
24
+ def convert_to_negative_american_odd
25
+ value = (100 * implied_probability.fraction) / (1 - implied_probability.fraction)
26
+
27
+ ROdds::Odd::American.new(value: -value)
28
+ end
29
+
30
+ def convert_to_positive_american_odd
31
+ value = ((100 * implied_probability.fraction) - 100) / -(implied_probability.fraction)
32
+
33
+ ROdds::Odd::American.new(value: value)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class ImpliedProbabilityToDecimal
4
+
5
+ def self.call(implied_probability)
6
+ new(implied_probability).call
7
+ end
8
+
9
+ def initialize(implied_probability)
10
+ @implied_probability = implied_probability
11
+ end
12
+
13
+ def call
14
+ value = (1 / implied_probability.fraction)
15
+
16
+ ROdds::Odd::Decimal.new(value: value)
17
+ end
18
+
19
+ private
20
+ attr_reader :implied_probability
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class ROdds::OddConverter::ImpliedProbabilityToFractional
4
+
5
+ def self.call(implied_probability)
6
+ new(implied_probability).call
7
+ end
8
+
9
+ def initialize(implied_probability)
10
+ @implied_probability = implied_probability
11
+ end
12
+
13
+ def call
14
+ fractional_numerator = (implied_probability.denominator - implied_probability.numerator)
15
+ fractional_denominator = implied_probability.numerator
16
+
17
+ ROdds::Odd::Fractional.new(numerator: fractional_numerator, denominator: fractional_denominator)
18
+ end
19
+
20
+ private
21
+ attr_reader :implied_probability
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module ROdds
2
+ module OddConverter
3
+ class ImpliedProbabilityToImpliedProbability
4
+
5
+ def self.call(implied_probability)
6
+ new(implied_probability).call
7
+ end
8
+
9
+ def initialize(implied_probability)
10
+ @implied_probability = implied_probability
11
+ end
12
+
13
+ def call
14
+ implied_probability
15
+ end
16
+
17
+ private
18
+ attr_reader :implied_probability
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module ROdds
2
+ module OddStandardizer
3
+ class American
4
+
5
+ def self.call(params)
6
+ new(params).call
7
+ end
8
+
9
+ def initialize(params)
10
+ @odd = params[:odd]
11
+ end
12
+
13
+ def call
14
+ value = Rational(odd)
15
+
16
+ ROdds::Odd::American.new(value: value)
17
+ end
18
+
19
+ private
20
+ attr_reader :odd
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module ROdds
2
+ module OddStandardizer
3
+ class ClassFactory
4
+
5
+ def self.for(params)
6
+ new(params).call
7
+ end
8
+
9
+ def initialize(params)
10
+ @format = params[:format]
11
+ end
12
+
13
+ def call
14
+ case format
15
+ when :fractional
16
+ ROdds::OddStandardizer::Fractional
17
+ when :american
18
+ ROdds::OddStandardizer::American
19
+ when :implied_probability
20
+ ROdds::OddStandardizer::ImpliedProbability
21
+ when :decimal
22
+ ROdds::OddStandardizer::Decimal
23
+ else
24
+ raise ArgumentError, "No implimented standardizer for odd format :#{format}"
25
+ end
26
+ end
27
+
28
+ private
29
+ attr_reader :format
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ module ROdds
2
+ module OddStandardizer
3
+ class Decimal
4
+
5
+ def self.call(params)
6
+ new(params).call
7
+ end
8
+
9
+ def initialize(params)
10
+ @odd = params[:odd]
11
+ end
12
+
13
+ def call
14
+ value = Rational(odd)
15
+
16
+ ROdds::Odd::Decimal.new(value: value)
17
+ end
18
+
19
+ private
20
+ attr_reader :odd
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module ROdds
2
+ module OddStandardizer
3
+ class Fractional
4
+ NUMERATOR_AND_DENOMINATOR = /(?<numerator>\d+)(\/|-to-| to |to|)(?<denominator>\d+)/
5
+
6
+ def self.call(params)
7
+ new(params).call
8
+ end
9
+
10
+ def initialize(params)
11
+ @raw_odd = params[:odd]
12
+ end
13
+
14
+ def call
15
+ fraction = NUMERATOR_AND_DENOMINATOR.match(raw_odd)
16
+
17
+ reduced_fraction = reduce(fraction)
18
+
19
+ ROdds::Odd::Fractional.new(
20
+ numerator: reduced_fraction.numerator,
21
+ denominator: reduced_fraction.denominator
22
+ )
23
+ end
24
+
25
+ private
26
+ attr_reader :raw_odd
27
+
28
+ def reduce(fraction)
29
+ Rational(fraction[:numerator], fraction[:denominator])
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,43 @@
1
+ module ROdds
2
+ module OddStandardizer
3
+ class ImpliedProbability
4
+
5
+ def self.call(params)
6
+ new(params).call
7
+ end
8
+
9
+ def initialize(params)
10
+ @odd = params[:odd]
11
+ end
12
+
13
+ def call
14
+ if odd_is_a_probability_fraction
15
+ rational_odd = odd.to_r
16
+
17
+ ROdds::Odd::ImpliedProbability.new(
18
+ numerator: rational_odd.numerator,
19
+ denominator: rational_odd.denominator
20
+ )
21
+ elsif odd_is_a_percentage
22
+ rational_odd = odd.to_r
23
+
24
+ ROdds::Odd::ImpliedProbability.new(
25
+ numerator: rational_odd,
26
+ denominator: Rational(100)
27
+ )
28
+ end
29
+ end
30
+
31
+ private
32
+ attr_reader :odd
33
+
34
+ def odd_is_a_probability_fraction
35
+ /\d+\/\d+/.match?(odd)
36
+ end
37
+
38
+ def odd_is_a_percentage
39
+ /\d*.?\d++%/.match?(odd)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ module ROdds
2
+ module Odd
3
+ class American < ROdds::Odd::Base
4
+ attr_reader :value
5
+
6
+ def initialize(value:)
7
+ if value.kind_of? Rational
8
+ @value = value
9
+ else
10
+ raise ArgumentError, "ROdds::Odd::American must be initialized with a Rational value. Given a #{value.class} instance"
11
+ end
12
+ end
13
+
14
+ def format
15
+ :american
16
+ end
17
+
18
+ def to_s
19
+ "%+d" % value
20
+ end
21
+
22
+ def negative?
23
+ value.negative?
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ module ROdds
2
+ module Odd
3
+ class Base
4
+ def format
5
+ raise NotImplementedError, "Subclasses must impliment this themselves"
6
+ end
7
+
8
+ def to_s
9
+ raise NotImplementedError, "Subclasses must impliment this themselves"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module ROdds
2
+ module Odd
3
+ class Decimal < ROdds::Odd::Base
4
+
5
+ def initialize(value:)
6
+ if value.kind_of? Rational
7
+ @value = value
8
+ else
9
+ raise ArgumentError, "Decimal odds must be initialized a Rational instance. Given a #{value.class} instance instead"
10
+ end
11
+ end
12
+
13
+ def format
14
+ :decimal
15
+ end
16
+
17
+ def to_s(round_to: 2)
18
+ "%.#{round_to}f" % value
19
+ end
20
+
21
+ def as_rational
22
+ value.rationalize
23
+ end
24
+
25
+ private
26
+ attr_reader :value
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,24 @@
1
+ module ROdds
2
+ module Odd
3
+ class Fractional < ROdds::Odd::Base
4
+ attr_reader :numerator, :denominator
5
+
6
+ def initialize(numerator:, denominator:)
7
+ @numerator = numerator
8
+ @denominator = denominator
9
+ @fraction = Rational(numerator, denominator)
10
+ end
11
+
12
+ def format
13
+ :fractional
14
+ end
15
+
16
+ def to_s
17
+ fraction.to_s
18
+ end
19
+
20
+ private
21
+ attr_reader :fraction
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ module ROdds
2
+ module Odd
3
+ class ImpliedProbability < ROdds::Odd::Base
4
+ FIFTY_PERCENT_AS_A_DECIMAL = 0.5
5
+ attr_reader :numerator, :denominator, :fraction
6
+
7
+ def initialize(numerator:, denominator:)
8
+ @numerator = numerator
9
+ @denominator = denominator
10
+ @fraction = Rational(numerator, denominator)
11
+ end
12
+
13
+ def to_s(round_to: 2)
14
+ fraction_to_percentage.round(round_to).to_s + "%"
15
+ end
16
+
17
+ def format
18
+ :implied_probability
19
+ end
20
+
21
+ def greater_than_50_percent?
22
+ fraction.to_f > FIFTY_PERCENT_AS_A_DECIMAL
23
+ end
24
+
25
+ private
26
+
27
+ def fraction_to_percentage
28
+ fraction.to_f * 100.to_f
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ module ROdds
2
+ module Odd
3
+ class Standardizer
4
+
5
+ def self.call(params)
6
+ new(params).call
7
+ end
8
+
9
+ def initialize(params)
10
+ @raw_odd = params[:odd]
11
+ @odd_format = params[:format] || ROdds::InferOddFormat.of(odd: raw_odd)
12
+ end
13
+
14
+ def call
15
+ standardizer_class = ROdds::OddStandardizer::ClassFactory.for(format: odd_format)
16
+
17
+ standardizer_class.call(odd: raw_odd)
18
+ end
19
+
20
+ private
21
+ attr_reader :raw_odd, :odd_format
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ROdds
2
+ VERSION = "0.1.0"
3
+ end
data/r_odds.gemspec ADDED
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "r_odds/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "r_odds"
8
+ spec.version = ROdds::VERSION
9
+ spec.authors = ["George Wambold"]
10
+ spec.email = ["georgewambold@gmail.com"]
11
+
12
+ spec.summary = %q{Easy to work with odd conversions}
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ #if spec.respond_to?(:metadata)
18
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19
+ #else
20
+ # raise "RubyGems 2.0 or newer is required to protect against " \
21
+ # "public gem pushes."
22
+ #end
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.7"
36
+ spec.add_development_dependency "pry", "~> 0.10.3"
37
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: r_odds
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - George Wambold
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-26 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.3
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.3
69
+ description:
70
+ email:
71
+ - georgewambold@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/r_odds.rb
88
+ - lib/r_odds/convert_odd.rb
89
+ - lib/r_odds/infer_odd_format.rb
90
+ - lib/r_odds/odd_converters/american_to_implied_probability.rb
91
+ - lib/r_odds/odd_converters/class_factory.rb
92
+ - lib/r_odds/odd_converters/decimal_to_implied_probability.rb
93
+ - lib/r_odds/odd_converters/fractional_to_implied_probability.rb
94
+ - lib/r_odds/odd_converters/implied_probability_to_american.rb
95
+ - lib/r_odds/odd_converters/implied_probability_to_decimal.rb
96
+ - lib/r_odds/odd_converters/implied_probability_to_fractional.rb
97
+ - lib/r_odds/odd_converters/implied_probability_to_implied_probability.rb
98
+ - lib/r_odds/odd_standardizers/american.rb
99
+ - lib/r_odds/odd_standardizers/class_factory.rb
100
+ - lib/r_odds/odd_standardizers/decimal.rb
101
+ - lib/r_odds/odd_standardizers/fractional.rb
102
+ - lib/r_odds/odd_standardizers/implied_probability.rb
103
+ - lib/r_odds/odds/american.rb
104
+ - lib/r_odds/odds/base.rb
105
+ - lib/r_odds/odds/decimal.rb
106
+ - lib/r_odds/odds/fractional.rb
107
+ - lib/r_odds/odds/implied_probability.rb
108
+ - lib/r_odds/standardizer.rb
109
+ - lib/r_odds/version.rb
110
+ - r_odds.gemspec
111
+ homepage:
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.7.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Easy to work with odd conversions
135
+ test_files: []