Skip to content

Commit

Permalink
modularize, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
parasquid committed Oct 14, 2011
1 parent 2431b42 commit 012df9b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 184 deletions.
5 changes: 5 additions & 0 deletions Rakefile
@@ -1 +1,6 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
File renamed without changes.
8 changes: 8 additions & 0 deletions lib/monkey_patch.rb
@@ -0,0 +1,8 @@
class Hash
def symbolize_keys!
keys.each do |key|
self[(key.to_sym rescue key) || key] = delete(key)
end
self
end
end
16 changes: 10 additions & 6 deletions lib/namecheap.rb
@@ -1,10 +1,14 @@
require 'monkey_patch'
require 'httparty'

$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/namecheap")
Dir.glob("#{File.dirname(__FILE__)}/namecheap/*.rb") { |lib| require File.basename(lib, '.*') }


module Namecheap
class Namecheap
attr_reader :username, :key, :client_ip
def initialize(options = {})
config = YAML.load_file("#{File.dirname(__FILE__)}/../config/namecheap.yml").symbolize_keys!
@key = options[:key] || config[:key]
@username = options[:username] || config[:username]
@client_ip = options[:client_ip] || config[:client_ip]
end
end
end


7 changes: 7 additions & 0 deletions lib/namecheap/domains.rb
@@ -0,0 +1,7 @@
require File.dirname(__FILE__) + '/../namecheap.rb'

module Namecheap
class Domains < Namecheap::Namecheap

end
end
82 changes: 0 additions & 82 deletions spec/namecheap_domain_check_response_spec.rb

This file was deleted.

25 changes: 0 additions & 25 deletions spec/namecheap_response_spec.rb

This file was deleted.

83 changes: 17 additions & 66 deletions spec/namecheap_spec.rb
@@ -1,85 +1,36 @@
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/../lib/namecheap'
require 'mocha'

describe "NamecheapAPI Wrapper" do
describe "initializating settings" do
describe "with defaults" do
describe "with defaults" do
it "should contain a username" do
namecheap = Namecheap.new
namecheap.send(:username).should == 'apiuser'
end
namecheap = Namecheap::Namecheap.new
namecheap.send(:username).should == 'apiuser'
end
it "should contain a key" do
namecheap = Namecheap.new
namecheap.send(:key).should == 'apikey'
namecheap = Namecheap::Namecheap.new
namecheap.send(:key).should == 'apikey'
end
it "should contain a client_ip" do
namecheap = Namecheap.new
namecheap.send(:client_ip).should == '127.0.0.1'
namecheap = Namecheap::Namecheap.new
namecheap.send(:client_ip).should == '127.0.0.1'
end
end

describe "with defaults overidden" do
describe "with defaults overidden" do
it "shoud contain a overidden username" do
namecheap = Namecheap.new(:username => 'testuser')
namecheap.send(:username).should == 'testuser'
end
namecheap = Namecheap::Namecheap.new(:username => 'testuser')
namecheap.send(:username).should == 'testuser'
end

it "shoud contain a key" do
namecheap = Namecheap.new(:key => 'testkey')
namecheap.send(:key).should == 'testkey'
namecheap = Namecheap::Namecheap.new(:key => 'testkey')
namecheap.send(:key).should == 'testkey'
end
it "shoud contain a client_ip" do
namecheap = Namecheap.new(:client_ip => '66.11.22.44')
namecheap.send(:client_ip).should == '66.11.22.44'
end
end
end

describe "Attempt to connect with bad credentials" do
it "should report an error on erroneous account information" do
namecheap = Namecheap.new
namecheap.domain_check("fakedomain").status.should == "ERROR"
namecheap = Namecheap::Namecheap.new(:client_ip => '66.11.22.44')
namecheap.send(:client_ip).should == '66.11.22.44'
end

it "should give error message for invalid api key when using an invalid key" do
namecheap = Namecheap.new
namecheap.domain_check("fakedomain").message.should include("API Key is invalid")
end
end

describe "Attempt to connect with valid credentials" do

end

describe "#domain_check" do
it "should build query with multiple domains" do
namecheap = Namecheap.new()
namecheap.expects(:do_query).with("namecheap.domains.check", "&DomainList=domain1.com,domain2.com")
namecheap.domain_check(['domain1.com','domain2.com'])
end
end

describe "#is_domain_available?" do
it "should return false if connection fails" do
namecheap = Namecheap.new(:apikey => 'BADKEY')
lambda {
namecheap.is_domain_available?('fakedomain.tld').should be_false
}.should raise_error(NilNamecheapResponse)
end

it "should return true if connections succeeds and domain is available" do
pending "Need API Access To Namecheap.com"
namecheap = Namecheap.new
namecheap.is_domain_available?('saucytuborswithashoefetish.com').should be_true
end

it "should return false if connections succeeds and domain is not available" do
namecheap = Namecheap.new
lambda {
namecheap.is_domain_available?('hashrocket.com').should be_false
}.should raise_error(NilNamecheapResponse)
end
end
end

end
7 changes: 2 additions & 5 deletions spec/spec_helper.rb
@@ -1,12 +1,9 @@
begin
require 'spec'
require 'rspec'
rescue LoadError
require 'rubygems'
gem 'rspec'
require 'spec'
end

require 'activesupport'
require 'activerecord'

$:.unshift(File.dirname(__FILE__) + '/../lib')
require File.dirname(__FILE__) + '/../lib/namecheap'

0 comments on commit 012df9b

Please sign in to comment.