porkbun 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd160032fe0fe5e7fdd9c899fcf253609f0104f0080ba73090edf9a4c01a5a97
4
- data.tar.gz: 1bd55b3b7cc3b3b564ff6836bd7d51666d32035a423f409ed1784ea22ff8c16b
3
+ metadata.gz: ece7707cda5e00c01e1f602dead38c53b0cfa7bc855bca379d1b49ed964d9edd
4
+ data.tar.gz: c6732f1843cc0a4e92875d8205160c35003c341abd34de9c08887aaedf0fd693
5
5
  SHA512:
6
- metadata.gz: '07881fdc1d92373386a055e2dc732dd230d0b2bfa48f690793a623c0fede50a6438041bce3b1133eb0dd3ca8a8298ffbf4a70c79a857bff126c70accebd117a7'
7
- data.tar.gz: 502b183391a39ba249c38abb6829c22ea925646d512df3fa5cbdf0670ac8311b3583b4afa6fa4735426098ed909a0c9bfbbb72945d2c2b85e6e9953ec2cabb50
6
+ metadata.gz: 9d3dcde1ed269afa5074308b79e0640be5982655e365d6586ab4f88b4b90db0a17471b21817e28a2f1addbaa2b17a015522ee87f29afe0043306fc3592a66725
7
+ data.tar.gz: 29a28d2cd653560dfb169ecb835a44c5f85f6ba7ffe5e05a910a8a503ea470fbee559681a266387219b81524c93aa2447ab1c07c86db90695f7e45f323bdc14d
data/Gemfile CHANGED
@@ -12,3 +12,5 @@ gem "rspec", "~> 3.12", :group => :test
12
12
  gem "webmock", "~> 3.19", :group => :test
13
13
 
14
14
  gem "pry", "~> 0.14.2", :group => :test
15
+
16
+ gem "thor", "~> 1.2"
data/README.md CHANGED
@@ -10,7 +10,7 @@ Reference: https://porkbun.com/api/json/v3/documentation
10
10
 
11
11
  ```ruby
12
12
  Porkbun.API_KEY = 'YOUR_API_KEY'
13
- Porkbun.SECRET_KEY = 'YOUR_SECRET_KEY'
13
+ Porkbun.SECRET_API_KEY = 'YOUR_SECRET_API_KEY'
14
14
  record = Porkbun::DNS.create(name: 'test',
15
15
  type: 'A',
16
16
  content: '1.1.1.1',
@@ -26,4 +26,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
26
26
 
27
27
  ## Contributing
28
28
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/danielb2/porkbun.
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/danielb2/porkbun-ruby.
data/bin/porkbun ADDED
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'porkbun'
5
+ require 'thor'
6
+
7
+ class CLI < Thor
8
+ desc 'import <file>', 'Import BIND zone file'
9
+ def import(file)
10
+ record_regex = /^(?<hostname>[^\s]+)\.\s+(?<ttl>\d+)\s+IN\s+(?<type>[^\s]+)\s*(?<priority>\d+)?\s+(?<content>.+)$/
11
+ IO.readlines(file).each do |line|
12
+ match_data = line.match(record_regex)
13
+ next unless match_data
14
+
15
+ domain = match_data[:hostname].split('.')[-2..].join('.').chomp '.'
16
+ name = match_data[:hostname].split('.')[0..-3].join('.')
17
+
18
+ options = {
19
+ domain:,
20
+ name:,
21
+ ttl: match_data[:ttl],
22
+ type: match_data[:type],
23
+ prio: match_data[:priority],
24
+ content: match_data[:content].chomp('.').gsub(/^"|"$/, '')
25
+ }.compact
26
+
27
+ record = Porkbun::DNS.create options
28
+ pp record
29
+ end
30
+ end
31
+
32
+ desc 'list', 'List all domains'
33
+ def list
34
+ Porkbun::Domain.list_all[:domains].each do |domain|
35
+ puts domain[:domain]
36
+ end
37
+ end
38
+
39
+ desc 'delete_all <domain>', 'deletes all records for a domain. this is destructive. use with caution'
40
+ def delete_all(domain, id = '')
41
+ records = get_all(domain, id)
42
+ records.each do |record|
43
+ next if record.type == 'NS'
44
+
45
+ puts "DELETE #{record}"
46
+ record.delete
47
+ end
48
+ end
49
+
50
+ desc 'retrieve <domain> [<id>]', 'List all records for a domain'
51
+ def retrieve(domain, id = '')
52
+ records = get_all(domain, id)
53
+ puts records
54
+ end
55
+
56
+ def self.exit_on_failure?
57
+ exit 1
58
+ end
59
+
60
+ private
61
+
62
+ def get_all(domain, id = '')
63
+ records = Porkbun::DNS.retrieve(domain, id)
64
+ if records.instance_of?(Porkbun::Error)
65
+ puts records.message
66
+ exit 1
67
+ end
68
+ records
69
+ end
70
+ end
71
+
72
+ CLI.start(ARGV)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Porkbun
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/porkbun.rb CHANGED
@@ -7,7 +7,6 @@ module Porkbun
7
7
  class Error < StandardError; end
8
8
 
9
9
  def self.porkbun(path, options = {})
10
- pp path
11
10
  res = HTTP.post File.join('https://porkbun.com/api/json/v3', path), json: {
12
11
  secretapikey: ENV.fetch('PORKBUN_SECRET_API_KEY', nil),
13
12
  apikey: ENV.fetch('PORKBUN_API_KEY', nil)
@@ -65,6 +64,8 @@ module Porkbun
65
64
  raise Error, 'need domain' unless domain
66
65
 
67
66
  res = Porkbun.porkbun File.join('dns/retrieve', domain, id || '').chomp('/')
67
+ return Error.new(res[:message]) if res[:status] == 'ERROR'
68
+
68
69
  res[:records].map do |record|
69
70
  DNS.new record.merge(domain:)
70
71
  end
@@ -78,13 +79,30 @@ module Porkbun
78
79
  self
79
80
  end
80
81
 
82
+ require 'pry'
83
+ def to_s
84
+ content_str = case type
85
+ when /TXT|SPF/
86
+ "\"#{content}\""
87
+ when /MX|CNAME|NS/
88
+ "#{content}."
89
+ else
90
+ String(content)
91
+ end
92
+
93
+ prio_str = prio == '0' ? '' : prio
94
+ "#{name}. #{ttl} IN #{type} #{prio_str} #{content_str}".tr_s(' ', ' ')
95
+ end
96
+
81
97
  def create
82
- res = Porkbun.porkbun File.join('dns/create', domain), {
98
+ options = {
83
99
  name:,
84
100
  content:,
85
101
  type:,
86
102
  ttl:
87
103
  }
104
+ options.merge!(prio:) if prio
105
+ res = Porkbun.porkbun File.join('dns/create', domain), options
88
106
  parse_response res
89
107
  @id = res[:id]
90
108
  self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: porkbun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bretoi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-20 00:00:00.000000000 Z
11
+ date: 2023-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -27,7 +27,8 @@ dependencies:
27
27
  description: Porkbun API wrapper for Ruby.
28
28
  email:
29
29
  - daniel@otherware.org
30
- executables: []
30
+ executables:
31
+ - porkbun
31
32
  extensions: []
32
33
  extra_rdoc_files: []
33
34
  files:
@@ -35,6 +36,7 @@ files:
35
36
  - Gemfile
36
37
  - README.md
37
38
  - Rakefile
39
+ - bin/porkbun
38
40
  - lib/porkbun.rb
39
41
  - lib/porkbun/version.rb
40
42
  - sig/porkbun.rbs