Permalink
Browse files

fixes issue with query string parsing when a z exists

closes #127
  • Loading branch information...
1 parent 4106ac6 commit 36db40cfe7086fde0c227c6cdeeedc0d98147ac6 @adamcooke adamcooke committed May 16, 2017
Showing with 46 additions and 1 deletion.
  1. +1 −1 lib/postal/query_string.rb
  2. +45 −0 spec/lib/postal/query_string_spec.rb
@@ -14,7 +14,7 @@ def empty?
end
def to_hash
- @hash ||= @string.scan(/([a-z]+)\:\s*(?:(\d{2,4}\-\d{2}-\d{2}\s\d{2}\:\d{2})|\"(.*?)\"|(.*?))[\s\z]/).each_with_object({}) do |(key, date, string_with_spaces, value), hash|
+ @hash ||= @string.scan(/([a-z]+)\:\s*(?:(\d{2,4}\-\d{2}-\d{2}\s\d{2}\:\d{2})|\"(.*?)\"|(.*?))(\s|\z)/).each_with_object({}) do |(key, date, string_with_spaces, value), hash|
if date
actual_value = date
elsif string_with_spaces
@@ -0,0 +1,45 @@
+require 'rails_helper'
+
+describe Postal::QueryString do
+
+ it "should work with a single item" do
+ qs = Postal::QueryString.new("to: test@example.com")
+ expect(qs.to_hash['to']).to eq 'test@example.com'
+ end
+
+
+ it "should work with a multiple items" do
+ qs = Postal::QueryString.new("to: test@example.com from: another@example.com")
+ expect(qs.to_hash['to']).to eq 'test@example.com'
+ expect(qs.to_hash['from']).to eq 'another@example.com'
+ end
+
+ it "should not require a space after the field name" do
+ qs = Postal::QueryString.new("to:test@example.com from:another@example.com")
+ expect(qs.to_hash['to']).to eq 'test@example.com'
+ expect(qs.to_hash['from']).to eq 'another@example.com'
+ end
+
+ it "should return nil when it receives blank" do
+ qs = Postal::QueryString.new("to:[blank]")
+ expect(qs.to_hash['to']).to eq nil
+ end
+
+ it "should handle dates with spaces" do
+ qs = Postal::QueryString.new("date: 2017-02-12 15:20")
+ expect(qs.to_hash['date']).to eq("2017-02-12 15:20")
+ end
+
+ it "should return an array for multiple items" do
+ qs = Postal::QueryString.new("to: test@example.com to: another@example.com")
+ expect(qs.to_hash['to']).to be_a(Array)
+ expect(qs.to_hash['to'][0]).to eq 'test@example.com'
+ expect(qs.to_hash['to'][1]).to eq 'another@example.com'
+ end
+
+ it "should work with a z in the string" do
+ qs = Postal::QueryString.new("to: testaz@example.com")
+ expect(qs.to_hash['to']).to eq "testaz@example.com"
+ end
+
+end

0 comments on commit 36db40c

Please sign in to comment.