Skip to content

Commit

Permalink
Enhance Dnsimple::Options::ListOptions to support pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jun 21, 2016
1 parent 5347a64 commit a53fe70
Show file tree
Hide file tree
Showing 23 changed files with 211 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/dnsimple/client/accounts.rb
Expand Up @@ -10,6 +10,8 @@ module Accounts
# client.accounts.list
#
# @param [Hash] options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @return [Dnsimple::Response<Dnsimple::Struct::Account>]
#
# @raise [Dnsimple::RequestError]
Expand Down
6 changes: 5 additions & 1 deletion lib/dnsimple/client/contacts.rb
Expand Up @@ -11,13 +11,15 @@ module Contacts
# client.contacts.list(1010)
#
# @example List contacts, provide a specific page
# client.contacts.list(1010, query: { page: 2 })
# client.contacts.list(1010, page: 2)
#
# @example List contacts, provide a sorting policy
# client.contacts.list(1010, sort: "email:asc")
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Contact>]
#
Expand All @@ -43,6 +45,8 @@ def contacts(account_id, options = {})
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Contact>]
#
Expand Down
6 changes: 5 additions & 1 deletion lib/dnsimple/client/domains.rb
Expand Up @@ -11,7 +11,7 @@ module Domains
# client.domains.list(1010)
#
# @example List domains, provide a specific page
# client.domains.list(1010, query: { page: 2 })
# client.domains.list(1010, page: 2)
#
# @example List domains, provide a sorting policy
# client.domains.list(1010, sort: "expires_on:asc")
Expand All @@ -21,6 +21,8 @@ module Domains
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @option options [Hash] :filter filtering policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Domain>]
Expand All @@ -47,6 +49,8 @@ def domains(account_id, options = {})
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting option
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @option options [Hash] :filter filtering policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Domain>]
Expand Down
6 changes: 5 additions & 1 deletion lib/dnsimple/client/domains_email_forwards.rb
Expand Up @@ -10,14 +10,16 @@ module DomainsEmailForwards
# client.domains.email_forwards(1010, "example.com")
#
# @example List email forwards, provide a specific page
# client.domains.email_forwards(1010, "example.com", query: { page: 2 })
# client.domains.email_forwards(1010, "example.com", page: 2)
#
# @example List email forwards, provide a sorting policy
# client.domains.email_forwards(1010, "example.com", sort: "from:asc")
#
# @param [Fixnum] account_id the account ID
# @param [#to_s] domain_id The domain ID or domain name
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::EmailForward>]
#
Expand All @@ -43,6 +45,8 @@ def email_forwards(account_id, domain_id, options = {})
# @param [Fixnum] account_id the account ID
# @param [#to_s] domain_id The domain ID or domain name
# @param [Hash] options the filtering and sorting option
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::EmailForward>]
#
Expand Down
7 changes: 7 additions & 0 deletions lib/dnsimple/client/services.rb
Expand Up @@ -9,10 +9,15 @@ module Services
# @example List one-click services:
# client.services.list_services
#
# @example List one-click services, provide a specific page:
# client.services.list_services(page: 2)
#
# @example List one-click services, provide a sorting policy:
# client.services.list_services(sort: "short_name:asc")
#
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Service>]
#
Expand Down Expand Up @@ -41,6 +46,8 @@ def services(options = {})
# @see #services
#
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Service>]
#
Expand Down
7 changes: 7 additions & 0 deletions lib/dnsimple/client/templates.rb
Expand Up @@ -9,11 +9,16 @@ module Templates
# @example List the templates for account 1010:
# client.templates.list_templates(1010)
#
# @example List the templates for account 1010, provide a specific page:
# client.templates.list_templates(1010, page: 2)
#
# @example List the templates for account 1010, provide sorting policy:
# client.templates.list_templates(1010, sort: "short_name:asc")
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Template>]
#
Expand Down Expand Up @@ -43,6 +48,8 @@ def templates(account_id, options = {})
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Template>]
#
Expand Down
6 changes: 5 additions & 1 deletion lib/dnsimple/client/templates_records.rb
Expand Up @@ -11,14 +11,16 @@ module TemplatesRecords
# client.templates.records(1010, "alpha")
#
# @example List records for the template "alpha", providing a specific page
# client.templates.records(1010, "alpha", query: { page: 2 })
# client.templates.records(1010, "alpha", page: 2)
#
# @example List records for the template "alpha", providing sorting policy
# client.templates.records(1010, "alpha", sort: "type:asc")
#
# @param [Fixnum] account_id the account ID
# @param [String] template_id the template name
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::TemplateRecord>]
#
Expand Down Expand Up @@ -49,6 +51,8 @@ def records(account_id, template_id, options = {})
# @param [Fixnum] account_id the account ID
# @param [String] template_id the template name
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::TemplateRecord>]
#
Expand Down
6 changes: 5 additions & 1 deletion lib/dnsimple/client/tlds.rb
Expand Up @@ -9,12 +9,14 @@ module Tlds
# client.tlds.list
#
# @example List TLDs, providing a specific page
# client.tlds.list(query: { page: 2 })
# client.tlds.list(page: 2)
#
# @example List TLDs, providing sorting policy
# client.tlds.list(sort: "tld:asc")
#
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Tld>]
#
Expand Down Expand Up @@ -42,6 +44,8 @@ def tlds(options = {})
# client.tlds.all
#
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Tld>]
#
Expand Down
5 changes: 5 additions & 0 deletions lib/dnsimple/client/webhooks.rb
Expand Up @@ -9,11 +9,16 @@ module Webhooks
# @example List all webhooks
# client.webhooks.list(1010)
#
# @example List all webhooks, provide a specific page
# client.webhooks.list(1010, page: 2)
#
# @example List all webhooks, provide sorting policy
# client.webhooks.list(1010, sort: "id:asc")
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Webhook>]
#
Expand Down
6 changes: 5 additions & 1 deletion lib/dnsimple/client/zones.rb
Expand Up @@ -11,7 +11,7 @@ module Zones
# client.zones.list(1010, "example.com")
#
# @example List zones, provide a specific page
# client.zones.list(1010, "example.com", query: { page: 2 })
# client.zones.list(1010, "example.com", page: 2)
#
# @example List zones, provide sorting policy
# client.zones.list(1010, "example.com", sort: "name:desc")
Expand All @@ -21,6 +21,8 @@ module Zones
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @option options [Hash] :filter filtering policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Zone>]
Expand All @@ -47,6 +49,8 @@ def zones(account_id, options = {})
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @option options [Hash] :filter filtering policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Zone>]
Expand Down
6 changes: 5 additions & 1 deletion lib/dnsimple/client/zones_records.rb
Expand Up @@ -11,7 +11,7 @@ module ZonesRecords
# client.zones.records(1010, "example.com")
#
# @example List records for the zone "example.com", provide a specific page
# client.zones.records(1010, "example.com", query: { page: 2 })
# client.zones.records(1010, "example.com", page: 2)
#
# @example List records for the zone "example.com", provide sorting policy
# client.zones.records(1010, "example.com", sort: "type:asc")
Expand All @@ -22,6 +22,8 @@ module ZonesRecords
# @param [Fixnum] account_id the account ID
# @param [String] zone_id the zone name
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @option options [Hash] :filter filtering policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Record>]
Expand Down Expand Up @@ -53,6 +55,8 @@ def records(account_id, zone_id, options = {})
# @param [Fixnum] account_id the account ID
# @param [String] zone_id the zone name
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @option options [Hash] :filter filtering policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Record>]
Expand Down
9 changes: 9 additions & 0 deletions lib/dnsimple/options.rb
Expand Up @@ -15,6 +15,7 @@ class ListOptions < Base
def initialize(options)
super
_prepare_query
_prepare_pagination
_prepare_sort
_prepare_filter
end
Expand All @@ -25,6 +26,14 @@ def _prepare_query
@options[:query] ||= {} if @options.any?
end

def _prepare_pagination
@page = @options.delete(:page)
_merge(page: @page) unless @page.nil?

@per_page = @options.delete(:per_page)
_merge(per_page: @per_page) unless @per_page.nil?
end

def _prepare_sort
@sort = @options.delete(:sort)
_merge(sort: @sort) unless @sort.nil?
Expand Down
2 changes: 1 addition & 1 deletion spec/dnsimple/client/contacts_spec.rb
Expand Up @@ -21,7 +21,7 @@
end

it "supports pagination" do
subject.contacts(account_id, query: { page: 2 })
subject.contacts(account_id, page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/contacts?page=2")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dnsimple/client/domains_email_forwards_spec.rb
Expand Up @@ -22,7 +22,7 @@
end

it "supports pagination" do
subject.email_forwards(account_id, domain_id, query: { page: 2 })
subject.email_forwards(account_id, domain_id, page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/domains/#{domain_id}/email_forwards?page=2")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dnsimple/client/domains_spec.rb
Expand Up @@ -21,7 +21,7 @@
end

it "supports pagination" do
subject.domains(account_id, query: { page: 2 })
subject.domains(account_id, page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/domains?page=2")
end
Expand Down
6 changes: 6 additions & 0 deletions spec/dnsimple/client/services_spec.rb
Expand Up @@ -18,6 +18,12 @@
with(headers: { "Accept" => "application/json" })
end

it "supports pagination" do
subject.services(page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/services?page=2")
end

it "supports extra request options" do
subject.services(query: { foo: "bar" })

Expand Down
6 changes: 6 additions & 0 deletions spec/dnsimple/client/templates_records_spec.rb
Expand Up @@ -21,6 +21,12 @@
with(headers: { "Accept" => "application/json" })
end

it "supports pagination" do
subject.records(account_id, template_id, page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/templates/#{template_id}/records?page=2")
end

it "supports extra request options" do
subject.records(account_id, template_id, query: { foo: "bar" })

Expand Down
6 changes: 6 additions & 0 deletions spec/dnsimple/client/templates_spec.rb
Expand Up @@ -20,6 +20,12 @@
with(headers: { "Accept" => "application/json" })
end

it "supports pagination" do
subject.templates(account_id, page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/templates?page=2")
end

it "supports extra request options" do
subject.templates(account_id, query: { foo: "bar" })

Expand Down
2 changes: 1 addition & 1 deletion spec/dnsimple/client/tlds_spec.rb
Expand Up @@ -17,7 +17,7 @@
end

it "supports pagination" do
subject.tlds(query: { page: 2 })
subject.tlds(page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/tlds?page=2")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dnsimple/client/zones_records_spec.rb
Expand Up @@ -22,7 +22,7 @@
end

it "supports pagination" do
subject.records(account_id, zone_id, query: { page: 2 })
subject.records(account_id, zone_id, page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/zones/#{zone_id}/records?page=2")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dnsimple/client/zones_spec.rb
Expand Up @@ -21,7 +21,7 @@
end

it "supports pagination" do
subject.zones(account_id, query: { page: 2 })
subject.zones(account_id, page: 2)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/zones?page=2")
end
Expand Down

0 comments on commit a53fe70

Please sign in to comment.