From 17b8102a51b8455f4aa472fcde6b8ae66d572ff8 Mon Sep 17 00:00:00 2001 From: James Kassemi Date: Fri, 12 Sep 2014 22:50:57 -0600 Subject: [PATCH] Supports explicit endpoint configuration --- README.md | 2 ++ lib/namecheap/api.rb | 15 ++++++++++----- lib/namecheap/config.rb | 2 +- spec/namecheap_spec.rb | 9 +++++++++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c5826e0..db4f307 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ Namecheap.configure do |config| config.key = 'apikey' config.username = 'apiuser' config.client_ip = '127.0.0.1' + config.endpoint = 'https://api.sandbox.namecheap.com/xml.response' + # config.endpoint = 'https://api.namecheap.com/xml.response' end ``` diff --git a/lib/namecheap/api.rb b/lib/namecheap/api.rb index a575314..dde6bf0 100644 --- a/lib/namecheap/api.rb +++ b/lib/namecheap/api.rb @@ -5,7 +5,6 @@ class Api SANDBOX = 'https://api.sandbox.namecheap.com/xml.response' PRODUCTION = 'https://api.namecheap.com/xml.response' ENVIRONMENT = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : (ENV["RACK_ENV"] || 'development') - ENDPOINT = (ENVIRONMENT == 'production' ? PRODUCTION : SANDBOX) def get(command, options = {}) request 'get', command, options @@ -33,16 +32,17 @@ def request(method, command, options = {}) case method when 'get' #raise options.inspect - HTTParty.get(ENDPOINT, { :query => options}) + HTTParty.get(endpoint, { :query => options}) when 'post' - HTTParty.post(ENDPOINT, { :query => options}) + HTTParty.post(endpoint, { :query => options}) when 'put' - HTTParty.put(ENDPOINT, { :query => options}) + HTTParty.put(endpoint, { :query => options}) when 'delete' - HTTParty.delete(ENDPOINT, { :query => options}) + HTTParty.delete(endpoint, { :query => options}) end end + private def init_args %w(username key client_ip).each do |key| if Namecheap.config.key.nil? @@ -58,5 +58,10 @@ def init_args client_ip: Namecheap.config.client_ip } end + + def endpoint + Namecheap.config.endpoint || + (ENVIRONMENT == 'production' ? PRODUCTION : SANDBOX) + end end end diff --git a/lib/namecheap/config.rb b/lib/namecheap/config.rb index 9736a49..cbb46f8 100644 --- a/lib/namecheap/config.rb +++ b/lib/namecheap/config.rb @@ -3,7 +3,7 @@ module Config class RequiredOptionMissing < RuntimeError ; end extend self - attr_accessor :key, :username, :client_ip + attr_accessor :key, :username, :client_ip, :endpoint # Configure namecheap from a hash. This is usually called after parsing a # yaml config file such as mongoid.yml. diff --git a/spec/namecheap_spec.rb b/spec/namecheap_spec.rb index ff6e3b3..563bb40 100644 --- a/spec/namecheap_spec.rb +++ b/spec/namecheap_spec.rb @@ -9,6 +9,7 @@ its(:username) { should be_nil } its(:key) { should be_nil } its(:client_ip) { should be_nil } + its(:endpoint) { should be_nil } end describe '.configure' do @@ -35,5 +36,13 @@ end }.to change { Namecheap::Config.client_ip }.to('the_client_ip') end + + it 'should set the endpoint' do + expect { + Namecheap.configure do |config| + config.endpoint = 'the_endpoint' + end + }.to change { Namecheap::Config.endpoint }.to('the_endpoint') + end end end