czds 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -0
- data/lib/czds/cli/main.rb +63 -0
- data/lib/czds/client.rb +113 -0
- data/lib/czds/configuration.rb +19 -0
- data/lib/czds/version.rb +5 -0
- data/lib/czds/zone_file/status.rb +18 -0
- data/lib/czds/zone_file.rb +27 -0
- data/lib/czds.rb +12 -0
- metadata +11 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bad75046d0a8d1900d29e4a949a7527749419b657ccec1f112db7f0b915c0086
|
4
|
+
data.tar.gz: 85cf821a7467c8c6645600d41d3691f793faad3c592a5c47d40b398527802c3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a589c30e9ad8e7597433a3579839db51ac2cac0949d4baeeb1a51ae0258bbd5a67d3f239a7bb982e46aff3f6aeff34c2bdc93352de35874fdb105e8faeb01dc
|
7
|
+
data.tar.gz: e259a4e46235c252eb71527cd793b1d57df6e8458a17082de5256aa5da31cd892ca7d68d229fc543ca07586860ec9f81132a6f50bfc5abf6ef2ed66c9a73eff0
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Czds
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/czds`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
+
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/czds.
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module CZDS
|
5
|
+
module CLI
|
6
|
+
class Main < Thor
|
7
|
+
attr_reader :tld
|
8
|
+
|
9
|
+
desc "download", "Download zone file"
|
10
|
+
option :config_file, type: :string, default: './czds.config.json', desc: 'Path to config file'
|
11
|
+
option :tld, type: :string, desc: 'Top-level domain, default: com'
|
12
|
+
option :download_dir, type: :string, desc: 'Directory to download zone file, default: ./'
|
13
|
+
option :no_timestamp, type: :boolean, desc: 'Disable timestamp in file name'
|
14
|
+
option :username, type: :string, desc: 'CZDS username'
|
15
|
+
option :password, type: :string, desc: 'CZDS password'
|
16
|
+
|
17
|
+
def download
|
18
|
+
config_file = load_config(options[:config_file])
|
19
|
+
configure_client(options, config_file)
|
20
|
+
|
21
|
+
tld = options[:tld] || config_file['tld'] || CZDS::Configuration::DEFAULTS[:tld]
|
22
|
+
|
23
|
+
zone_file = CZDS::ZoneFile.new(tld)
|
24
|
+
zone_file.download
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "status", "Show zone file status"
|
28
|
+
option :config_file, type: :string, default: './czds.config.json', desc: 'Path to config file'
|
29
|
+
option :tld, type: :string, desc: 'Top-level domain, default: com'
|
30
|
+
option :no_timestamp, type: :boolean, desc: 'Disable timestamp in file name'
|
31
|
+
option :username, type: :string, desc: 'CZDS username'
|
32
|
+
option :password, type: :string, desc: 'CZDS password'
|
33
|
+
|
34
|
+
def status
|
35
|
+
config_file = load_config(options[:config_file])
|
36
|
+
configure_client(options, config_file)
|
37
|
+
|
38
|
+
tld = options[:tld] || config_file['tld'] || CZDS::Configuration::DEFAULTS[:tld]
|
39
|
+
|
40
|
+
zone_file = CZDS::ZoneFile.new(tld)
|
41
|
+
puts zone_file.status.to_h
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def configure_client(options, config_file)
|
47
|
+
CZDS::Client.configure do |config|
|
48
|
+
config.username = options[:username] || config_file['username']
|
49
|
+
config.password = options[:password] || config_file['password']
|
50
|
+
config.download_dir = options[:download_dir] || config_file['download_dir'] || CZDS::Configuration::DEFAULTS[:download_dir]
|
51
|
+
config.timestamp_file_name = ![options[:no_timestamp], config_file['no_timestamp']].compact[0]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def load_config(config_file)
|
56
|
+
return {} unless File.exist?(config_file)
|
57
|
+
|
58
|
+
JSON.parse(File.read(config_file))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/lib/czds/client.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'forwardable'
|
5
|
+
require_relative './configuration'
|
6
|
+
require_relative './zone_file/status'
|
7
|
+
|
8
|
+
module CZDS
|
9
|
+
class Client
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
@config = Configuration.new
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :config
|
16
|
+
|
17
|
+
def configure
|
18
|
+
yield(config) if block_given?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :zone_file_statuses
|
23
|
+
|
24
|
+
def_delegators :@config, :username, :password, :download_dir, :timestamp_file_name, :user_agent
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@zone_file_statuses = {}
|
28
|
+
@config = self.class.config
|
29
|
+
|
30
|
+
raise NoCredentialsError if @config.username.nil? || @config.password.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
def connection(url)
|
34
|
+
Faraday.new(
|
35
|
+
url:,
|
36
|
+
headers: {
|
37
|
+
'Authorization' => "Bearer #{access_token}",
|
38
|
+
'User-Agent' => user_agent,
|
39
|
+
})
|
40
|
+
end
|
41
|
+
|
42
|
+
def zone_file_status(tld)
|
43
|
+
return zone_file_statuses[tld] unless zone_file_statuses[tld].nil?
|
44
|
+
|
45
|
+
conn = connection(download_link(tld))
|
46
|
+
|
47
|
+
response = conn.head
|
48
|
+
|
49
|
+
headers = response.headers.slice('content-disposition', 'content-length', 'last-modified')
|
50
|
+
headers['content-length'] = headers['content-length'].to_i
|
51
|
+
headers['last-modified'] = DateTime.parse(headers['last-modified']).strftime('%Y-%m-%dT%H:%M:%S')
|
52
|
+
headers['content-disposition'] = headers['content-disposition'].match(/filename=(.*)$/)[1]
|
53
|
+
|
54
|
+
@zone_file_statuses[tld] = CZDS::ZoneFile::Status.new(*headers.values)
|
55
|
+
end
|
56
|
+
|
57
|
+
def download_links
|
58
|
+
return @download_links unless @download_links.nil?
|
59
|
+
|
60
|
+
conn = connection('https://czds-api.icann.org')
|
61
|
+
|
62
|
+
response = conn.get("/czds/downloads/links")
|
63
|
+
|
64
|
+
download_links = JSON.parse(response.body)
|
65
|
+
end
|
66
|
+
|
67
|
+
def download_link(tld)
|
68
|
+
download_links.find { |link| link =~ /#{tld}\.zone$/ }
|
69
|
+
end
|
70
|
+
|
71
|
+
def download_zone_file(tld)
|
72
|
+
status = zone_file_status(tld)
|
73
|
+
|
74
|
+
file_name = timestamp_file_name ? "#{status.timestamp}_#{status.file_name}" : status.file_name
|
75
|
+
file_path = File.join(download_dir, file_name)
|
76
|
+
|
77
|
+
raise FileAlreadyDownloadedError if File.exist?(file_path) && File.size(file_path) == status.length
|
78
|
+
|
79
|
+
conn = connection(download_link(tld))
|
80
|
+
|
81
|
+
File.open(file_path, 'wb') do |file|
|
82
|
+
response = conn.get do |req|
|
83
|
+
req.options.on_data = Proc.new { |chunk| file.write(chunk) }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
raise FileSizeError if File.size(file_path) != status.length
|
88
|
+
|
89
|
+
status.to_h.merge(file_path:)
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def access_token
|
95
|
+
return @access_token unless @access_token.nil?
|
96
|
+
|
97
|
+
conn = Faraday.new(
|
98
|
+
url: 'https://account-api.icann.org',
|
99
|
+
headers: {
|
100
|
+
'Content-Type' => 'application/json',
|
101
|
+
'Accept' => 'application/json',
|
102
|
+
'User-Agent' => user_agent,
|
103
|
+
}
|
104
|
+
)
|
105
|
+
|
106
|
+
response = conn.post("/api/authenticate") do |req|
|
107
|
+
req.body = { username:, password: }.to_json
|
108
|
+
end
|
109
|
+
|
110
|
+
@access_token = JSON.parse(response.body)['accessToken'] if response.status == 200
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CZDS
|
4
|
+
class Configuration
|
5
|
+
DEFAULTS = {
|
6
|
+
user_agent: "czds/#{CZDS::VERSION} (Ruby Client)",
|
7
|
+
tld: "com",
|
8
|
+
download_dir: "./"
|
9
|
+
}
|
10
|
+
|
11
|
+
attr_accessor :username, :password, :download_dir, :timestamp_file_name, :user_agent
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@download_dir = DEFAULTS[:download_dir]
|
15
|
+
@timestamp_file_name = true
|
16
|
+
@user_agent = DEFAULTS[:user_agent]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/czds/version.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module CZDS
|
2
|
+
class ZoneFile
|
3
|
+
Status = Data.define(:file_name, :length, :updated_at) do
|
4
|
+
def timestamp
|
5
|
+
updated_at.gsub(':', '-')
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_h
|
9
|
+
{
|
10
|
+
file_name:,
|
11
|
+
length:,
|
12
|
+
updated_at:,
|
13
|
+
timestamp:,
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
require_relative "./client"
|
5
|
+
|
6
|
+
module CZDS
|
7
|
+
class ZoneFile
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
attr_reader :tld, :client
|
11
|
+
|
12
|
+
def_delegators :status, :file_name, :length, :updated_at, :timestamp
|
13
|
+
|
14
|
+
def initialize(tld = "com")
|
15
|
+
@tld = tld
|
16
|
+
@client = Client.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def download
|
20
|
+
client.download_zone_file(tld)
|
21
|
+
end
|
22
|
+
|
23
|
+
def status
|
24
|
+
@status ||= client.zone_file_status(tld)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/czds.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./czds/version"
|
4
|
+
require_relative "./czds/client"
|
5
|
+
require_relative "./czds/zone_file"
|
6
|
+
require_relative "./czds/cli/main"
|
7
|
+
|
8
|
+
module CZDS
|
9
|
+
class FileSizeError < StandardError; end
|
10
|
+
class FileAlreadyDownloadedError < StandardError; end
|
11
|
+
class NoCredentialsError < StandardError; end
|
12
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: czds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcin Doliwa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05-
|
11
|
+
date: 2024-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -46,7 +46,15 @@ executables:
|
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
48
48
|
files:
|
49
|
+
- README.md
|
49
50
|
- bin/czds
|
51
|
+
- lib/czds.rb
|
52
|
+
- lib/czds/cli/main.rb
|
53
|
+
- lib/czds/client.rb
|
54
|
+
- lib/czds/configuration.rb
|
55
|
+
- lib/czds/version.rb
|
56
|
+
- lib/czds/zone_file.rb
|
57
|
+
- lib/czds/zone_file/status.rb
|
50
58
|
homepage: https://github.com/mdoliwa/czds
|
51
59
|
licenses:
|
52
60
|
- MIT
|
@@ -66,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
74
|
- !ruby/object:Gem::Version
|
67
75
|
version: '0'
|
68
76
|
requirements: []
|
69
|
-
rubygems_version: 3.5.
|
77
|
+
rubygems_version: 3.5.10
|
70
78
|
signing_key:
|
71
79
|
specification_version: 4
|
72
80
|
summary: Ruby client for the ICANN Centralized Zone Data Service (CZDS) API.
|