czds 0.1.0 → 0.1.1

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: 9bf44d91cbccade4f1900cf55a79ba5e7e9f9a498655e411ca9546f692accee0
4
- data.tar.gz: efa43634a5ac9b6e3e9dcbea6fa7c528067174d5d023aa58ccc798bdb5466357
3
+ metadata.gz: b1b1669d9f2fbe5c01a32c851f56ba49470f26d563db177000bd082d6d114a06
4
+ data.tar.gz: 113490aacf7138ca8d2cb2bcbd1118fccdf5114db48f28de1519c959add72a42
5
5
  SHA512:
6
- metadata.gz: 9917ed5ab613c56750d4452610ccc6e5fa459451332be3bbdb97dce36cf8745ef096daa2ca31781241182800fd422707fbe0afa730aeaf48750fca88795ea9bf
7
- data.tar.gz: e2d853df2a94402f45b5b2cab693975cc0d0b7bb8299ca2fa96953cd09a45b597f7102507bf156f63a43097e25f2de124e79cd4785079b05791eff2ef10a211a
6
+ metadata.gz: 7385c9935d178cf80078ecbda41c18b17ed5aab48e66eb6182eafd7dc9b6be5f83211870a0e558b0c26bd110f94d95d35c35cba9c7cf5007cd18c4d03b1b663c
7
+ data.tar.gz: 84a57b7265e649a59a184bbf23b70e704c36b935a32cc3c39f6ee8f96088b7b9e3f61cde4ecf8967bdce5c12f838e853a768ae73901585d3b942bdcfd7b0a201
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,46 @@
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
+ private
28
+
29
+ def configure_client(options, config_file)
30
+ CZDS::Client.configure do |config|
31
+ config.username = options[:username] || config_file['username']
32
+ config.password = options[:password] || config_file['password']
33
+ config.download_dir = options[:download_dir] || config_file['download_dir'] || CZDS::Configuration::DEFAULTS[:download_dir]
34
+ config.timestamp_file_name = ![options[:no_timestamp], config_file['no_timestamp']].compact[0]
35
+ end
36
+ end
37
+
38
+ def load_config(config_file)
39
+ return {} unless File.exist?(config_file)
40
+
41
+ JSON.parse(File.read(config_file))
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -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.length
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CZDS
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,9 @@
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
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
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
+ private
24
+
25
+ def status
26
+ @status ||= client.zone_file_status(tld)
27
+ end
28
+ end
29
+ 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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: czds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Doliwa
@@ -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