namecheap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm 1.8.7
2
+ rvm gemset create namecheap
3
+ rvm 1.8.7@namecheap
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in namecheap.gemspec
4
+ gemspec
data/README.textile ADDED
@@ -0,0 +1,20 @@
1
+ h1. Ruby Wrapper for Namecheap.com API
2
+
3
+ Allow the ability to check domain availability through namecheap.com API.
4
+
5
+ h2. Usage
6
+
7
+ <pre>
8
+ <code>
9
+ require 'lib/namecheap'
10
+
11
+ namecheap = Namecheap.new
12
+ namecheap.is_domain_availble? 'hashrocket.com' # => false
13
+ </code>
14
+ </pre>
15
+
16
+ h2. Contributors
17
+
18
+ * "Hashrocket":http://www.hashrocket.com
19
+ * "Corey Grusden":http://coreygrusden.com
20
+ * "Big Tiger":http://bigtiger.github.com
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/namecheap.rb ADDED
@@ -0,0 +1,77 @@
1
+ require "namecheap/version"
2
+ require 'httparty'
3
+
4
+ module Namecheap
5
+ end
6
+
7
+ class NilNamecheapResponse < Exception
8
+ end
9
+
10
+ class NamecheapResponse
11
+ def initialize(response)
12
+ @response = response
13
+ end
14
+
15
+ def status
16
+ @response["ApiResponse"]["Status"]
17
+ end
18
+
19
+ def message
20
+ if @response["ApiResponse"]["Errors"].any?
21
+ @response["ApiResponse"]["Errors"]["Error"]
22
+ end
23
+ end
24
+
25
+ def items
26
+ response = @response["ApiResponse"]["CommandResponse"]
27
+ raise NilNamecheapResponse if response.nil?
28
+ response.delete_if { |key, value| key == "Type" }
29
+ end
30
+
31
+ end
32
+
33
+ class DomainCheck
34
+ attr_accessor :domain, :available, :error, :description
35
+
36
+ def initialize(item)
37
+ @domain = item["Domain"]
38
+ @available = (item["Available"] == "true" ? true : false)
39
+ @error = item["ErrorNo"]
40
+ @description = item["Description"]
41
+ end
42
+ end
43
+
44
+ class NamecheapDomainCheckResponse < NamecheapResponse
45
+ def items
46
+ super.collect {|item| DomainCheck.new(item[1])}
47
+ end
48
+ end
49
+
50
+ class Namecheap
51
+ attr_reader :username, :key, :client_ip
52
+ def initialize(options = {})
53
+ config = YAML.load_file("#{File.dirname(__FILE__)}/namecheap.yml").symbolize_keys!
54
+ @key = options[:key] || config[:key]
55
+ @username = options[:username] || config[:username]
56
+ @client_ip = options[:client_ip] || config[:client_ip]
57
+ end
58
+
59
+ def is_domain_available?(domain)
60
+ results = domain_check(domain).items
61
+ results.nil? ? false : results.first.available?
62
+ end
63
+
64
+ def domain_check(domain)
65
+ domain = domain.join(",") if domain.is_a? Array
66
+ NamecheapDomainCheckResponse.new(do_query("namecheap.domains.check", "&DomainList=#{domain}"))
67
+ end
68
+
69
+ protected
70
+
71
+ def do_query(api_method, options)
72
+ query = "https://api.sandbox.namecheap.com/xml.response?ApiUser=#{@username}&ApiKey=#{@key}&UserName=#{@username}&ClientIp=#{@client_ip}&Command=#{api_method}"
73
+ query += options
74
+ HTTParty.get(query)
75
+ end
76
+
77
+ end
data/lib/namecheap.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ key: apikey
3
+ username: apiuser
4
+ client_ip: 127.0.0.1
@@ -0,0 +1,3 @@
1
+ module Namecheap
2
+ VERSION = "0.0.1"
3
+ end
data/namecheap.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "namecheap/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "namecheap"
7
+ s.version = Namecheap::VERSION
8
+ s.authors = ["parasquid"]
9
+ s.email = ["tristan.gomezd@gmail.com"]
10
+ s.homepage = "https://github.com/parasquid/namecheap"
11
+ s.summary = %q{Ruby wrapper for the Namecheap API}
12
+ s.description = %q{Ruby wrapper for the Namecheap API}
13
+
14
+ s.rubyforge_project = "namecheap"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ s.add_runtime_dependency "httparty"
25
+ end
@@ -0,0 +1,78 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/../lib/namecheap'
3
+
4
+ describe NamecheapDomainCheckResponse do
5
+
6
+ describe "successful response from namecheap" do
7
+ before(:each) do
8
+ @domain_check_response_hash = {"ApiResponse"=>{"Status"=>"OK",
9
+ "Errors"=>{},
10
+ "CommandResponse" => {"Type" => "namecheap.domains.check",
11
+ "DomainCheckResult1" => {"Domain" => "domain1.com", "Available" => "true"},
12
+ "DomainCheckResult2" => {"Domain" => "domain2.com", "Available" => "false"},
13
+ "DomainCheckResult3" => {"Domain" => "domain.wtf", "Available" => "error",
14
+ "ErrorNo" => "750", "Description" => "No response from the registry"}},
15
+ "GMTTimeDifference"=>"--6:00",
16
+ "RequestedCommand"=>"namecheap.domains.check",
17
+ "Server"=>"SERVER159",
18
+ "ExecutionTime"=>"0.01",
19
+ "xmlns"=>"http://api.namecheap.com/xml.response"}}
20
+
21
+ @response = NamecheapDomainCheckResponse.new(@domain_check_response_hash)
22
+
23
+ end
24
+
25
+ it "should have a domain check result" do
26
+ @response.items.length.should == 3
27
+ end
28
+
29
+ it "should report that domain1.com is available" do
30
+ @response.items[1].available.should be_true
31
+ end
32
+
33
+ it "should report that domain2.com is not available" do
34
+ @response.items[2].available.should be_false
35
+ end
36
+
37
+ it "should report errors if there are any" do
38
+ @response.items[0].error.should == "750"
39
+ end
40
+
41
+ it "should include a description if there are errors" do
42
+ @response.items[0].description == "No response from the registry"
43
+ end
44
+ end
45
+
46
+ describe "failure response from namecheap" do
47
+ before(:each) do
48
+ @bad_response_hash = {"ApiResponse"=>{"Status"=>"ERROR",
49
+ "Errors"=>{"Error"=>"API Key is invalid or API access has not been enabled"},
50
+ "GMTTimeDifference"=>"--6:00",
51
+ "RequestedCommand"=>"namecheap.domains.check",
52
+ "Server"=>"SERVER159",
53
+ "ExecutionTime"=>"0.01",
54
+ "xmlns"=>"http://api.namecheap.com/xml.response"}}
55
+
56
+
57
+ it "should return false if bad hash is returned" do
58
+
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ #<?xml version="1.0" encoding="utf-8"?>
65
+ # <ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
66
+ # <Errors />
67
+ # <RequestedCommand>namecheap.domains.check</RequestedCommand>
68
+ # <CommandResponse Type="namecheap.domains.check">
69
+ # <DomainCheckResult Domain="domain1.com" Available="true" />
70
+ # <DomainCheckResult Domain="availabledomain.com" Available="false" />
71
+ # <DomainCheckResult Domain="err.tld" Available="error" ErrorNo="750" Description="No response from the registry" />
72
+ # </CommandResponse>
73
+ # <Server>SERVER-NAME</Server>
74
+ # <GMTTimeDifference>+5</GMTTimeDifference>
75
+ # <ExecutionTime>32.76</ExecutionTime>
76
+ #</ApiResponse>
77
+
78
+
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/../lib/namecheap'
3
+
4
+ describe NamecheapResponse do
5
+ before(:each) do
6
+ @bad_response_hash = {"ApiResponse"=>{"Status"=>"ERROR",
7
+ "Errors"=>{"Error"=>"API Key is invalid or API access has not been enabled"},
8
+ "GMTTimeDifference"=>"--6:00",
9
+ "RequestedCommand"=>"namecheap.domains.check",
10
+ "Server"=>"SERVER159",
11
+ "ExecutionTime"=>"0.01",
12
+ "xmlns"=>"http://api.namecheap.com/xml.response"}}
13
+ end
14
+
15
+ it "should return a status" do
16
+ response = NamecheapResponse.new(@bad_response_hash)
17
+ response.status.should == "ERROR"
18
+ end
19
+
20
+ it "should give error message for invalid api key when using an invalid key" do
21
+ response = NamecheapResponse.new(@bad_response_hash)
22
+ response.message.should include("API Key is invalid")
23
+ end
24
+ end
25
+
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/../lib/namecheap'
3
+ require 'mocha'
4
+
5
+ describe "NamecheapAPI Wrapper" do
6
+ describe "initializating settings" do
7
+ describe "with defaults" do
8
+ it "should contain a username" do
9
+ namecheap = Namecheap.new
10
+ namecheap.send(:username).should == 'apiuser'
11
+ end
12
+ it "should contain a key" do
13
+ namecheap = Namecheap.new
14
+ namecheap.send(:key).should == 'apikey'
15
+ end
16
+ it "should contain a client_ip" do
17
+ namecheap = Namecheap.new
18
+ namecheap.send(:client_ip).should == '127.0.0.1'
19
+ end
20
+ end
21
+
22
+ describe "with defaults overidden" do
23
+ it "shoud contain a overidden username" do
24
+ namecheap = Namecheap.new(:username => 'testuser')
25
+ namecheap.send(:username).should == 'testuser'
26
+ end
27
+
28
+ it "shoud contain a key" do
29
+ namecheap = Namecheap.new(:key => 'testkey')
30
+ namecheap.send(:key).should == 'testkey'
31
+ end
32
+ it "shoud contain a client_ip" do
33
+ namecheap = Namecheap.new(:client_ip => '66.11.22.44')
34
+ namecheap.send(:client_ip).should == '66.11.22.44'
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "Attempt to connect with bad credentials" do
40
+ it "should report an error on erroneous account information" do
41
+ namecheap = Namecheap.new
42
+ namecheap.domain_check("fakedomain").status.should == "ERROR"
43
+ end
44
+
45
+ it "should give error message for invalid api key when using an invalid key" do
46
+ namecheap = Namecheap.new
47
+ namecheap.domain_check("fakedomain").message.should include("API Key is invalid")
48
+ end
49
+ end
50
+
51
+ describe "Attempt to connect with valid credentials" do
52
+
53
+ end
54
+
55
+ describe "#domain_check" do
56
+ it "should build query with multiple domains" do
57
+ namecheap = Namecheap.new()
58
+ namecheap.expects(:do_query).with("namecheap.domains.check", "&DomainList=domain1.com,domain2.com")
59
+ namecheap.domain_check(['domain1.com','domain2.com'])
60
+ end
61
+ end
62
+
63
+ describe "#is_domain_available?" do
64
+ it "should return false if connection fails" do
65
+ namecheap = Namecheap.new(:apikey => 'BADKEY')
66
+ lambda {
67
+ namecheap.is_domain_available?('fakedomain.tld').should be_false
68
+ }.should raise_error(NilNamecheapResponse)
69
+ end
70
+
71
+ it "should return true if connections succeeds and domain is available" do
72
+ pending "Need API Access To Namecheap.com"
73
+ namecheap = Namecheap.new
74
+ namecheap.is_domain_available?('saucytuborswithashoefetish.com').should be_true
75
+ end
76
+
77
+ it "should return false if connections succeeds and domain is not available" do
78
+ namecheap = Namecheap.new
79
+ lambda {
80
+ namecheap.is_domain_available?('hashrocket.com').should be_false
81
+ }.should raise_error(NilNamecheapResponse)
82
+ end
83
+ end
84
+ end
85
+
@@ -0,0 +1,12 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ require 'activesupport'
10
+ require 'activerecord'
11
+
12
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namecheap
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - parasquid
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-07 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: httparty
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Ruby wrapper for the Namecheap API
49
+ email:
50
+ - tristan.gomezd@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - .rvmrc
60
+ - Gemfile
61
+ - README.textile
62
+ - Rakefile
63
+ - lib/namecheap.rb
64
+ - lib/namecheap.yml
65
+ - lib/namecheap/version.rb
66
+ - namecheap.gemspec
67
+ - spec/namecheap_domain_check_response_spec.rb
68
+ - spec/namecheap_response_spec.rb
69
+ - spec/namecheap_spec.rb
70
+ - spec/spec_helper.rb
71
+ homepage: https://github.com/parasquid/namecheap
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project: namecheap
100
+ rubygems_version: 1.8.10
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Ruby wrapper for the Namecheap API
104
+ test_files:
105
+ - spec/namecheap_domain_check_response_spec.rb
106
+ - spec/namecheap_response_spec.rb
107
+ - spec/namecheap_spec.rb
108
+ - spec/spec_helper.rb