ingreedy 0.0.9 → 0.1.0
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 +4 -4
- data/.travis.yml +7 -1
- data/README.md +21 -0
- data/lib/ingreedy.rb +2 -6
- data/lib/ingreedy/dictionaries/en.yml +11 -1
- data/lib/ingreedy/rationalizer.rb +27 -21
- data/lib/ingreedy/version.rb +1 -1
- data/spec/ingreedy_spec.rb +26 -0
- metadata +3 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 7c24a989bf96f54f8190d1b6fd568a8e8c143954
         | 
| 4 | 
            +
              data.tar.gz: cf1c0034a35aac3dfa9ee7c9ac4914756349591e
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 5941276bb95a4068ab44a4cf41923cf7b1f2ad2d243badeea3ffaacd8d3cb0601a10f29a32c33634c9dfb81c3f400db6d3a90ef16369ac7526607d9703356a9d
         | 
| 7 | 
            +
              data.tar.gz: 94055cb4fa3134feec302d5a741161ac9f365eb03c52c80b5de40d540e4919f9cf8cc0b23adc1e513d3abd30b9945b3315b46628ec6666b36522c85217af76ec
         | 
    
        data/.travis.yml
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -30,6 +30,27 @@ print result.ingredient | |
| 30 30 | 
             
              #=> "sucre"
         | 
| 31 31 | 
             
            ```
         | 
| 32 32 |  | 
| 33 | 
            +
            ### Handling amounts
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            By default, Ingreedy will convert all amounts to a rational number:
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            ```ruby
         | 
| 38 | 
            +
            result = Ingreedy.parse("1 1/2 cups flour")
         | 
| 39 | 
            +
            print result.amount
         | 
| 40 | 
            +
              #=> 3/2
         | 
| 41 | 
            +
            ```
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            However, setting `Ingreedy.preverse_amounts = true`, will allow amounts
         | 
| 44 | 
            +
            to be detected and returned as originally input:
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            ```ruby
         | 
| 47 | 
            +
            Ingreedy.preserve_amounts = true
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            result = Ingreedy.parse("1 1/2 cups flour")
         | 
| 50 | 
            +
            print result.amount
         | 
| 51 | 
            +
              #=> 1 1/2
         | 
| 52 | 
            +
            ```
         | 
| 53 | 
            +
             | 
| 33 54 | 
             
            [Live demo](http://hangryingreedytest.herokuapp.com/)
         | 
| 34 55 |  | 
| 35 56 | 
             
            # Pieces of Flair
         | 
    
        data/lib/ingreedy.rb
    CHANGED
    
    | @@ -7,12 +7,8 @@ require File.join(path, "dictionary_collection") | |
| 7 7 | 
             
            module Ingreedy
         | 
| 8 8 | 
             
              ParseFailed = Class.new(StandardError)
         | 
| 9 9 |  | 
| 10 | 
            -
               | 
| 11 | 
            -
                 | 
| 12 | 
            -
              end
         | 
| 13 | 
            -
             | 
| 14 | 
            -
              def self.locale=(locale)
         | 
| 15 | 
            -
                @locale = locale
         | 
| 10 | 
            +
              class << self
         | 
| 11 | 
            +
                attr_accessor :locale, :preserve_amounts
         | 
| 16 12 | 
             
              end
         | 
| 17 13 |  | 
| 18 14 | 
             
              def self.parse(query)
         | 
| @@ -59,26 +59,36 @@ | |
| 59 59 | 
             
                - "gr."
         | 
| 60 60 | 
             
                - "gram"
         | 
| 61 61 | 
             
                - "grams"
         | 
| 62 | 
            +
                - "gramme"
         | 
| 63 | 
            +
                - "grammes"
         | 
| 62 64 | 
             
              :kilogram:
         | 
| 63 65 | 
             
                - "kg"
         | 
| 64 66 | 
             
                - "kg."
         | 
| 65 67 | 
             
                - "kilogram"
         | 
| 66 68 | 
             
                - "kilograms"
         | 
| 69 | 
            +
                - "kilogramme"
         | 
| 70 | 
            +
                - "kilogrammes"
         | 
| 67 71 | 
             
              :liter:
         | 
| 68 72 | 
             
                - "l"
         | 
| 69 73 | 
             
                - "l."
         | 
| 70 74 | 
             
                - "liter"
         | 
| 71 75 | 
             
                - "liters"
         | 
| 76 | 
            +
                - "litre"
         | 
| 77 | 
            +
                - "litres"
         | 
| 72 78 | 
             
              :milligram:
         | 
| 73 79 | 
             
                - "mg"
         | 
| 74 80 | 
             
                - "mg."
         | 
| 75 81 | 
             
                - "milligram"
         | 
| 76 82 | 
             
                - "milligrams"
         | 
| 83 | 
            +
                - "milligramme"
         | 
| 84 | 
            +
                - "milligrammes"
         | 
| 77 85 | 
             
              :milliliter:
         | 
| 78 86 | 
             
                - "ml"
         | 
| 79 87 | 
             
                - "ml."
         | 
| 80 88 | 
             
                - "milliliter"
         | 
| 81 89 | 
             
                - "milliliters"
         | 
| 90 | 
            +
                - "millilitre"
         | 
| 91 | 
            +
                - "millilitres"
         | 
| 82 92 | 
             
              :pinch:
         | 
| 83 93 | 
             
                - "pinch"
         | 
| 84 94 | 
             
                - "pinches"
         | 
| @@ -132,6 +142,6 @@ | |
| 132 142 | 
             
              sixty: 60
         | 
| 133 143 | 
             
              seventy: 70
         | 
| 134 144 | 
             
              eighty: 80
         | 
| 135 | 
            -
              ninety:  | 
| 145 | 
            +
              ninety: 90
         | 
| 136 146 | 
             
            :prepositions:
         | 
| 137 147 | 
             
              - "of"
         | 
| @@ -12,37 +12,43 @@ module Ingreedy | |
| 12 12 | 
             
                end
         | 
| 13 13 |  | 
| 14 14 | 
             
                def rationalize
         | 
| 15 | 
            -
                  if  | 
| 16 | 
            -
                     | 
| 17 | 
            -
                   | 
| 18 | 
            -
                     | 
| 19 | 
            -
                    if @integer
         | 
| 20 | 
            -
                      result += @integer.to_i
         | 
| 21 | 
            -
                    end
         | 
| 22 | 
            -
                  elsif @integer
         | 
| 23 | 
            -
                    result = @integer.to_r
         | 
| 24 | 
            -
                  elsif @float
         | 
| 25 | 
            -
                    result = @float.tr(",", ".").to_r
         | 
| 15 | 
            +
                  if Ingreedy.preserve_amounts
         | 
| 16 | 
            +
                    (normalized_word || compound_fraction || @float || @integer)
         | 
| 17 | 
            +
                  else
         | 
| 18 | 
            +
                    (normalized_word || rationalized_fraction || rationalized_float || @integer).to_r
         | 
| 26 19 | 
             
                  end
         | 
| 27 | 
            -
             | 
| 28 | 
            -
                  result
         | 
| 29 20 | 
             
                end
         | 
| 30 21 |  | 
| 31 22 | 
             
                private
         | 
| 32 23 |  | 
| 33 | 
            -
                def  | 
| 34 | 
            -
                   | 
| 35 | 
            -
             | 
| 24 | 
            +
                def normalized_word
         | 
| 25 | 
            +
                  return unless @word
         | 
| 26 | 
            +
                  Ingreedy.dictionaries.current.numbers[@word.downcase]
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def normalized_fraction
         | 
| 30 | 
            +
                  @fraction.tap do |fraction|
         | 
| 31 | 
            +
                    Ingreedy.dictionaries.current.vulgar_fractions.each do |char, amount|
         | 
| 32 | 
            +
                      fraction.gsub!(char, amount.to_s)
         | 
| 33 | 
            +
                    end
         | 
| 36 34 | 
             
                  end
         | 
| 37 | 
            -
                  @fraction.to_r
         | 
| 38 35 | 
             
                end
         | 
| 39 36 |  | 
| 40 | 
            -
                def  | 
| 41 | 
            -
                   | 
| 37 | 
            +
                def rationalized_fraction
         | 
| 38 | 
            +
                  return unless @fraction
         | 
| 39 | 
            +
                  result = normalized_fraction
         | 
| 40 | 
            +
                  result = result.to_r + @integer.to_i
         | 
| 41 | 
            +
                  result
         | 
| 42 42 | 
             
                end
         | 
| 43 43 |  | 
| 44 | 
            -
                def  | 
| 45 | 
            -
                   | 
| 44 | 
            +
                def compound_fraction
         | 
| 45 | 
            +
                  return unless @fraction
         | 
| 46 | 
            +
                  "#{@integer} #{normalized_fraction}".strip
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                def rationalized_float
         | 
| 50 | 
            +
                  return unless @float
         | 
| 51 | 
            +
                  @float.tr(",", ".")
         | 
| 46 52 | 
             
                end
         | 
| 47 53 | 
             
              end
         | 
| 48 54 | 
             
            end
         | 
    
        data/lib/ingreedy/version.rb
    CHANGED
    
    
    
        data/spec/ingreedy_spec.rb
    CHANGED
    
    | @@ -41,6 +41,32 @@ describe Ingreedy, "amount parsing" do | |
| 41 41 | 
             
              end
         | 
| 42 42 | 
             
            end
         | 
| 43 43 |  | 
| 44 | 
            +
            describe Ingreedy, "amount parsing preserving original value" do
         | 
| 45 | 
            +
              around do |example|
         | 
| 46 | 
            +
                Ingreedy.preserve_amounts = true
         | 
| 47 | 
            +
                example.run
         | 
| 48 | 
            +
                Ingreedy.preserve_amounts = false
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              {
         | 
| 52 | 
            +
                "1 cup flour" => "1",
         | 
| 53 | 
            +
                "2 cups flour" => "2",
         | 
| 54 | 
            +
                "1 1/2 cups flour" => "1 1/2",
         | 
| 55 | 
            +
                "¼ cups flour" => "1/4",
         | 
| 56 | 
            +
                "1 ½ cups flour" => "1 1/2",
         | 
| 57 | 
            +
                "1½ cups flour" => "1 1/2",
         | 
| 58 | 
            +
                "1.0 cup flour" => "1.0",
         | 
| 59 | 
            +
                "1.5 cups flour" => "1.5",
         | 
| 60 | 
            +
                "1,5 cups flour" => "1,5",
         | 
| 61 | 
            +
                "1/2 cups flour" => "1/2",
         | 
| 62 | 
            +
                ".25 cups flour" => ".25",
         | 
| 63 | 
            +
              }.each do |query, expected|
         | 
| 64 | 
            +
                it "parses the correct amount" do
         | 
| 65 | 
            +
                  expect(Ingreedy.parse(query).amount).to eq(expected)
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
              end
         | 
| 68 | 
            +
            end
         | 
| 69 | 
            +
             | 
| 44 70 | 
             
            describe Ingreedy, "english units" do
         | 
| 45 71 | 
             
              context "abbreviated" do
         | 
| 46 72 | 
             
                {
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: ingreedy
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0 | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Ian C. Anderson
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016- | 
| 11 | 
            +
            date: 2016-08-19 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: parslet
         | 
| @@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 157 157 | 
             
                  version: '0'
         | 
| 158 158 | 
             
            requirements: []
         | 
| 159 159 | 
             
            rubyforge_project: 
         | 
| 160 | 
            -
            rubygems_version: 2.4. | 
| 160 | 
            +
            rubygems_version: 2.4.5.1
         | 
| 161 161 | 
             
            signing_key: 
         | 
| 162 162 | 
             
            specification_version: 4
         | 
| 163 163 | 
             
            summary: Recipe parser
         |