Class: Namecheap::Api

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

Direct Known Subclasses

Dns, Domains, Ns, Ssl, Transfers, Users, Whois_Guard

Constant Summary collapse

SANDBOX =
"https://api.sandbox.namecheap.com/xml.response"
PRODUCTION =
"https://api.namecheap.com/xml.response"
ENVIRONMENT =
(Rails) && Rails.respond_to?(:env)) ? Rails.env.to_s : (ENV["RACK_ENV"] || "development")
ENDPOINT =
(ENVIRONMENT == "production") ? PRODUCTION : SANDBOX

Instance Method Summary collapse

Instance Method Details

#delete(command, options = {}) ⇒ Object



22
23
24
# File 'lib/namecheap/api.rb', line 22

def delete(command, options = {})
  request "delete", command, options
end

#get(command, options = {}) ⇒ Object



10
11
12
# File 'lib/namecheap/api.rb', line 10

def get(command, options = {})
  request "get", command, options
end

#init_argsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/namecheap/api.rb', line 45

def init_args
  %w[username key client_ip].each do |key|
    if Namecheap.config.public_send(key).nil?
      raise Namecheap::Config::RequiredOptionMissing,
        "Configuration parameter missing: #{key}, " \
        "please add it to the Namecheap.configure block"
    end
  end

  {
    api_user: Namecheap.config.username,
    user_name: Namecheap.config.username,
    api_key: Namecheap.config.key,
    client_ip: Namecheap.config.client_ip
  }
end

#post(command, options = {}) ⇒ Object



14
15
16
# File 'lib/namecheap/api.rb', line 14

def post(command, options = {})
  request "post", command, options
end

#put(command, options = {}) ⇒ Object



18
19
20
# File 'lib/namecheap/api.rb', line 18

def put(command, options = {})
  request "put", command, options
end

#request(method, command, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/namecheap/api.rb', line 26

def request(method, command, options = {})
  command = "namecheap.#{command}"
  options = init_args.merge(options).merge(command: command)
  options.keys.each do |key|
    options[key.to_s.camelize] = options.delete(key)
  end

  case method
  when "get"
    HTTParty.get(endpoint, query: options)
  when "post"
    HTTParty.post(endpoint, query: options)
  when "put"
    HTTParty.put(endpoint, query: options)
  when "delete"
    HTTParty.delete(endpoint, query: options)
  end
end