Class: OpenSRS::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/opensrs/domain.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, client: nil, expires_at: nil, created_at: nil, updated_at: nil, auto_renew: false, locked: nil, whois_private: nil, nameservers: [], contacts: {}) ⇒ Domain

Returns a new instance of Domain.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/opensrs/domain.rb', line 11

def initialize(name:, client: nil, expires_at: nil, created_at: nil,
               updated_at: nil, auto_renew: false, locked: nil,
               whois_private: nil, nameservers: [], contacts: {})
  @name = name
  @client = client || OpenSRS.default_client
  @expires_at = expires_at
  @created_at = created_at
  @updated_at = updated_at
  @auto_renew = auto_renew
  @locked = locked
  @whois_private = whois_private
  @nameservers = nameservers
  @contacts = contacts
end

Instance Attribute Details

#contactsObject (readonly)

Returns the value of attribute contacts.



9
10
11
# File 'lib/opensrs/domain.rb', line 9

def contacts
  @contacts
end

#created_atObject (readonly)

Returns the value of attribute created_at.



9
10
11
# File 'lib/opensrs/domain.rb', line 9

def created_at
  @created_at
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



9
10
11
# File 'lib/opensrs/domain.rb', line 9

def expires_at
  @expires_at
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/opensrs/domain.rb', line 9

def name
  @name
end

#nameserversObject (readonly)

Returns the value of attribute nameservers.



9
10
11
# File 'lib/opensrs/domain.rb', line 9

def nameservers
  @nameservers
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



9
10
11
# File 'lib/opensrs/domain.rb', line 9

def updated_at
  @updated_at
end

Class Method Details

.all(from: "2020-01-01", to: "2030-12-31", client: OpenSRS.default_client) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/opensrs/domain.rb', line 113

def all(from: "2020-01-01", to: "2030-12-31", client: OpenSRS.default_client)
  result = client.call(action: "GET_DOMAINS_BY_EXPIREDATE", object: "DOMAIN",
    attributes: { "exp_from" => from, "exp_to" => to, "limit" => "200", "page" => "0" })
  domains = result.dig("attributes", "exp_domains") || []
  domains.map do |d|
    new(
      name: d["name"],
      expires_at: parse_time(d["expiredate"]),
      client: client
    )
  end
end

.available?(domain_name, client: OpenSRS.default_client) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/opensrs/domain.rb', line 49

def available?(domain_name, client: OpenSRS.default_client)
  result = client.call(action: "LOOKUP", object: "DOMAIN",
    attributes: { "domain" => domain_name })
  result.dig("attributes", "status") == "available"
rescue OpenSRS::DomainUnavailable
  false
end

.check(domain_name, period: 1, client: OpenSRS.default_client) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/opensrs/domain.rb', line 57

def check(domain_name, period: 1, client: OpenSRS.default_client)
  lookup = client.call(action: "LOOKUP", object: "DOMAIN",
    attributes: { "domain" => domain_name })
  available = lookup.dig("attributes", "status") == "available"

  price = nil
  if available
    begin
      price_result = client.call(action: "GET_PRICE", object: "DOMAIN",
        attributes: { "domain" => domain_name, "period" => period.to_s, "reg_type" => "new" })
      price = price_result.dig("attributes", "price")&.to_f
    rescue OpenSRS::Error
      # price unavailable for this period (e.g. TLD minimum not met)
    end
  end
  price ||= lookup.dig("attributes", "price")&.to_f

  DomainCheck.new(domain: domain_name, available: available, price: price, period: period)
rescue OpenSRS::DomainUnavailable
  DomainCheck.new(domain: domain_name, available: false, price: nil, period: period)
end

.find(domain_name, client: OpenSRS.default_client) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/opensrs/domain.rb', line 105

def find(domain_name, client: OpenSRS.default_client)
  result = client.call(action: "GET", object: "DOMAIN",
    attributes: { "type" => "all_info" },
    extra: { "domain" => domain_name })
  attrs = result["attributes"] || {}
  from_api(domain_name, attrs, client: client)
end

.price(domain_name, period: 1, client: OpenSRS.default_client) ⇒ Object



79
80
81
82
83
# File 'lib/opensrs/domain.rb', line 79

def price(domain_name, period: 1, client: OpenSRS.default_client)
  result = client.call(action: "GET_PRICE", object: "DOMAIN",
    attributes: { "domain" => domain_name, "period" => period.to_s, "reg_type" => "new" })
  result.dig("attributes", "price").to_f
end

.register!(domain_name, period: 1, contacts:, nameservers:, client: OpenSRS.default_client) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/opensrs/domain.rb', line 132

def register!(domain_name, period: 1, contacts:, nameservers:, client: OpenSRS.default_client)
  contact_set = build_contact_set(contacts)

  ns_list = nameservers.each_with_index.map do |ns, i|
    { "name" => ns, "sortorder" => (i + 1).to_s }
  end

  reg_user = domain_name.gsub(/[^a-z0-9]/i, "")[0, 20]
  reg_user = "user#{reg_user}" if reg_user.length < 3
  reg_pass = "Rg!" + Digest::MD5.hexdigest(domain_name + Time.now.to_s)[0, 17]

  client.call(action: "SW_REGISTER", object: "DOMAIN", attributes: {
    "domain" => domain_name,
    "reg_type" => "new",
    "period" => period.to_s,
    "reg_username" => reg_user,
    "reg_password" => reg_pass,
    "handle" => "process",
    "custom_nameservers" => "1",
    "custom_tech_contact" => "0",
    "auto_renew" => "0",
    "f_lock_domain" => "1",
    "f_whois_privacy" => "1",
    "contact_set" => contact_set,
    "nameserver_list" => ns_list,
  })

  new(name: domain_name, client: client, nameservers: nameservers)
end

.suggest(search_string, tlds: [".com", ".net", ".org", ".io"], client: OpenSRS.default_client) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/opensrs/domain.rb', line 85

def suggest(search_string, tlds: [".com", ".net", ".org", ".io"], client: OpenSRS.default_client)
  result = client.call(action: "NAME_SUGGEST", object: "DOMAIN",
    attributes: { "searchstring" => search_string, "tlds" => tlds, "max_wait_time" => "5" })
  attrs = result["attributes"] || {}

  suggestions = []
  %w[lookup suggestion].each do |category|
    items = attrs.dig(category, "items")
    next unless items.is_a?(Array)
    items.each do |item|
      next unless item.is_a?(Hash) && item["domain"]
      suggestions << DomainSuggestion.new(
        name: item["domain"],
        available: item["status"] == "available"
      )
    end
  end
  suggestions
end

.transfer!(domain_name, auth_code:, contacts:, nameservers: [], client: OpenSRS.default_client) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/opensrs/domain.rb', line 162

def transfer!(domain_name, auth_code:, contacts:, nameservers: [], client: OpenSRS.default_client)
  contact_set = build_contact_set(contacts)

  reg_user = domain_name.gsub(/[^a-z0-9]/i, "")[0, 20]
  reg_user = "user#{reg_user}" if reg_user.length < 3
  reg_pass = "Rg!" + Digest::MD5.hexdigest(domain_name + Time.now.to_s)[0, 17]

  attrs = {
    "domain" => domain_name,
    "reg_type" => "transfer",
    "period" => "1",
    "reg_username" => reg_user,
    "reg_password" => reg_pass,
    "handle" => "process",
    "custom_nameservers" => nameservers.any? ? "1" : "0",
    "custom_tech_contact" => "0",
    "auto_renew" => "0",
    "contact_set" => contact_set,
    "auth_info" => auth_code,
  }

  if nameservers.any?
    attrs["nameserver_list"] = nameservers.each_with_index.map { |ns, i|
      { "name" => ns, "sortorder" => (i + 1).to_s }
    }
  end

  client.call(action: "SW_REGISTER", object: "DOMAIN", attributes: attrs)
  new(name: domain_name, client: client, nameservers: nameservers)
end

.transferable?(domain_name, client: OpenSRS.default_client) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/opensrs/domain.rb', line 126

def transferable?(domain_name, client: OpenSRS.default_client)
  result = client.call(action: "CHECK_TRANSFER", object: "DOMAIN",
    attributes: { "domain" => domain_name, "check_status" => "1" })
  result.dig("attributes", "transferrable") == "1"
end

Instance Method Details

#auto_renew?Boolean

Returns:

  • (Boolean)


26
# File 'lib/opensrs/domain.rb', line 26

def auto_renew? = @auto_renew

#disable_whois_privacy!Object



283
284
285
286
287
288
# File 'lib/opensrs/domain.rb', line 283

def disable_whois_privacy!
  @client.call(action: "MODIFY", object: "DOMAIN",
    attributes: { "affect_domains" => "0", "data" => "whois_privacy_state", "state" => "disable" },
    extra: { "domain" => @name })
  @whois_private = false
end

#enable_whois_privacy!Object



276
277
278
279
280
281
# File 'lib/opensrs/domain.rb', line 276

def enable_whois_privacy!
  @client.call(action: "MODIFY", object: "DOMAIN",
    attributes: { "affect_domains" => "0", "data" => "whois_privacy_state", "state" => "enable" },
    extra: { "domain" => @name })
  @whois_private = true
end

#lock!Object



254
255
256
257
258
# File 'lib/opensrs/domain.rb', line 254

def lock!
  @client.call(action: "MODIFY", object: "DOMAIN",
    attributes: { "domain" => @name, "data" => "status", "affect_domains" => "0", "lock_state" => "1" })
  @locked = true
end

#locked?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/opensrs/domain.rb', line 28

def locked?
  if @locked.nil?
    result = @client.call(action: "GET", object: "DOMAIN",
      attributes: { "type" => "status", "domain_name" => @name },
      extra: { "domain" => @name })
    @locked = result.dig("attributes", "lock_state").to_s == "1"
  end
  @locked
end

#renew!(years: 1) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/opensrs/domain.rb', line 241

def renew!(years: 1)
  current = self.class.find(@name, client: @client)
  expiry_year = current.expires_at&.year || Time.now.year

  @client.call(action: "RENEW", object: "DOMAIN", attributes: {
    "domain" => @name,
    "auto_renew" => "0",
    "handle" => "process",
    "currentexpirationyear" => expiry_year.to_s,
    "period" => years.to_s,
  })
end

#set_auto_renew(enabled) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/opensrs/domain.rb', line 290

def set_auto_renew(enabled)
  @client.call(action: "MODIFY", object: "DOMAIN",
    attributes: { "affect_domains" => "0", "data" => "expire_action",
                   "auto_renew" => enabled ? "1" : "0", "let_expire" => enabled ? "0" : "1" },
    extra: { "domain" => @name })
  @auto_renew = enabled
end

#set_nameservers(nameservers) ⇒ Object



266
267
268
269
270
271
272
273
274
# File 'lib/opensrs/domain.rb', line 266

def set_nameservers(nameservers)
  @client.call(action: "ADVANCED_UPDATE_NAMESERVERS", object: "DOMAIN",
    attributes: {
      "domain" => @name,
      "op_type" => "assign",
      "assign_ns" => nameservers
    })
  @nameservers = nameservers
end

#unlock!Object



260
261
262
263
264
# File 'lib/opensrs/domain.rb', line 260

def unlock!
  @client.call(action: "MODIFY", object: "DOMAIN",
    attributes: { "domain" => @name, "data" => "status", "affect_domains" => "0", "lock_state" => "0" })
  @locked = false
end

#whois_private?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/opensrs/domain.rb', line 38

def whois_private?
  if @whois_private.nil?
    result = @client.call(action: "GET", object: "DOMAIN",
      attributes: { "type" => "whois_privacy_state" },
      extra: { "domain" => @name })
    @whois_private = result.dig("attributes", "state") == "enabled"
  end
  @whois_private
end