Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports explicit endpoint configuration #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
15 changes: 10 additions & 5 deletions lib/namecheap/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/namecheap/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions spec/namecheap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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