namecheap 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  development:
2
- key: 4fad5a63f8bd4e03a8bbc8131a2fb331
3
- username: parasquid
4
- client_ip: 175.136.192.89
2
+ key: apikey
3
+ username: apiuser
4
+ client_ip: 127.0.0.1
5
5
 
6
6
  test:
7
7
  key: apikey
@@ -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
@@ -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
- protected
10
+ extend self
19
11
 
20
- def api_call(command, command_args)
21
- args = {}
22
- args['ApiUser'] = args['UserName'] = @username
23
- args['ApiKey'] = @key
24
- args['ClientIp'] = @client_ip
25
- args['Command'] = command
26
- query = @endpoint + '?' + args.to_param
27
- HTTParty.get(query)
28
- end
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
- end
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
- $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/namecheap")
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
@@ -1,8 +1,14 @@
1
1
  module Namecheap
2
- class Domains < Namecheap::Api
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
@@ -1,3 +1,3 @@
1
1
  module Namecheap
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe "Domains" do
4
- it "should instantiate" do
5
- Namecheap::Domains.new(:environment => 'test')
4
+ it 'should initialize' do
5
+ Namecheap::Domains.new
6
6
  end
7
7
  end
@@ -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
- namecheap = Namecheap::Api.new(:environment => 'test')
8
- namecheap.send(:username).should == 'apiuser'
12
+ Namecheap.username.should == 'apiuser'
9
13
  end
10
14
  it "should contain a key" do
11
- namecheap = Namecheap::Api.new(:environment => 'test')
12
- namecheap.send(:key).should == 'apikey'
15
+ Namecheap.key.should == 'apikey'
13
16
  end
14
17
  it "should contain a client_ip" do
15
- namecheap = Namecheap::Api.new(:environment => 'test')
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 "shoud contain a overidden username" do
22
- namecheap = Namecheap::Api.new(:environment => 'test', :username => 'testuser')
23
- namecheap.send(:username).should == 'testuser'
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 "shoud contain a key" do
27
- namecheap = Namecheap::Api.new(:environment => 'test', :key => 'testkey')
28
- namecheap.send(:key).should == 'testkey'
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
- it "shoud contain a client_ip" do
31
- namecheap = Namecheap::Api.new(:environmeny => 'test', :client_ip => '66.11.22.44')
32
- namecheap.send(:client_ip).should == '66.11.22.44'
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
- - 2
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-14 00:00:00 Z
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