namecheap-client 0.1.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/namecheap-client.rb +163 -0
  3. metadata +87 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 03c4ea68d701b6b743bf6289f826fae4e7ac8af5c1dfe65aa1ba0f4734851d8b
4
+ data.tar.gz: e005d6a732e26397d16d663e94cff09915527a9d00308d2b2b44e58a94f60da5
5
+ SHA512:
6
+ metadata.gz: 770a4fbd5c5029087822c8f71d8799a8986989a0daacc9be5c33d8b39021744b2ab1f8e5ae93763d05c4f16d9a35def47b0ac1140d58b0f818280c371f0cbd7e
7
+ data.tar.gz: 35d06f859aced4784b136b6a524c46e60afa20387c9c26f86bed0f05f5bdcbaa7f9cb3c85152bb7b1ea285ee45edff321d5f48d7e5b453fe04cb90e1b0bfb567
@@ -0,0 +1,163 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'nokogiri'
4
+ #require 'pry'
5
+
6
+ class NamecheapClient
7
+ API_URL = 'https://api.namecheap.com/xml.response'
8
+
9
+ def initialize(api_user:, api_key:, username:, client_ip:)
10
+ @api_user = api_user
11
+ @api_key = api_key
12
+ @username = username
13
+ @client_ip = client_ip
14
+ end
15
+
16
+ # List all domains in the account
17
+ def list_domains
18
+ params = {
19
+ 'Command' => 'namecheap.domains.getList',
20
+ 'PageSize' => '100'
21
+ }
22
+ response = make_request(params)
23
+ parse_domain_list(response)
24
+ end
25
+
26
+ # Add DNS record to a domain
27
+ def add_dns_record(domain, record_type, host_name, address, ttl = '1800')
28
+ # First, get existing records
29
+ existing_records = get_dns_records(domain)
30
+
31
+ # Add the new record
32
+ existing_records << {
33
+ 'Type' => record_type,
34
+ 'Name' => host_name,
35
+ 'Address' => address,
36
+ 'TTL' => ttl
37
+ }
38
+
39
+ # Set the records
40
+ set_dns_records(domain, existing_records)
41
+ end
42
+
43
+ # Remove DNS record from a domain
44
+ def remove_dns_record(domain, record_type, host_name)
45
+ # Get existing records
46
+ existing_records = get_dns_records(domain)
47
+
48
+ # Remove the specified record(s)
49
+ updated_records = existing_records.reject do |record|
50
+ record['Type'] == record_type && record['Name'] == host_name
51
+ end
52
+
53
+ # Set the updated records
54
+ set_dns_records(domain, updated_records)
55
+ end
56
+
57
+ private
58
+
59
+ def make_request(extra_params)
60
+ uri = URI.parse(API_URL)
61
+ params = {
62
+ 'ApiUser' => @api_user,
63
+ 'ApiKey' => @api_key,
64
+ 'UserName' => @username,
65
+ 'ClientIp' => @client_ip
66
+ }.merge(extra_params)
67
+
68
+ response = Net::HTTP.post_form(uri, params)
69
+ Nokogiri::XML(response.body)
70
+ end
71
+
72
+ def parse_domain_list(xml_response)
73
+ # Define the namespace
74
+ namespaces = { 'nc' => 'http://api.namecheap.com/xml.response' }
75
+
76
+ domains = []
77
+ # Use the namespace in the XPath query
78
+ xml_response.xpath('//nc:DomainGetListResult/nc:Domain', namespaces).each do |domain_node|
79
+ domains << {
80
+ 'ID' => domain_node['ID'],
81
+ 'Name' => domain_node['Name'],
82
+ 'User' => domain_node['User'],
83
+ 'Created' => domain_node['Created'],
84
+ 'Expires' => domain_node['Expires'],
85
+ 'IsExpired' => domain_node['IsExpired'],
86
+ 'IsLocked' => domain_node['IsLocked'],
87
+ 'AutoRenew' => domain_node['AutoRenew'],
88
+ 'WhoisGuard' => domain_node['WhoisGuard'],
89
+ 'IsPremium' => domain_node['IsPremium'],
90
+ 'IsOurDNS' => domain_node['IsOurDNS']
91
+ }
92
+ end
93
+ domains
94
+ end
95
+
96
+ def get_dns_records(domain)
97
+ params = {
98
+ 'Command' => 'namecheap.domains.dns.getHosts',
99
+ 'SLD' => domain_label(domain),
100
+ 'TLD' => domain_extension(domain)
101
+ }
102
+ response = make_request(params)
103
+ parse_dns_records(response)
104
+ end
105
+
106
+ def set_dns_records(domain, records)
107
+ params = {
108
+ 'Command' => 'namecheap.domains.dns.setHosts',
109
+ 'SLD' => domain_label(domain),
110
+ 'TLD' => domain_extension(domain)
111
+ }
112
+
113
+ records.each_with_index do |record, index|
114
+ idx = index + 1
115
+ params["HostName#{idx}"] = record['Name']
116
+ params["RecordType#{idx}"] = record['Type']
117
+ params["Address#{idx}"] = record['Address']
118
+ params["TTL#{idx}"] = record['TTL'] || '1800'
119
+ end
120
+
121
+ response = make_request(params)
122
+ namespaces = { 'nc' => 'http://api.namecheap.com/xml.response' }
123
+
124
+ # Check the IsSuccess attribute in the DomainDNSSetHostsResult element
125
+ result_node = response.at_xpath('//nc:DomainDNSSetHostsResult', namespaces)
126
+
127
+ if result_node && result_node['IsSuccess'] == 'true'
128
+ return true
129
+ else
130
+ # If IsSuccess is not 'true', parse and raise errors
131
+ error_message = parse_errors(response)
132
+ raise StandardError, error_message.empty? ? 'Failed to set DNS records' : error_message
133
+ end
134
+ end
135
+
136
+ def parse_dns_records(xml_response)
137
+ records = []
138
+ xml_response.xpath('//DomainDNSGetHostsResult/host').each do |host_node|
139
+ records << {
140
+ 'Type' => host_node['Type'],
141
+ 'Name' => host_node['Name'],
142
+ 'Address' => host_node['Address'],
143
+ 'TTL' => host_node['TTL']
144
+ }
145
+ end
146
+ records
147
+ end
148
+
149
+ def parse_errors(xml_response)
150
+ errors = xml_response.xpath('//Errors/Error').map(&:text)
151
+ errors.join('; ')
152
+ end
153
+
154
+ def domain_label(domain)
155
+ domain_parts = domain.split('.')
156
+ domain_parts[0..-2].join('.')
157
+ end
158
+
159
+ def domain_extension(domain)
160
+ domain_parts = domain.split('.')
161
+ domain_parts[-1]
162
+ end
163
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namecheap-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Leandro Daniel Sardi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: uri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.11.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.11.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.10
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.10
55
+ description: A simple Ruby client library for interacting with the [Namecheap API](https://www.namecheap.com/support/api/intro/),
56
+ allowing you to manage your domains programmatically.
57
+ email: leandro@massprospecting.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/namecheap-client.rb
63
+ homepage: https://rubygems.org/gems/namecheap-client
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.3.7
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A simple Ruby client library for interacting with the [Namecheap API](https://www.namecheap.com/support/api/intro/),
86
+ allowing you to manage your domains programmatically.
87
+ test_files: []