-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
357 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Luadns | ||
BASE_URL = 'https://api.luadns.com/v1' | ||
BASE_URL = 'https://api.luadns.com/v1' | ||
JSON_MIME = 'application/json' | ||
USER_AGENT = "luadns-ruby/#{VERSION}".freeze | ||
end | ||
|
||
require 'httparty' | ||
require_relative 'luadns/errors' | ||
require_relative 'luadns/schema' | ||
require_relative 'luadns/schema/base' | ||
require_relative 'luadns/schema/user' | ||
require_relative 'luadns/schema/zone' | ||
require_relative 'luadns/schema/record' | ||
require_relative 'luadns/response' | ||
require_relative 'luadns/http_client' | ||
require_relative 'luadns/client' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,99 @@ | ||
module Luadns | ||
class Client | ||
@@options = { | ||
format: :json, | ||
headers: { | ||
'Accept' => JSON_MIME, | ||
'User-Agent' => USER_AGENT | ||
} | ||
} | ||
|
||
attr_reader :base_url | ||
attr_reader :username | ||
attr_reader :password | ||
|
||
def initialize(username, password, base_url = Luadns::BASE_URL) | ||
@base_url = base_url | ||
def initialize(username, password, base_url = BASE_URL) | ||
@http_client = HttpClient.new | ||
|
||
@username = username | ||
@password = password | ||
@base_url = base_url | ||
end | ||
|
||
def me | ||
@http_client.get(endpoint('/users/me'), options) do |body| | ||
JSON.parse(body, object_class: Schema::User) | ||
end | ||
end | ||
|
||
def list_zones | ||
@http_client.get(endpoint('/zones'), options) do |body| | ||
JSON.parse(body, object_class: Schema::Zone) | ||
end | ||
end | ||
|
||
def create_zone(attrs) | ||
@http_client.post(endpoint('/zones'), attrs, options) do |body| | ||
JSON.parse(body, object_class: Schema::Zone) | ||
end | ||
end | ||
|
||
def get_zone(zone_id) | ||
@http_client.get(endpoint("/zones/#{zone_id}"), options) do |body| | ||
JSON.parse(body, object_class: Schema::Zone) | ||
end | ||
end | ||
|
||
def update_zone(zone_id, attrs) | ||
@http_client.put(endpoint("/zones/#{zone_id}"), attrs, options) do |body| | ||
JSON.parse(body, object_class: Schema::Zone) | ||
end | ||
end | ||
|
||
def delete_zone(zone_id) | ||
@http_client.delete(endpoint("/zones/#{zone_id}"), options) do |body| | ||
JSON.parse(body, object_class: Schema::Zone) | ||
end | ||
end | ||
|
||
def list_records(zone) | ||
@http_client.get(endpoint("/zones/#{zone.id}/records"), options) do |body| | ||
JSON.parse(body, object_class: Schema::Record) | ||
end | ||
end | ||
|
||
def create_record(zone, attrs) | ||
@http_client.post(endpoint("/zones/#{zone.id}/records"), attrs, options) do |body| | ||
JSON.parse(body, object_class: Schema::Record) | ||
end | ||
end | ||
|
||
def get_record(zone, record_id) | ||
@http_client.get(endpoint("/zones/#{zone.id}/records/#{record_id}"), options) do |body| | ||
JSON.parse(body, object_class: Schema::Record) | ||
end | ||
end | ||
|
||
def update_record(zone, record_id, attrs) | ||
@http_client.put(endpoint("/zones/#{zone.id}/records/#{record_id}"), attrs, options) do |body| | ||
JSON.parse(body, object_class: Schema::Record) | ||
end | ||
end | ||
|
||
def delete_record(zone, record_id) | ||
@http_client.delete(endpoint("/zones/#{zone.id}/records/#{record_id}"), options) do |body| | ||
JSON.parse(body, object_class: Schema::Record) | ||
end | ||
end | ||
|
||
private | ||
|
||
def endpoint(path) | ||
"#{@base_url}#{path}" | ||
end | ||
|
||
def options | ||
@@options.merge({ basic_auth: { username: @username, password: @password } }) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
module Luadns | ||
# class LuadnsError < RuntimeError; end | ||
class Error < StandardError; end | ||
class NotFoundError < Error; end | ||
|
||
class TooManyRequestsError < Error | ||
attr_reader :response | ||
attr_reader :reset | ||
|
||
def initialize(response) | ||
@response = response | ||
@reset = response.headers['X-Ratelimit-Reset'].to_i | ||
end | ||
end | ||
|
||
class RequestError < Error | ||
attr_reader :response | ||
|
||
def initialize(response) | ||
@response = response | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Luadns | ||
class HttpClient | ||
# Make a HTTP GET request. | ||
def get(url, options = {}) | ||
yield http_request(:get, url, nil, options) | ||
end | ||
|
||
# Make a HTTP POST request. | ||
def post(url, data = nil, options = {}) | ||
yield http_request(:post, url, data, options) | ||
end | ||
|
||
# Make a HTTP PUT request. | ||
def put(url, data = nil, options = {}) | ||
yield http_request(:put, url, data, options) | ||
end | ||
|
||
# Make a HTTP DELETE request. | ||
def delete(url, options = {}) | ||
yield http_request(:delete, url, nil, options) | ||
end | ||
|
||
private | ||
|
||
def http_request(method, url, data = nil, options = {}) | ||
content_type = options.dig(:headers, 'Content-Type') | ||
options.merge!(body: encode_body(data, content_type)) if data | ||
response = HTTParty.send(method, url, options) | ||
|
||
case response.code | ||
when 200 | ||
response.body | ||
when 404 | ||
raise NotFoundError, response | ||
when 429 | ||
raise TooManyRequestsError, response | ||
else | ||
raise RequestError, response | ||
end | ||
end | ||
|
||
def encode_body(data, content_type) | ||
content_type == JSON_MIME ? JSON.dump(data) : data | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Luadns | ||
class Response | ||
attr_reader :status_code | ||
attr_reader :body | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Luadns | ||
module Schema | ||
class Base | ||
def initialize(options = {}) | ||
options.each do |name, value| | ||
send("#{name}=", value) | ||
end | ||
end | ||
|
||
def []=(name, value) | ||
send("#{name}=", value) if respond_to?(name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.