Class: CZDS::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/czds/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.

Raises:



26
27
28
29
30
31
# File 'lib/czds/client.rb', line 26

def initialize
  @zone_file_statuses = {}
  @config = self.class.config

  raise NoCredentialsError if @config.username.nil? || @config.password.nil?
end

Class Attribute Details

.configObject

Returns the value of attribute config.



15
16
17
# File 'lib/czds/client.rb', line 15

def config
  @config
end

Instance Attribute Details

#zone_file_statusesObject (readonly)

Returns the value of attribute zone_file_statuses.



22
23
24
# File 'lib/czds/client.rb', line 22

def zone_file_statuses
  @zone_file_statuses
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/czds/client.rb', line 17

def configure
  yield(config) if block_given?
end

Instance Method Details

#connection(url) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/czds/client.rb', line 33

def connection(url)
  Faraday.new(
    url:,
    headers: {
      'Authorization' => "Bearer #{access_token}",
      'User-Agent' => user_agent,
    })
end


67
68
69
# File 'lib/czds/client.rb', line 67

def download_link(tld)
  download_links.find { |link| link =~ /#{tld}\.zone$/ }
end


57
58
59
60
61
62
63
64
65
# File 'lib/czds/client.rb', line 57

def download_links
  return @download_links unless @download_links.nil?

  conn = connection('https://czds-api.icann.org')

  response = conn.get("/czds/downloads/links")

  download_links = JSON.parse(response.body)
end

#download_zone_file(tld) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/czds/client.rb', line 71

def download_zone_file(tld)
  status = zone_file_status(tld)

  file_name = timestamp_file_name ? "#{status.timestamp}_#{status.file_name}" : status.file_name
  file_path = File.join(download_dir, file_name)

  raise FileAlreadyDownloadedError if File.exist?(file_path) && File.size(file_path) == status.length

  conn = connection(download_link(tld)) 

  File.open(file_path, 'wb') do |file|
    response = conn.get do |req|
      req.options.on_data = Proc.new { |chunk| file.write(chunk) }
    end
  end

  raise FileSizeError if File.size(file_path) != status.length

  status.to_h.merge(file_path:)
end

#zone_file_status(tld) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/czds/client.rb', line 42

def zone_file_status(tld)
  return zone_file_statuses[tld] unless zone_file_statuses[tld].nil?

  conn = connection(download_link(tld))

  response = conn.head

  headers = response.headers.slice('content-disposition', 'content-length', 'last-modified')
  headers['content-length'] = headers['content-length'].to_i
  headers['last-modified'] = DateTime.parse(headers['last-modified']).strftime('%Y-%m-%dT%H:%M:%S')
  headers['content-disposition'] = headers['content-disposition'].match(/filename=(.*)$/)[1]

  @zone_file_statuses[tld] = CZDS::ZoneFile::Status.new(*headers.values)
end