oddx 0.1.4

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
+ SHA256:
3
+ metadata.gz: eb960e3ecfc08baf0ee3e2b7b1b7da507f8190bb6546d2530cf544c09970338a
4
+ data.tar.gz: 8116f4b3bca59a93ec214fc817f7581d7d5730bf59875fd188dc93fbf6cbcdae
5
+ SHA512:
6
+ metadata.gz: 4cb9369d8addcfe95ce1966ca580381d7e92baf7f5056e07c1593c771c1945f43ced50d5b35149e215a36563fbdafb996b419f18344a7cf01338a3c5dbea454d
7
+ data.tar.gz: efa12e6797e580bdbd62b748cd985a591d19de2488036a0b0eea1155792dc2f32deab4cfd286b48e4cb9254b459886e043f55246e8e494d5e28ff22fab86467b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,236 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
4
+ # to ignore them, so only the ones explicitly set in this file are enabled.
5
+ DisabledByDefault: true
6
+ SuggestExtensions: false
7
+
8
+ # Prefer &&/|| over and/or.
9
+ Style/AndOr:
10
+ Enabled: true
11
+
12
+ # Align `when` with `case`.
13
+ Layout/CaseIndentation:
14
+ Enabled: true
15
+
16
+ Layout/ClosingHeredocIndentation:
17
+ Enabled: true
18
+
19
+ Layout/ClosingParenthesisIndentation:
20
+ Enabled: true
21
+
22
+ # Align comments with method definitions.
23
+ Layout/CommentIndentation:
24
+ Enabled: true
25
+
26
+ Layout/ElseAlignment:
27
+ Enabled: true
28
+
29
+ # Align `end` with the matching keyword or starting expression except for
30
+ # assignments, where it should be aligned with the LHS.
31
+ Layout/EndAlignment:
32
+ Enabled: true
33
+ EnforcedStyleAlignWith: variable
34
+ AutoCorrect: true
35
+
36
+ Layout/EndOfLine:
37
+ Enabled: true
38
+
39
+ Layout/EmptyLineAfterMagicComment:
40
+ Enabled: true
41
+
42
+ Layout/EmptyLinesAroundAccessModifier:
43
+ Enabled: true
44
+ EnforcedStyle: only_before
45
+
46
+ Layout/EmptyLinesAroundBlockBody:
47
+ Enabled: true
48
+
49
+ # In a regular class definition, no empty lines around the body.
50
+ Layout/EmptyLinesAroundClassBody:
51
+ Enabled: true
52
+
53
+ # In a regular method definition, no empty lines around the body.
54
+ Layout/EmptyLinesAroundMethodBody:
55
+ Enabled: true
56
+
57
+ # In a regular module definition, no empty lines around the body.
58
+ Layout/EmptyLinesAroundModuleBody:
59
+ Enabled: true
60
+
61
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
62
+ Style/HashSyntax:
63
+ Enabled: true
64
+
65
+ # Method definitions after `private` or `protected` isolated calls need one
66
+ # extra level of indentation.
67
+ Layout/IndentationConsistency:
68
+ Enabled: true
69
+ EnforcedStyle: indented_internal_methods
70
+
71
+ # Two spaces, no tabs (for indentation).
72
+ Layout/IndentationWidth:
73
+ Enabled: true
74
+
75
+ Layout/LeadingCommentSpace:
76
+ Enabled: true
77
+
78
+ Layout/SpaceAfterColon:
79
+ Enabled: true
80
+
81
+ Layout/SpaceAfterComma:
82
+ Enabled: true
83
+
84
+ Layout/SpaceAfterSemicolon:
85
+ Enabled: true
86
+
87
+ Layout/SpaceAroundEqualsInParameterDefault:
88
+ Enabled: true
89
+
90
+ Layout/SpaceAroundKeyword:
91
+ Enabled: true
92
+
93
+ Layout/SpaceAroundOperators:
94
+ Enabled: true
95
+
96
+ Layout/SpaceBeforeComma:
97
+ Enabled: true
98
+
99
+ Layout/SpaceBeforeComment:
100
+ Enabled: true
101
+
102
+ Layout/SpaceBeforeFirstArg:
103
+ Enabled: true
104
+
105
+ Style/DefWithParentheses:
106
+ Enabled: true
107
+
108
+ # Defining a method with parameters needs parentheses.
109
+ Style/MethodDefParentheses:
110
+ Enabled: true
111
+
112
+ Style/ExplicitBlockArgument:
113
+ Enabled: true
114
+
115
+ Style/FrozenStringLiteralComment:
116
+ Enabled: true
117
+ EnforcedStyle: always
118
+
119
+ Style/MapToHash:
120
+ Enabled: true
121
+
122
+ Style/RedundantFreeze:
123
+ Enabled: true
124
+
125
+ # Use `foo {}` not `foo{}`.
126
+ Layout/SpaceBeforeBlockBraces:
127
+ Enabled: true
128
+
129
+ # Use `foo { bar }` not `foo {bar}`.
130
+ Layout/SpaceInsideBlockBraces:
131
+ Enabled: true
132
+ EnforcedStyleForEmptyBraces: space
133
+
134
+ # Use `{ a: 1 }` not `{a:1}`.
135
+ Layout/SpaceInsideHashLiteralBraces:
136
+ Enabled: true
137
+
138
+ Layout/SpaceInsideParens:
139
+ Enabled: true
140
+
141
+ # Check quotes usage according to lint rule below.
142
+ Style/StringLiterals:
143
+ Enabled: true
144
+ EnforcedStyle: double_quotes
145
+
146
+ # Detect hard tabs, no hard tabs.
147
+ Layout/IndentationStyle:
148
+ Enabled: true
149
+
150
+ # Empty lines should not have any spaces.
151
+ Layout/TrailingEmptyLines:
152
+ Enabled: true
153
+
154
+ # No trailing whitespace.
155
+ Layout/TrailingWhitespace:
156
+ Enabled: true
157
+
158
+ # Use quotes for string literals when they are enough.
159
+ Style/RedundantPercentQ:
160
+ Enabled: true
161
+
162
+ Lint/AmbiguousOperator:
163
+ Enabled: true
164
+
165
+ Lint/AmbiguousRegexpLiteral:
166
+ Enabled: true
167
+
168
+ Lint/DuplicateRequire:
169
+ Enabled: true
170
+
171
+ Lint/DuplicateMagicComment:
172
+ Enabled: true
173
+
174
+ Lint/DuplicateMethods:
175
+ Enabled: true
176
+
177
+ Lint/ErbNewArguments:
178
+ Enabled: true
179
+
180
+ Lint/EnsureReturn:
181
+ Enabled: true
182
+
183
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
184
+ Lint/RequireParentheses:
185
+ Enabled: true
186
+
187
+ Lint/RedundantStringCoercion:
188
+ Enabled: true
189
+
190
+ Lint/UriEscapeUnescape:
191
+ Enabled: true
192
+
193
+ Lint/UselessAssignment:
194
+ Enabled: true
195
+
196
+ Lint/DeprecatedClassMethods:
197
+ Enabled: true
198
+
199
+ Style/EvalWithLocation:
200
+ Enabled: true
201
+ Exclude:
202
+ - "**/test/**/*"
203
+
204
+ Style/ParenthesesAroundCondition:
205
+ Enabled: true
206
+
207
+ Style/HashTransformKeys:
208
+ Enabled: true
209
+
210
+ Style/HashTransformValues:
211
+ Enabled: true
212
+
213
+ Style/RedundantBegin:
214
+ Enabled: true
215
+
216
+ Style/RedundantReturn:
217
+ Enabled: true
218
+ AllowMultipleReturnValues: true
219
+
220
+ Style/RedundantRegexpEscape:
221
+ Enabled: true
222
+
223
+ Style/Semicolon:
224
+ Enabled: true
225
+ AllowAsExpressionSeparator: true
226
+
227
+ # Prefer Foo.method over Foo::method
228
+ Style/ColonMethodCall:
229
+ Enabled: true
230
+
231
+ Style/TrivialAccessors:
232
+ Enabled: true
233
+
234
+ # Prefer a = b || c over a = b ? b : c
235
+ Style/RedundantCondition:
236
+ Enabled: true
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in oddx.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ oddx (0.1.4)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.3)
12
+ parallel (1.22.1)
13
+ parser (3.2.1.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.7.0)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.1)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.45.1)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.2.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.24.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 2.4.0, < 3.0)
42
+ rubocop-ast (1.26.0)
43
+ parser (>= 3.2.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.4.2)
46
+
47
+ PLATFORMS
48
+ x86_64-darwin-22
49
+ x86_64-linux
50
+
51
+ DEPENDENCIES
52
+ oddx!
53
+ rake (~> 13.0)
54
+ rspec (~> 3.0)
55
+ rubocop (~> 1.21)
56
+
57
+ BUNDLED WITH
58
+ 2.4.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2023] [Bryan Byrne]
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Oddx - Convert sporting odds to any format
2
+
3
+ Oddx is a ruby library designed to simplify conversion of sporting odds to alternate formats (e.g. 3/1 » +300).
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/oddx.svg)](https://badge.fury.io/rb/oddx) [![Tests](https://github.com/sportsroom/oddx/actions/workflows/tests.yml/badge.svg)](https://github.com/sportsroom/oddx/actions/workflows/tests.yml)
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add oddx
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install oddx
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ require "oddx"
21
+
22
+ class OddxExample
23
+ def self.convert(odds)
24
+ converter = Oddx.parse(odds)
25
+
26
+ puts "Fractional: #{converter.fractional}"
27
+ puts "Decimal: #{converter.decimal}"
28
+ puts "Implied Probability: #{converter.probability}"
29
+ puts "Moneyline: #{converter.moneyline}"
30
+ end
31
+ end
32
+ ```
33
+
34
+ ```shell
35
+ > OddxExample.convert("+2040")
36
+ Fractional: 102/5
37
+ Decimal: 21.4
38
+ Implied Probability: 4.7%
39
+ Moneyline: +2040
40
+
41
+ > OddxExample.convert("4/1")
42
+ Fractional: 4/1
43
+ Decimal: 5
44
+ Implied Probability: 20%
45
+ Moneyline: +400
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sportsroom/oddx.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "odds"
4
+
5
+ module Oddx
6
+ class DecimalOdds < Oddx::Odds
7
+ REGEX = /^(?<val>\d*\.?\d+$)/
8
+
9
+ def initialize(odds)
10
+ @odds = odds
11
+ @bigdecimal_odds = BigDecimal(odds, Oddx::Odds::BIGDECIMAL_SIG_DIGITS)
12
+ end
13
+
14
+ def decimal
15
+ @odds
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "odds"
4
+
5
+ module Oddx
6
+ class FractionalOdds < Oddx::Odds
7
+ REGEX = /^(?<num>\d+)\/(?<den>\d+)$/
8
+
9
+ def initialize(odds)
10
+ @odds = odds
11
+ regex_match = @odds.match(REGEX)
12
+ @bigdecimal_odds = BigDecimal((regex_match[:num].to_f / regex_match[:den].to_f) + 1,
13
+ Oddx::Odds::BIGDECIMAL_SIG_DIGITS)
14
+ end
15
+
16
+ def fractional
17
+ @odds
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "odds"
4
+
5
+ module Oddx
6
+ class MoneylineOdds < Oddx::Odds
7
+ REGEX = /^(?<mod>\+|-)(?<value>\d+\.?\d+)$/
8
+
9
+ def initialize(odds)
10
+ @odds = odds
11
+ regex_match = @odds.match(REGEX)
12
+ case regex_match[:mod]
13
+ when "+"
14
+ @bigdecimal_odds = BigDecimal((regex_match[:value].to_f / 100) + 1,
15
+ Oddx::Odds::BIGDECIMAL_SIG_DIGITS)
16
+ else
17
+ @bigdecimal_odds = BigDecimal((100 / regex_match[:value].to_f) + 1,
18
+ Oddx::Odds::BIGDECIMAL_SIG_DIGITS)
19
+ end
20
+ end
21
+
22
+ def moneyline
23
+ @odds
24
+ end
25
+ end
26
+ end
data/lib/oddx/odds.rb ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+
5
+ module Oddx
6
+ class Odds
7
+ BIGDECIMAL_SIG_DIGITS = 3
8
+
9
+ def initialize
10
+ raise NotImplementedError
11
+ end
12
+
13
+ def decimal
14
+ "%g" % (@bigdecimal_odds.round(2))
15
+ end
16
+
17
+ def fractional
18
+ (@bigdecimal_odds - 1).to_r.rationalize.to_s
19
+ end
20
+
21
+ def moneyline
22
+ if @bigdecimal_odds >= 2
23
+ "+%g" % ((@bigdecimal_odds - 1) * 100).round(1)
24
+ else
25
+ "%g" % (-100 / (@bigdecimal_odds - 1)).round(1)
26
+ end
27
+ end
28
+
29
+ def probability
30
+ "%g%%" % ((1 / @bigdecimal_odds) * 100).round(1)
31
+ end
32
+
33
+ def to_s
34
+ "Fractional: #{fractional}, Moneyline: #{moneyline}, Decimal: #{decimal}, Implied Probability: #{probability}"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oddx
4
+ class OddxError < StandardError
5
+ attr_reader :odds
6
+ def initialize(msg: "Ensure provided odds match supported formats", odds: nil)
7
+ @odds = odds
8
+ super(msg)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "odds"
4
+
5
+ module Oddx
6
+ class ProbabilityOdds < Oddx::Odds
7
+ REGEX = /^(?<value>\d+\.?\d+)?%/
8
+
9
+ def initialize(odds)
10
+ @odds = odds
11
+ @bigdecimal_odds = BigDecimal(1 / (odds.to_f / 100),
12
+ Oddx::Odds::BIGDECIMAL_SIG_DIGITS)
13
+ end
14
+
15
+ def probability
16
+ @odds
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oddx
4
+ VERSION = "0.1.4"
5
+ end
data/lib/oddx.rb ADDED
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "oddx/version"
4
+ require_relative "oddx/decimal_odds"
5
+ require_relative "oddx/fractional_odds"
6
+ require_relative "oddx/moneyline_odds"
7
+ require_relative "oddx/probability_odds"
8
+ require_relative "oddx/oddx_error"
9
+
10
+ # The main Oddx driver
11
+ module Oddx
12
+ # Convert sporting odds to any format
13
+ #
14
+ # Example:
15
+ # >> odds = Oddx.parse "3/1"
16
+ # >> odds.moneyline
17
+ # => "+300"
18
+ # >> odds.decimal
19
+ # => "4"
20
+ # >> odds.probability
21
+ # => "25%"
22
+ # >> odds.fractional
23
+ # => "3/1"
24
+ #
25
+ # Supported formats:
26
+ # 1. moneyline (e.g. +300, -300)
27
+ # 2. fractional (e.g. 3/1, 1/5)
28
+ # 3. decimal (e.g. 1.2, 4)
29
+ # 4. probability (e.g. 95%, 30%)
30
+
31
+ ODDS_FORMATS = [
32
+ Oddx::FractionalOdds,
33
+ Oddx::MoneylineOdds,
34
+ Oddx::DecimalOdds,
35
+ Oddx::ProbabilityOdds
36
+ ]
37
+
38
+ def self.parse(odds)
39
+ begin
40
+ ODDS_FORMATS.each do |format|
41
+ if odds.match format::REGEX
42
+ return format.new(odds)
43
+ end
44
+ end
45
+ rescue NoMethodError
46
+ raise Oddx::OddxError.new(msg: "Error parsing odds, provide as String to avoid ambiguity. e.g: '+4000'", odds: odds)
47
+ end
48
+ raise Oddx::OddxError.new(odds: odds)
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oddx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - irishbryan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - bbyrne@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/oddx.rb
28
+ - lib/oddx/decimal_odds.rb
29
+ - lib/oddx/fractional_odds.rb
30
+ - lib/oddx/moneyline_odds.rb
31
+ - lib/oddx/odds.rb
32
+ - lib/oddx/oddx_error.rb
33
+ - lib/oddx/probability_odds.rb
34
+ - lib/oddx/version.rb
35
+ homepage: https://github.com/irishbryan/oddx
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/irishbryan/oddx
40
+ source_code_uri: https://github.com/irishbryan/oddx
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.6.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.3.26
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Convert sporting odds to any format.
60
+ test_files: []