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 =
defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : (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 'post', 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



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

def init_args
  %w(username key client_ip).each do |key|
    if Namecheap.config.key.nil?
      raise Namecheap::Config::RequiredOptionMissing,
        "Configuration parameter missing: #{key}, " +
        "please add it to the Namecheap.configure block"
    end
  end
  options = {
    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 'post', 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
44
# 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'
    #raise options.inspect
    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