Class: RegisteredDomains::Namecheap::Domains

Inherits:
Object
  • Object
show all
Defined in:
lib/registered_domains/namecheap.rb

Overview

Get the list of registered domains for an account from namecheap Returns an array of domain names.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, api_key, api_user) ⇒ Domains

Returns a new instance of Domains.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/registered_domains/namecheap.rb', line 13

def initialize(user, api_key, api_user)
  ip = ::HTTParty.get('https://ipv4.icanhazip.com')
  ip.success?
  @config = {
    client_ip: ip,
    username: user,
    api_key: api_key,
    api_username: api_user,
  }

  @page_size = 100
  @total_items = 0
  @domains = []
  get
end

Instance Attribute Details

#domainsObject (readonly)

Returns the value of attribute domains.



11
12
13
# File 'lib/registered_domains/namecheap.rb', line 11

def domains
  @domains
end

Instance Method Details

#clientObject



29
30
31
# File 'lib/registered_domains/namecheap.rb', line 29

def client
  @client ||= NamecheapApi::Client.new(@config)
end

#docObject



37
38
39
# File 'lib/registered_domains/namecheap.rb', line 37

def doc
  REXML::Document.new @response.raw_body
end

#extract_domainsObject



45
46
47
48
49
# File 'lib/registered_domains/namecheap.rb', line 45

def extract_domains
  domains = []
  doc.elements.each("ApiResponse/CommandResponse/DomainGetListResult/Domain") { |element| domains << element.attributes['Name'] }
  domains
end

#getObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/registered_domains/namecheap.rb', line 51

def get
  # value for total domains for initial request.  actual value determined from first response
  @total_items = @page_size

  page = 0
  requested_items = 0
  while requested_items < @total_items
    # Initial request, from which we'll determine if we need to keep requesting more pages
    requested_items += @page_size
    page += 1
    @response = send_request(@page_size, page)
    response_ok?
    get_total_items
    @domains += extract_domains
  end
end

#get_total_itemsObject



41
42
43
# File 'lib/registered_domains/namecheap.rb', line 41

def get_total_items
  doc.elements.each("ApiResponse/CommandResponse/Paging/TotalItems") { |element| @total_items = element.text.to_i }
end

#response_ok?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
# File 'lib/registered_domains/namecheap.rb', line 68

def response_ok?
  doc.elements.each("ApiResponse") { |element| @response_status = element.attributes['Status'] }
  if @response_status == 'OK'
    return true
  else
    @errors = []
    doc.elements.each("ApiResponse/Errors/Error") { |element| @errors << element.text }
    raise @errors.join(':').to_s
  end
end

#send_request(page_size, page) ⇒ Object



33
34
35
# File 'lib/registered_domains/namecheap.rb', line 33

def send_request(page_size, page)
  client.call('namecheap.domains.getList', {PageSize: @page_size, Page: page})
end