Skip to content

Commit

Permalink
feat: support for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Apr 15, 2021
1 parent fe8e464 commit 975a39a
Show file tree
Hide file tree
Showing 89 changed files with 1,194 additions and 590 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ group = client.group('some-ref', field: :external_reference)
# Get a list of zones within a group
zones = group.zones

# Get pagination details for the groups
zones.pagination.current_page
zones.pagination.total
zones.pagination.per_page
zones.pagination.total_pages

# Get a zone by its ID
zone = client.zone(123)

# Get a zone by its external reference
zone = client.zone('some-ref', field: :external_reference)

# Get all the records for a zone
# Get all the records for a zone (paginated)
zone.records
zone.records(page: 2)
zone.records(per_page: 50)

# Get specific type of records for a zone
zone.records(type: 'A')
Expand All @@ -45,6 +53,9 @@ group = client.create_group(name: 'My example group', external_reference: 'some-
# Return an array of nameservers assigned to a group
group.nameservers

# Get all tagged records for a group
group.tagged_records(['tag1'])

# Create a new zone
zone = group.create_zone('example.com')

Expand All @@ -56,4 +67,7 @@ record.delete

# Update a record
record.update(content: { ip_address: '185.22.208.5'} )

# Get all records with any of the given tags
client.tagged_records(['tag1'])
```
4 changes: 4 additions & 0 deletions lib/dennis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def record(id, field: :id)
Record.find_by(self, field, id)
end

def tagged_records(tags, group: nil)
Record.all_by_tag(self, tags, group: group)
end

def nameservers
Nameserver.all(self)
end
Expand Down
14 changes: 11 additions & 3 deletions lib/dennis/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ class Group

class << self

def all(client)
groups = client.api.perform(:get, 'groups')
groups.hash['groups'].map { |hash| new(client, hash) }
def all(client, page: nil, per_page: nil)
request = client.api.create_request(:get, 'groups')
request.arguments[:page] = page if page
request.arguments[:per_page] = per_page if per_page
PaginatedArray.create(request.perform.hash, 'groups') do |hash|
new(client, hash)
end
end

def find_by(client, field, value)
Expand Down Expand Up @@ -82,6 +86,10 @@ def zones
Zone.all_for_group(@client, { id: id })
end

def tagged_records(tags)
Record.all_by_tag(@client, tags, group: { id: id })
end

def update(properties)
req = @client.api.create_request(:patch, 'groups/:group')
req.arguments['group'] = { id: id }
Expand Down
22 changes: 22 additions & 0 deletions lib/dennis/paginated_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'dennis/pagination'

module Dennis
class PaginatedArray < Array

attr_accessor :pagination

class << self

def create(result, key, &block)
result_hash = result[key]
array = new(result_hash.map(&block))
array.pagination = Pagination.new(result['pagination'])
array
end

end

end
end
31 changes: 31 additions & 0 deletions lib/dennis/pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Dennis
class Pagination

def initialize(hash)
@hash = hash
end

def current_page
@hash['current_page']
end

def total_pages
@hash['total_pages']
end

def total
@hash['total']
end

def per_page
@hash['per_page']
end

def large_set?
@hash['large_set']
end

end
end
8 changes: 6 additions & 2 deletions lib/dennis/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ class Record

class << self

def all(client, zone, type: nil, name: nil, query: nil, tags: nil)
def all(client, zone, type: nil, name: nil, query: nil, tags: nil, page: nil, per_page: nil)
request = client.api.create_request(:get, 'zones/:zone/records')
request.arguments[:zone] = zone
request.arguments[:name] = name if name
request.arguments[:type] = type if type
request.arguments[:query] = query if query
request.arguments[:tags] = tags if tags
request.perform.hash['records'].map { |hash| new(client, hash) }
request.arguments[:page] = page if page
request.arguments[:per_page] = per_page if per_page
PaginatedArray.create(request.perform.hash, 'records') do |hash|
new(client, hash)
end
end

def all_by_tag(client, tags, group: nil)
Expand Down
17 changes: 13 additions & 4 deletions lib/dennis/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
require 'dennis/validation_error'
require 'dennis/group_not_found_error'
require 'dennis/record'
require 'dennis/paginated_array'

module Dennis
class Zone

class << self

def all(client, query: nil, view: nil)
def all(client, query: nil, view: nil, page: nil, per_page: nil)
request = client.api.create_request(:get, 'zones')
request.arguments[:query] = query if query
request.arguments[:view] = view if view
request.perform.hash['zones'].map { |hash| new(client, hash) }
request.arguments[:page] = page if page
request.arguments[:per_page] = per_page if per_page
PaginatedArray.create(request.perform.hash, 'zones') do |hash|
new(client, hash)
end
end

def all_for_group(client, group, query: nil, view: nil)
def all_for_group(client, group, query: nil, view: nil, page: nil, per_page: nil)
request = client.api.create_request(:get, 'groups/:group/zones')
request.arguments[:group] = group
request.arguments[:query] = query if query
request.arguments[:view] = view if view
request.perform.hash['zones'].map { |hash| new(client, hash) }
request.arguments[:page] = page if page
request.arguments[:per_page] = per_page if per_page
PaginatedArray.create(request.perform.hash, 'zones') do |hash|
new(client, hash)
end
end

def find_by(client, field, value)
Expand Down
8 changes: 4 additions & 4 deletions spec/fixtures/vcr_cassettes/client.yml

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

49 changes: 49 additions & 0 deletions spec/fixtures/vcr_cassettes/group-all-page.yml

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

49 changes: 49 additions & 0 deletions spec/fixtures/vcr_cassettes/group-all-per-page.yml

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

21 changes: 11 additions & 10 deletions spec/fixtures/vcr_cassettes/group-all.yml

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

8 changes: 4 additions & 4 deletions spec/fixtures/vcr_cassettes/group-create-validation-error.yml

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

Loading

0 comments on commit 975a39a

Please sign in to comment.