porkbun 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd160032fe0fe5e7fdd9c899fcf253609f0104f0080ba73090edf9a4c01a5a97
4
- data.tar.gz: 1bd55b3b7cc3b3b564ff6836bd7d51666d32035a423f409ed1784ea22ff8c16b
3
+ metadata.gz: bfab2dd7301e980ef4e44f2db8579b6797cf18371863b295d904ba1129720b31
4
+ data.tar.gz: 18c9df86bbbf47ad1a7cf4d9c2805a63195f17ee87edb79abb95492ca82023af
5
5
  SHA512:
6
- metadata.gz: '07881fdc1d92373386a055e2dc732dd230d0b2bfa48f690793a623c0fede50a6438041bce3b1133eb0dd3ca8a8298ffbf4a70c79a857bff126c70accebd117a7'
7
- data.tar.gz: 502b183391a39ba249c38abb6829c22ea925646d512df3fa5cbdf0670ac8311b3583b4afa6fa4735426098ed909a0c9bfbbb72945d2c2b85e6e9953ec2cabb50
6
+ metadata.gz: 9fd355b2102b8ad2352e6e389c103877e3b4cea191d4d88420be4f6460755b30541ee67def037bed4b9f0192b5e6afa8b84bc5fd3df3e77059e37ec67ee8cd45
7
+ data.tar.gz: 1f1b61ce3edb87a0d846c8f7e5089d4d02619e9bf07334c8b9da93903a424650eed89416e057199a4250a72fa88590262d354b79069b7086ca3a10bebe9d1f2d
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
@@ -9,15 +9,50 @@ Reference: https://porkbun.com/api/json/v3/documentation
9
9
  ## Usage
10
10
 
11
11
  ```ruby
12
- Porkbun.API_KEY = 'YOUR_API_KEY'
13
- Porkbun.SECRET_KEY = 'YOUR_SECRET_KEY'
12
+ ENV['PORKBUN_API_KEY'] = 'YOUR_API_KEY'
13
+ ENV['PORKBUN_SECRET_API_KEY'] = 'YOUR_SECRET_API_KEY'
14
14
  record = Porkbun::DNS.create(name: 'test',
15
- type: 'A',
16
- content: '1.1.1.1',
17
- ttl: 300
15
+ type: 'A',
16
+ content: '1.1.1.1',
17
+ ttl: 300
18
18
  )
19
19
  ```
20
20
 
21
+ ## API
22
+
23
+ ### `Porkbun::Domain.ping`
24
+
25
+ Make sure your keys are good.
26
+
27
+ ### `Porkbun::DNS.retrieve(domain, id)`
28
+
29
+ Retrive all or specific record for a domain
30
+
31
+ ### `Porkbun::DNS.create(options)`
32
+
33
+ Create record for a domain
34
+
35
+ options:
36
+ - `name` - name of record. example `www`
37
+ - `type` - record type. example: CNAME
38
+ - `content` - content of record. example '1.1.1.1',
39
+ - `ttl` - time to live. example: 600. porkbun seems to have this as a minimum
40
+ - `prio` - record priority. mainly for MX records. example 10.
41
+
42
+ returns instance of DNS which can be used to delete
43
+
44
+ ## CLI
45
+
46
+ The gem also comes with a CLI
47
+
48
+ $ porkbun
49
+ Commands:
50
+ porkbun delete_all <domain> # deletes all records for a domain. this is destructive. use with caution
51
+ porkbun help [COMMAND] # Describe available commands or one specific command
52
+ porkbun import <file> # Import BIND zone file
53
+ porkbun list # List all domains
54
+ porkbun retrieve <domain> [<id>] # List all records for a domain
55
+
21
56
  ## Development
22
57
 
23
58
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -26,4 +61,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
26
61
 
27
62
  ## Contributing
28
63
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/danielb2/porkbun.
64
+ 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.1"
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.1
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