-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move Postal::QueryString to app/util/query_string
- Loading branch information
Showing
5 changed files
with
81 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
class QueryString | ||
|
||
def initialize(string) | ||
@string = string.strip + " " | ||
end | ||
|
||
def [](value) | ||
hash[value.to_s] | ||
end | ||
|
||
delegate :empty?, to: :hash | ||
|
||
def 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 | ||
actual_value = string_with_spaces | ||
elsif value == "[blank]" | ||
actual_value = nil | ||
else | ||
actual_value = value | ||
end | ||
|
||
if hash.keys.include?(key.to_s) | ||
hash[key.to_s] = [hash[key.to_s]].flatten | ||
hash[key.to_s] << actual_value | ||
else | ||
hash[key.to_s] = actual_value | ||
end | ||
end | ||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe QueryString do | ||
it "works with a single item" do | ||
qs = described_class.new("to: [email protected]") | ||
expect(qs.hash["to"]).to eq "[email protected]" | ||
end | ||
|
||
it "works with a multiple items" do | ||
qs = described_class.new("to: [email protected] from: [email protected]") | ||
expect(qs.hash["to"]).to eq "[email protected]" | ||
expect(qs.hash["from"]).to eq "[email protected]" | ||
end | ||
|
||
it "does not require a space after the field name" do | ||
qs = described_class.new("to:[email protected] from:[email protected]") | ||
expect(qs.hash["to"]).to eq "[email protected]" | ||
expect(qs.hash["from"]).to eq "[email protected]" | ||
end | ||
|
||
it "returns nil when it receives blank" do | ||
qs = described_class.new("to:[blank]") | ||
expect(qs.hash["to"]).to eq nil | ||
end | ||
|
||
it "handles dates with spaces" do | ||
qs = described_class.new("date: 2017-02-12 15:20") | ||
expect(qs.hash["date"]).to eq("2017-02-12 15:20") | ||
end | ||
|
||
it "returns an array for multiple items" do | ||
qs = described_class.new("to: [email protected] to: [email protected]") | ||
expect(qs.hash["to"]).to be_a(Array) | ||
expect(qs.hash["to"][0]).to eq "[email protected]" | ||
expect(qs.hash["to"][1]).to eq "[email protected]" | ||
end | ||
|
||
it "works with a z in the string" do | ||
qs = described_class.new("to: [email protected]") | ||
expect(qs.hash["to"]).to eq "[email protected]" | ||
end | ||
end |