Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

modularize, update tests

  • Loading branch information...
commit 012df9bc6f491504a138eca8b2ffb0999d79767c 1 parent 2431b42
parasquid authored
View
5 Rakefile
@@ -1 +1,6 @@
require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
View
0  lib/namecheap.yml → config/namecheap.yml
File renamed without changes
View
8 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
View
16 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
-
-
View
7 lib/namecheap/domains.rb
@@ -0,0 +1,7 @@
+require File.dirname(__FILE__) + '/../namecheap.rb'
+
+module Namecheap
+ class Domains < Namecheap::Namecheap
+
+ end
+end
View
82 spec/namecheap_domain_check_response_spec.rb
@@ -1,82 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper'
-
-$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib/namecheap")
-Dir.glob("#{File.dirname(__FILE__)}/../lib/namecheap/*.rb") { |lib| require File.basename(lib, '.*') }
-
-require File.dirname(__FILE__) + '/../lib/namecheap'
-
-describe NamecheapDomainCheckResponse do
-
- describe "successful response from namecheap" do
- before(:each) do
- @domain_check_response_hash = {"ApiResponse"=>{"Status"=>"OK",
- "Errors"=>{},
- "CommandResponse" => {"Type" => "namecheap.domains.check",
- "DomainCheckResult1" => {"Domain" => "domain1.com", "Available" => "true"},
- "DomainCheckResult2" => {"Domain" => "domain2.com", "Available" => "false"},
- "DomainCheckResult3" => {"Domain" => "domain.wtf", "Available" => "error",
- "ErrorNo" => "750", "Description" => "No response from the registry"}},
- "GMTTimeDifference"=>"--6:00",
- "RequestedCommand"=>"namecheap.domains.check",
- "Server"=>"SERVER159",
- "ExecutionTime"=>"0.01",
- "xmlns"=>"http://api.namecheap.com/xml.response"}}
-
- @response = NamecheapDomainCheckResponse.new(@domain_check_response_hash)
-
- end
-
- it "should have a domain check result" do
- @response.items.length.should == 3
- end
-
- it "should report that domain1.com is available" do
- @response.items[1].available.should be_true
- end
-
- it "should report that domain2.com is not available" do
- @response.items[2].available.should be_false
- end
-
- it "should report errors if there are any" do
- @response.items[0].error.should == "750"
- end
-
- it "should include a description if there are errors" do
- @response.items[0].description == "No response from the registry"
- end
- end
-
- describe "failure response from namecheap" do
- before(:each) do
- @bad_response_hash = {"ApiResponse"=>{"Status"=>"ERROR",
- "Errors"=>{"Error"=>"API Key is invalid or API access has not been enabled"},
- "GMTTimeDifference"=>"--6:00",
- "RequestedCommand"=>"namecheap.domains.check",
- "Server"=>"SERVER159",
- "ExecutionTime"=>"0.01",
- "xmlns"=>"http://api.namecheap.com/xml.response"}}
-
-
- it "should return false if bad hash is returned" do
-
- end
- end
- end
-end
-
- #<?xml version="1.0" encoding="utf-8"?>
- # <ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
- # <Errors />
- # <RequestedCommand>namecheap.domains.check</RequestedCommand>
- # <CommandResponse Type="namecheap.domains.check">
- # <DomainCheckResult Domain="domain1.com" Available="true" />
- # <DomainCheckResult Domain="availabledomain.com" Available="false" />
- # <DomainCheckResult Domain="err.tld" Available="error" ErrorNo="750" Description="No response from the registry" />
- # </CommandResponse>
- # <Server>SERVER-NAME</Server>
- # <GMTTimeDifference>+5</GMTTimeDifference>
- # <ExecutionTime>32.76</ExecutionTime>
- #</ApiResponse>
-
-
View
25 spec/namecheap_response_spec.rb
@@ -1,25 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper'
-require File.dirname(__FILE__) + '/../lib/namecheap'
-
-describe NamecheapResponse do
- before(:each) do
- @bad_response_hash = {"ApiResponse"=>{"Status"=>"ERROR",
- "Errors"=>{"Error"=>"API Key is invalid or API access has not been enabled"},
- "GMTTimeDifference"=>"--6:00",
- "RequestedCommand"=>"namecheap.domains.check",
- "Server"=>"SERVER159",
- "ExecutionTime"=>"0.01",
- "xmlns"=>"http://api.namecheap.com/xml.response"}}
- end
-
- it "should return a status" do
- response = NamecheapResponse.new(@bad_response_hash)
- response.status.should == "ERROR"
- end
-
- it "should give error message for invalid api key when using an invalid key" do
- response = NamecheapResponse.new(@bad_response_hash)
- response.message.should include("API Key is invalid")
- end
-end
-
View
83 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
View
7 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'
Please sign in to comment.
Something went wrong with that request. Please try again.