namecheap 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/config/namecheap.yml.orig +3 -3
- data/lib/monkey_patch.rb +35 -0
- data/lib/namecheap.rb +26 -26
- data/lib/namecheap/api.rb +19 -0
- data/lib/namecheap/config.rb +78 -0
- data/lib/namecheap/domains.rb +8 -2
- data/lib/namecheap/version.rb +1 -1
- data/spec/domains_spec.rb +2 -2
- data/spec/namecheap_spec.rb +27 -15
- metadata +5 -3
data/config/namecheap.yml.orig
CHANGED
data/lib/monkey_patch.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# Monkey-patches, mostly taken from Rails :)
|
2
|
+
|
1
3
|
class Hash
|
2
4
|
def symbolize_keys!
|
3
5
|
keys.each do |key|
|
@@ -19,3 +21,36 @@ class Hash
|
|
19
21
|
"#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
|
20
22
|
end
|
21
23
|
end
|
24
|
+
|
25
|
+
class Module
|
26
|
+
# Delegate method
|
27
|
+
# It expects an array of arguments that contains
|
28
|
+
# the methods to be delegated and a hash of options
|
29
|
+
def delegate(*methods)
|
30
|
+
# Pop up the options hash from arguments array
|
31
|
+
options = methods.pop
|
32
|
+
# Check the availability of the options hash
|
33
|
+
# and more specifically the :to option
|
34
|
+
# Raises an error if one of them is not there
|
35
|
+
unless options.is_a?(Hash) && to = options[:to]
|
36
|
+
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
|
37
|
+
end
|
38
|
+
|
39
|
+
# Make sure the :to option follows syntax rules
|
40
|
+
# for method names
|
41
|
+
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
|
42
|
+
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
|
43
|
+
end
|
44
|
+
|
45
|
+
# Set the real prefix value
|
46
|
+
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
|
47
|
+
|
48
|
+
# Here comes the magic of ruby :)
|
49
|
+
# Reflection techniques are used here:
|
50
|
+
# module_eval is used to add new methods on the fly which:
|
51
|
+
# expose the contained methods' objects
|
52
|
+
methods.each do |method|
|
53
|
+
module_eval("def #{prefix}#{method}(*args, &block)\n#{to}.__send__(#{method.inspect}, *args, &block)\nend\n", "(__DELEGATION__)", 1)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/namecheap.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
require 'httparty'
|
2
|
+
require 'monkey_patch'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/namecheap")
|
6
|
+
Dir.glob("#{File.dirname(__FILE__)}/namecheap/*.rb") { |lib| require File.basename(lib, '.*') }
|
2
7
|
|
3
8
|
module Namecheap
|
4
|
-
SANDBOX = 'https://api.sandbox.namecheap.com/xml.response'
|
5
|
-
PRODUCTION = 'https://api.namecheap.com/xml.response'
|
6
|
-
class Api
|
7
|
-
attr_reader :username, :key, :client_ip
|
8
|
-
def initialize(options = {})
|
9
|
-
config_file = options[:config_file] || "#{File.dirname(__FILE__)}/../config/namecheap.yml"
|
10
|
-
environment = options[:environment] || ENV['RACK_ENV'] || 'development'
|
11
|
-
config = options[:config_file] || YAML.load_file(config_file)[environment].symbolize_keys!
|
12
|
-
@key = options[:key] || config[:key]
|
13
|
-
@username = options[:username] || config[:username]
|
14
|
-
@client_ip = options[:client_ip] || config[:client_ip]
|
15
|
-
@endpoint = (environment == 'production' ? PRODUCTION : SANDBOX)
|
16
|
-
end
|
17
9
|
|
18
|
-
|
10
|
+
extend self
|
19
11
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
12
|
+
# Sets the Mongoid configuration options. Best used by passing a block.
|
13
|
+
#
|
14
|
+
# @example Set up configuration options.
|
15
|
+
# Namecheap.configure do |config|
|
16
|
+
# key = "apikey"
|
17
|
+
# username = "apiuser"
|
18
|
+
# client_ip = "127.0.0.1"
|
19
|
+
# end
|
20
|
+
# @return [ Config ] The configuration obejct.
|
21
|
+
def configure
|
22
|
+
block_given? ? yield(Config) : Config
|
29
23
|
end
|
30
|
-
|
24
|
+
alias :config :configure
|
25
|
+
|
26
|
+
# Take all the public instance methods from the Config singleton and allow
|
27
|
+
# them to be accessed through the Namecheap module directly.
|
28
|
+
#
|
29
|
+
# @example Delegate the configuration methods.
|
30
|
+
# Namecheap.key = 'newkey'
|
31
|
+
delegate *(Config.public_instance_methods(false) << { :to => Config })
|
31
32
|
|
32
|
-
|
33
|
-
Dir.glob("#{File.dirname(__FILE__)}/namecheap/*.rb") { |lib| require File.basename(lib, '.*') }
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Namecheap
|
2
|
+
class Api
|
3
|
+
SANDBOX = 'https://api.sandbox.namecheap.com/xml.response'
|
4
|
+
PRODUCTION = 'https://api.namecheap.com/xml.response'
|
5
|
+
ENVIRONMENT = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : (ENV["RACK_ENV"] || 'development')
|
6
|
+
ENDPOINT = (ENVIRONMENT == 'production' ? PRODUCTION : SANDBOX)
|
7
|
+
|
8
|
+
def api_call(command, command_args)
|
9
|
+
args = {}
|
10
|
+
args['ApiUser'] = args['UserName'] = Namecheap.username
|
11
|
+
args['ApiKey'] = Namecheap.key
|
12
|
+
args['ClientIp'] = Namecheap.client_ip
|
13
|
+
args['Command'] = command
|
14
|
+
args.merge! command_args
|
15
|
+
query = ENDPOINT + '?' + args.to_param
|
16
|
+
HTTParty.get(query)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Namecheap
|
2
|
+
module Config
|
3
|
+
# Taken and modified from Mongoid config.rb
|
4
|
+
|
5
|
+
extend self
|
6
|
+
|
7
|
+
attr_accessor :settings, :defaults
|
8
|
+
@settings = {}
|
9
|
+
@defaults = {}
|
10
|
+
|
11
|
+
# Define a configuration option with a default.
|
12
|
+
#
|
13
|
+
# @example Define the option.
|
14
|
+
# Config.option(:client_ip, :default => '127.0.0.1')
|
15
|
+
#
|
16
|
+
# @param [ Symbol ] name The name of the configuration option.
|
17
|
+
# @param [ Hash ] options Extras for the option.
|
18
|
+
#
|
19
|
+
# @option options [ Object ] :default The default value.
|
20
|
+
def option(name, options = {})
|
21
|
+
defaults[name] = settings[name] = options[:default]
|
22
|
+
|
23
|
+
class_eval <<-RUBY
|
24
|
+
def #{name}
|
25
|
+
settings[#{name.inspect}]
|
26
|
+
end
|
27
|
+
|
28
|
+
def #{name}=(value)
|
29
|
+
settings[#{name.inspect}] = value
|
30
|
+
end
|
31
|
+
|
32
|
+
def #{name}?
|
33
|
+
#{name}
|
34
|
+
end
|
35
|
+
RUBY
|
36
|
+
end
|
37
|
+
|
38
|
+
option :key, :default => 'apikey'
|
39
|
+
option :username, :default => 'apiuser'
|
40
|
+
option :client_ip, :default => '127.0.0.1'
|
41
|
+
|
42
|
+
# Configure namecheap from a hash. This is usually called after parsing a
|
43
|
+
# yaml config file such as mongoid.yml.
|
44
|
+
#
|
45
|
+
# @example Configure Namecheap.
|
46
|
+
# config.from_hash({})
|
47
|
+
#
|
48
|
+
# @param [ Hash ] options The settings to use.
|
49
|
+
def from_hash(options = {})
|
50
|
+
options.each_pair do |name, value|
|
51
|
+
send("#{name}=", value) if respond_to?("#{name}=")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Load the settings from a compliant namecheap.yml file. This can be used for
|
56
|
+
# easy setup with frameworks other than Rails.
|
57
|
+
#
|
58
|
+
# @example Configure Namecheap.
|
59
|
+
# Namecheap.load!("/path/to/namecheap.yml")
|
60
|
+
#
|
61
|
+
# @param [ String ] path The path to the file.
|
62
|
+
def load!(path)
|
63
|
+
settings = YAML.load(ERB.new(File.new(path).read).result)[ENVIRONMENT]
|
64
|
+
if settings.present?
|
65
|
+
from_hash(settings)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Reset the configuration options to the defaults.
|
70
|
+
#
|
71
|
+
# @example Reset the configuration options.
|
72
|
+
# config.reset
|
73
|
+
def reset
|
74
|
+
settings.replace(defaults)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
data/lib/namecheap/domains.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
module Namecheap
|
2
|
-
class Domains <
|
2
|
+
class Domains < Api
|
3
3
|
def get_list(options = {})
|
4
4
|
args = options.clone
|
5
5
|
api_call('namecheap.domains.getList', args)
|
6
6
|
end
|
7
|
+
|
8
|
+
def check(domains = [], options = {})
|
9
|
+
args = options.clone
|
10
|
+
args['DomainList'] = domains.respond_to?(:join) ? domains.join(',') : domains
|
11
|
+
api_call('namecheap.domains.check', args)
|
12
|
+
end
|
7
13
|
end
|
8
|
-
end
|
14
|
+
end
|
data/lib/namecheap/version.rb
CHANGED
data/spec/domains_spec.rb
CHANGED
data/spec/namecheap_spec.rb
CHANGED
@@ -2,34 +2,46 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
|
3
3
|
describe "NamecheapAPI Wrapper" do
|
4
4
|
describe "initializating settings" do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
Namecheap.reset
|
8
|
+
end
|
9
|
+
|
5
10
|
describe "with defaults" do
|
6
11
|
it "should contain a username" do
|
7
|
-
|
8
|
-
namecheap.send(:username).should == 'apiuser'
|
12
|
+
Namecheap.username.should == 'apiuser'
|
9
13
|
end
|
10
14
|
it "should contain a key" do
|
11
|
-
|
12
|
-
namecheap.send(:key).should == 'apikey'
|
15
|
+
Namecheap.key.should == 'apikey'
|
13
16
|
end
|
14
17
|
it "should contain a client_ip" do
|
15
|
-
|
16
|
-
namecheap.send(:client_ip).should == '127.0.0.1'
|
18
|
+
Namecheap.client_ip.should == '127.0.0.1'
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
22
|
describe "with defaults overidden" do
|
21
|
-
it "
|
22
|
-
|
23
|
-
|
23
|
+
it "should contain an overidden key" do
|
24
|
+
Namecheap.configure do |config|
|
25
|
+
config.key = 'newkey'
|
26
|
+
end
|
27
|
+
|
28
|
+
Namecheap.key.should == 'newkey'
|
24
29
|
end
|
25
30
|
|
26
|
-
it "
|
27
|
-
|
28
|
-
|
31
|
+
it "should contain an overridden username" do
|
32
|
+
Namecheap.configure do |config|
|
33
|
+
config.username = 'newuser'
|
34
|
+
end
|
35
|
+
|
36
|
+
Namecheap.username.should == 'newuser'
|
29
37
|
end
|
30
|
-
|
31
|
-
|
32
|
-
|
38
|
+
|
39
|
+
it "should contain an overridden client_ip" do
|
40
|
+
Namecheap.configure do |config|
|
41
|
+
config.client_ip = '192.168.0.1'
|
42
|
+
end
|
43
|
+
|
44
|
+
Namecheap.client_ip.should == '192.168.0.1'
|
33
45
|
end
|
34
46
|
end
|
35
47
|
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- parasquid
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-10-
|
18
|
+
date: 2011-10-15 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -63,6 +63,8 @@ files:
|
|
63
63
|
- config/namecheap.yml.orig
|
64
64
|
- lib/monkey_patch.rb
|
65
65
|
- lib/namecheap.rb
|
66
|
+
- lib/namecheap/api.rb
|
67
|
+
- lib/namecheap/config.rb
|
66
68
|
- lib/namecheap/domains.rb
|
67
69
|
- lib/namecheap/version.rb
|
68
70
|
- namecheap.gemspec
|