Skip to content

Commit

Permalink
feat: zone management
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Mar 8, 2021
1 parent cd4c7ae commit 759abae
Show file tree
Hide file tree
Showing 32 changed files with 1,116 additions and 99 deletions.
9 changes: 9 additions & 0 deletions lib/dennis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'rapid_api'
require 'dennis/group'
require 'dennis/zone'

module Dennis
class Client
Expand Down Expand Up @@ -30,5 +31,13 @@ def create_group(**opts)
Group.create(self, **opts)
end

def zones
Zone.all(self)
end

def zone(id, field: :id)
Zone.find_by(self, field, id)
end

end
end
4 changes: 4 additions & 0 deletions lib/dennis/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def nameservers
end
end

def create_zone(**options)
Zone.create(@client, group: { id: id }, **options)
end

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

require 'dennis/error'

module Dennis
class GroupNotFoundError < Error
end
end
75 changes: 75 additions & 0 deletions lib/dennis/zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

require 'dennis/validation_error'
require 'dennis/group_not_found_error'

module Dennis
class Zone

class << self

def all(client)
groups = client.api.perform(:get, 'zones')
groups.hash['zones'].map { |hash| new(client, hash) }
end

def find_by(client, field, value)
request = client.api.create_request(:get, 'zones/:zone')
request.arguments[:zone] = { field => value }
new(client, request.perform.hash['zone'])
rescue RapidAPI::RequestError => e
e.code == 'zone_not_found' ? nil : raise
end

def create(client, group:, name:, external_reference: nil)
request = client.api.create_request(:post, 'zones')
request.arguments[:group] = group
request.arguments[:properties] = { name: name, external_reference: external_reference }
new(client, request.perform.hash['zone'])
rescue RapidAPI::RequestError => e
raise GroupNotFoundError if e.code == 'group_not_found'
raise ValidationError, e.detail['errors'] if e.code == 'validation_error'

raise
end

end

def initialize(client, hash)
@client = client
@hash = hash
end

def id
@hash['id']
end

def name
@hash['name']
end

def external_reference
@hash['external_reference']
end

def update(properties)
req = @client.api.create_request(:patch, 'zones/:zone')
req.arguments['zone'] = { id: id }
req.arguments['properties'] = properties
@hash = req.perform.hash['zone']
true
rescue RapidAPI::RequestError => e
raise ValidationError, e.detail['errors'] if e.code == 'validation_error'

raise
end

def delete
req = @client.api.create_request(:delete, 'zones/:zone')
req.arguments['zone'] = { id: id }
req.perform
true
end

end
end
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.

18 changes: 10 additions & 8 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.

12 changes: 6 additions & 6 deletions spec/fixtures/vcr_cassettes/group-create.yml

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

10 changes: 5 additions & 5 deletions spec/fixtures/vcr_cassettes/group-delete-verify.yml

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

Loading

0 comments on commit 759abae

Please sign in to comment.