Skip to content

Commit

Permalink
fix: Include parent resource in pagination calls
Browse files Browse the repository at this point in the history
Use the next and previous `_links` in the pagination logic.

Previously, pagination calls to a nested resource were incorrectly scoped.
For, example:

  payments = customer.payments(limit: 2)
  payments.next

The first line correctly retrieves:

  /v2/customers/cst_8wmqcHMN4U/payments?from=tr_3&limit=2

The second line incorrectly called:

  /v2/payments?from=tr_3&limit=2 (customer scope omitted)

Resolves #144
  • Loading branch information
justincase committed Feb 28, 2021
1 parent 31a47aa commit 506d52e
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/mollie/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def next(options = {})
href = URI.parse(links['next']['href'])
query = URI.decode_www_form(href.query).to_h

klass.all(options.merge(query))
response = Mollie::Client.instance.perform_http_call('GET', links['next']['href'], nil, {}, options.merge(query))
self.class.new(response, klass)
end

def previous(options = {})
Expand All @@ -47,7 +48,8 @@ def previous(options = {})
href = URI.parse(links['previous']['href'])
query = URI.decode_www_form(href.query).to_h

klass.all(options.merge(query))
response = Mollie::Client.instance.perform_http_call('GET', links['previous']['href'], nil, {}, options.merge(query))
self.class.new(response, klass)
end
end
end
36 changes: 36 additions & 0 deletions test/fixtures/customer/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"resource": "customer",
"id": "cst_8wmqcHMN4U",
"mode": "test",
"name": "Customer A",
"email": "[email protected]",
"locale": "nl_NL",
"metadata": null,
"createdAt": "2018-04-06T13:23:21.0Z",
"_links": {
"self": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U",
"type": "application/hal+json"
},
"dashboard": {
"href": "https://www.mollie.com/dashboard/org_123456789/customers/cst_8wmqcHMN4U",
"type": "text/html"
},
"mandates": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/mandates",
"type": "application/hal+json"
},
"subscriptions": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/subscriptions",
"type": "application/hal+json"
},
"payments": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments",
"type": "application/hal+json"
},
"documentation": {
"href": "https://docs.mollie.com/reference/v2/customers-api/get-customer",
"type": "text/html"
}
}
}
30 changes: 30 additions & 0 deletions test/fixtures/customer/list-payments-next.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"count": 2,
"_embedded": {
"payments": [
{
"resource": "payment",
"id":"tr_3"
},
{
"resource": "payment",
"id":"tr_4"
}
]
},
"_links": {
"documentation": {
"href": "https://docs.mollie.com/reference/v2/customers-api/list-customer-payments",
"type": "text/html"
},
"self": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_3&limit=2",
"type": "application/hal+json"
},
"previous": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_1&limit=2",
"type": "application/hal+json"
},
"next": null
}
}
30 changes: 30 additions & 0 deletions test/fixtures/customer/list-payments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"count": 2,
"_embedded": {
"payments": [
{
"resource": "payment",
"id":"tr_1"
},
{
"resource": "payment",
"id":"tr_2"
}
]
},
"_links": {
"documentation": {
"href": "https://docs.mollie.com/reference/v2/customers-api/list-customer-payments",
"type": "text/html"
},
"self": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?limit=2",
"type": "application/hal+json"
},
"previous": null,
"next": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_3&limit=2",
"type": "application/hal+json"
}
}
}
60 changes: 60 additions & 0 deletions test/mollie/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,65 @@ def test_previous_page
list = Mollie::List.new(attributes, Payment)
assert_equal 2, list.previous.count
end

def test_pagination_for_nested_resources
stub_request(:get, 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U')
.to_return(
status: 200,
body: read_fixture('customer/get.json'),
headers: {}
)

# First page of customer payments
stub_request(:get, 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?limit=2')
.to_return(
status: 200,
body: read_fixture('customer/list-payments.json'),
headers: {}
)

# Second page
stub_request(:get, 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_3&limit=2')
.to_return(
status: 200,
body: read_fixture('customer/list-payments-next.json'),
headers: {}
)

# Previous page
stub_request(:get, 'https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_1&limit=2')
.to_return(
status: 200,
body: read_fixture('customer/list-payments.json'),
headers: {}
)

customer = Mollie::Customer.get('cst_8wmqcHMN4U')

# First page of customer payments
payments = customer.payments(limit: 2)

assert_equal ["tr_1", "tr_2"], payments.map { |p| p.id }
assert_equal "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_3&limit=2",
payments.links["next"]["href"]
assert_equal nil, payments.links["previous"]

# Second page
payments = payments.next

assert_equal ["tr_3", "tr_4"], payments.map { |p| p.id }
assert_equal nil, payments.links["next"]
assert_equal "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_1&limit=2",
payments.links["previous"]["href"]

# Previous page
payments = payments.previous

assert_equal ["tr_1", "tr_2"], payments.map { |p| p.id }
assert_equal "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U/payments?from=tr_3&limit=2",
payments.links["next"]["href"]
assert_equal nil, payments.links["previous"]
end

end
end

0 comments on commit 506d52e

Please sign in to comment.