Skip to content

Commit

Permalink
Don't escape ISO 8601 time query string parameters
Browse files Browse the repository at this point in the history
API will not process them when escaped
  • Loading branch information
sshaw committed Oct 31, 2024
1 parent ac4f5ac commit b316cc2
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 13 deletions.
20 changes: 13 additions & 7 deletions lib/big_commerce/management_api/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def with_in_param(options, *param_names)

next unless values.any?

options[in_name] = values.join(",")
options[in_name] = values
end

options
Expand All @@ -212,18 +212,24 @@ def with_in_param(options, *param_names)
private

def query_string(params)
params = params.dup
params.keys.each do |name|
value = params[name]
query = []

# We do this manually to join arrays and because time cannot be escaped as it will not be properly applied server-side
params.each do |name, value|
name = URI.encode_www_form_component(name)

if value.is_a?(Array)
params[name] = value.join(",")
value = value.join(",")
elsif value.respond_to?(:strftime)
params[name] = value.strftime("%Y-%m-%dT%H:%M:%S%z")
value = value.strftime("%Y-%m-%dT%H:%M:%S%z")
else
value = URI.encode_www_form_component(value)
end

query << "#{name}=#{value}"
end

sprintf("?%s", URI.encode_www_form(params))
sprintf("?%s", query.join("&"))
end

def endpoint(path)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 41 additions & 6 deletions spec/customers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
RSpec.describe BigCommerce::ManagementAPI::Customers, :vcr do
describe "#customers" do
describe "#create" do
before { @created = [] }
before { @created = [] }

after do
client.customers.delete(*@created) if @created.any?
end
after do
client.customers.delete(*@created) if @created.any?
end

describe "#customers" do
describe "#create" do
describe "creating a single customer" do
it "returns the new customer" do
result = client.customers.create(
Expand Down Expand Up @@ -72,6 +72,41 @@
)
end

describe ":date_created:min" do
it "returns customers created after the given date" do
# This is the time the VCR cassette was recorded
# If test is re-recorded switch use Time.now first. Other ways to do this?
# Can't freeze time on API server!

# after = Time.now
after = Time.parse("2024-10-30T22:30:38-0400")
result = client.customers.create(
{
:email => "test.customers.created.at.min1@example.com",
:first_name => "John",
:last_name => "Doe"
},
{
:email => "test.customers.created.at.min2@example.com",
:first_name => "Jane",
:last_name => "Doe"
}
)

@created.concat(result.map(&:id))

result = client.customers.get("date_created:min" => after)
expect(result.count).to eq 2

customers = result.sort_by(&:email)
expect(customers[0]).to be_a(BigCommerce::ManagementAPI::Customer)
expect(customers[0].email).to eq "test.customers.created.at.min1@example.com"

expect(customers[1]).to be_a(BigCommerce::ManagementAPI::Customer)
expect(customers[1].email).to eq "test.customers.created.at.min2@example.com"
end
end

describe ":id" do
context "given a single ID" do
it "returns the customer with the given ID" do
Expand Down

0 comments on commit b316cc2

Please sign in to comment.