fuzzy_money 0.0.1

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: 9ccb5754acc560237cf95ea88dace0e1f387848d
4
+ data.tar.gz: aa2a303bf2f244fcd2bd1cc335cce1a2d153600a
5
+ SHA512:
6
+ metadata.gz: 6af903c8643e1034ccaa7145874cb890c27d0f77393a2dcbc7b575fd2b6f3cb99d8babf9c108e73c517af53395d62f09680f890a145b21315a0079f98fff51b5
7
+ data.tar.gz: e8d7251a6b4c1c030f47c1fa515f763141cc94937f928d417b0eec38f481e9acb441af6ff084c7542b0ca81fe9a51a0e94b5bf80f20cd77af85a88f44c144f9a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 2.0.0
3
+ - ruby-head
4
+ matrix:
5
+ allow_failures:
6
+ - rvm: ruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fuzzy_money.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ben Sheldon
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # FuzzyMoney
2
+
3
+ [![Build Status](https://travis-ci.org/bensheldon/fuzzy_money.svg?branch=master)](https://travis-ci.org/bensheldon/fuzzy_money)
4
+
5
+ Make accurate-enough conversions and comparisons between price strings. For when you're collecting inconsistently structured pricing information (for example, via scraping) and you don't need the rigidness of [RubyMoney](https://github.com/RubyMoney/money).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'fuzzy_money'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fuzzy_money
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it ( http://github.com/<my-github-username>/fuzzy_money/fork )
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fuzzy_money/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fuzzy_money"
8
+ spec.version = FuzzyMoney::VERSION
9
+ spec.authors = ["Ben Sheldon"]
10
+ spec.email = ["bensheldon@gmail.com"]
11
+ spec.summary = %q{Make accurate-enough conversions and comparisons between price strings.}
12
+ spec.description = %q{Make accurate-enough conversions and comparisons between price strings. For when you're collecting inconsistently structured pricing information (for example, via scraping) and you don't need the rigidness of [RubyMoney](https://github.com/RubyMoney/money).
13
+ }
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "guard-rspec"
26
+ # spec.add_development_dependency "simplecov"
27
+ # spec.add_development_dependency "coveralls"
28
+ end
@@ -0,0 +1,107 @@
1
+ module FuzzyMoney
2
+ class Price
3
+ attr_reader :raw, :normalizations, :denominated_order
4
+
5
+ DEFAULT_NORMALIZATIONS = {
6
+ '$' => 1.0,
7
+ 'C$' => 0.95,
8
+ '€' => 1.32,
9
+ '£' => 1.56,
10
+ }
11
+
12
+ DEFAULT_DENOMINATED_ORDER = {
13
+ '$' => :before,
14
+ 'C$' => :before,
15
+ '€' => :after,
16
+ '£' => :before,
17
+ }
18
+
19
+ def initialize(raw, options={})
20
+ if raw.is_a? String
21
+ @raw = raw.clone.strip
22
+ else
23
+ @raw = ""
24
+ end
25
+
26
+ @normalizations = options.fetch(:normalizations, DEFAULT_NORMALIZATIONS)
27
+ @denominated_order = options.fetch(:denominated_order, DEFAULT_DENOMINATED_ORDER)
28
+ end
29
+
30
+ def multiple
31
+ Array(
32
+ raw.split('/')
33
+ .map(&:strip)
34
+ .map { |r| Price.new(r) }
35
+ )
36
+ end
37
+
38
+ def range
39
+ Array(
40
+ raw.split('-')
41
+ .map(&:strip)
42
+ .map { |r| Price.new(r) }
43
+ )
44
+ end
45
+
46
+ def range?
47
+ range.size > 1
48
+ end
49
+
50
+ def multiple?
51
+ multiple.size > 1
52
+ end
53
+
54
+ def min
55
+ if range?
56
+ range[0]
57
+ elsif multiple?
58
+ multiple.sort { |p| p.normalized_float }.first
59
+ else
60
+ self
61
+ end
62
+ end
63
+
64
+ def normalize
65
+ min.denominate(:round)
66
+ end
67
+
68
+ def denominate(convert=:to_f)
69
+ value = split[:value].send(convert)
70
+ denomination = split[:denomination]
71
+
72
+ if denominated_order[denomination] == :after
73
+ "#{value}#{denomination}"
74
+ else
75
+ "#{denomination}#{value}"
76
+ end
77
+ end
78
+
79
+ def split
80
+ denomination, value = \
81
+ case raw
82
+ when /C\$(\d+\.?\d{0,2})/ # Canadian
83
+ ['C$', $1.to_f]
84
+ when /\$(\d+\.?\d{0,2})/ # USA
85
+ ['$', $1.to_f]
86
+ when /€(\d+[\.,]?\d{0,2})/ # Leading Euro
87
+ ['€', $1.to_f]
88
+ when /(\d+[\.,]?\d{0,2})€/ # Trailing Euro
89
+ ['€', $1.to_f]
90
+ when /£(\d+[\.,]?\d{0,2})/ # Pounds
91
+ ['£', $1.to_f]
92
+ else
93
+ [nil, nil]
94
+ end
95
+
96
+ {
97
+ denomination: denomination,
98
+ value: value
99
+ }
100
+ end
101
+
102
+ def normalized_float
103
+ return 0.0 if split[:value].nil?
104
+ split[:value] * normalizations[split[:denomination]]
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module FuzzyMoney
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'fuzzy_money/version'
2
+ require_relative 'fuzzy_money/price'
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ describe FuzzyMoney::Price do
4
+ describe '#normalize' do
5
+ {
6
+ '$10' => '$10',
7
+ '$10-$55.00' => '$10',
8
+ '£8/€10/$12' => '$12',
9
+ '$10.99' => '$11',
10
+ '€10,00' => '10€',
11
+ '10.00€' => '10€',
12
+ }.each do |raw, result|
13
+ it "#{raw} -> #{result}" do
14
+ price = FuzzyMoney::Price.new(raw)
15
+ expect(price.normalize).to eq result
16
+ end
17
+ end
18
+ end
19
+
20
+ describe '#denominate' do
21
+ it "'£8/€10/$12'.min -> $12" do
22
+ price = FuzzyMoney::Price.new('£8/€10/$12')
23
+
24
+ expect(price.min.denominate(:to_i)).to eq('$12')
25
+ end
26
+ end
27
+
28
+ describe '#multiple' do
29
+ {
30
+ '£8/€10/$12' => ['£8', '€10' ,'$12'],
31
+ }.each do |raw, result|
32
+ it "#{raw} -> #{result}" do
33
+ price = FuzzyMoney::Price.new(raw)
34
+ ranges = price.multiple.map(&:raw)
35
+ expect(ranges).to eq result
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#multiple?' do
41
+ {
42
+ '£8/€10/$12' => true,
43
+ '€10' => false
44
+ }.each do |raw, result|
45
+ it "#{raw} -> #{result}" do
46
+ price = FuzzyMoney::Price.new(raw)
47
+ expect(price.multiple?).to eq result
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#range' do
53
+ {
54
+ '$10-$55.00' => ['$10', '$55.00'],
55
+ }.each do |raw, result|
56
+ it "#{raw} -> #{result}" do
57
+ price = FuzzyMoney::Price.new(raw)
58
+ ranges = price.range.map(&:raw)
59
+ expect(ranges).to eq result
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '#range?' do
65
+ {
66
+ '$10-$55.00' => true,
67
+ '$55.00' => false,
68
+ }.each do |raw, result|
69
+ it "#{raw} -> #{result}" do
70
+ price = FuzzyMoney::Price.new(raw)
71
+ expect(price.range?).to eq result
72
+ end
73
+ end
74
+ end
75
+
76
+ describe '#min' do
77
+ {
78
+ '$10-$55.00' => '$10',
79
+ '£8/€10/$12' => '$12',
80
+ '$10.00' => '$10.00',
81
+ }.each do |raw, result|
82
+ it "#{raw} -> #{result}" do
83
+ price = FuzzyMoney::Price.new(raw)
84
+ expect(price.min.raw).to eq result
85
+ end
86
+ end
87
+ end
88
+
89
+ describe "#split" do
90
+ {
91
+ # USD
92
+ "$10.50" => ['$', 10.50],
93
+ # Canadian Dollars
94
+ "C$10.50" => ['C$', 10.50],
95
+ # Euros
96
+ "€10.50" => ['€', 10.50],
97
+ "10.50€" => ['€', 10.50],
98
+ # GBP
99
+ "£10.50" => ['£', 10.50]
100
+ }.each do |raw, result|
101
+ it "splits #{raw} -> { denomination: '#{result[0]}', value: #{result[1]} }" do
102
+ denomination = result[0]
103
+ value = result[1]
104
+
105
+ price = FuzzyMoney::Price.new(raw)
106
+ expect(price.split).to eq({
107
+ denomination: denomination,
108
+ value: value
109
+ })
110
+ end
111
+ end
112
+ end
113
+
114
+ describe '#normalized_float' do
115
+ {
116
+ # USD
117
+ "$10.99" => 10.99,
118
+ # Canadian Dollars
119
+ "C$10.00" => 9.50,
120
+ # Euros
121
+ "€10.00" => 13.20,
122
+ "€10,00" => 13.20,
123
+ "10.00€" => 13.20,
124
+ "10,00€" => 13.20,
125
+ # GBP
126
+ "£10.00" => 15.60,
127
+ # Multiple
128
+ '£8/€10/$12' => 12.0,
129
+ # Range
130
+ "$10-$16" => 10.0,
131
+ }.each do |raw, result|
132
+ it "converts #{raw} -> #{result}" do
133
+ price = FuzzyMoney::Price.new(raw)
134
+ expect(price.normalized_float).to be_within(0.01).of(result)
135
+ end
136
+ end
137
+
138
+ it 'converts nil -> 0.0' do
139
+ price = FuzzyMoney::Price.new(nil)
140
+ expect(price.normalized_float).to eq 0.0
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'fuzzy_money'
9
+
10
+ RSpec.configure do |config|
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fuzzy_money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Sheldon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-29 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: |
70
+ Make accurate-enough conversions and comparisons between price strings. For when you're collecting inconsistently structured pricing information (for example, via scraping) and you don't need the rigidness of [RubyMoney](https://github.com/RubyMoney/money).
71
+ email:
72
+ - bensheldon@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Guardfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - fuzzy_money.gemspec
86
+ - lib/fuzzy_money.rb
87
+ - lib/fuzzy_money/price.rb
88
+ - lib/fuzzy_money/version.rb
89
+ - spec/lib/fuzzy_money/price_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.0
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Make accurate-enough conversions and comparisons between price strings.
115
+ test_files:
116
+ - spec/lib/fuzzy_money/price_spec.rb
117
+ - spec/spec_helper.rb
118
+ has_rdoc: