Class: DNSimple::Client
- Inherits:
-
Object
- Object
- DNSimple::Client
- Defined in:
- lib/dnsimple/client.rb
Constant Summary collapse
- DEFAULT_BASE_URI =
"https://api.dnsimple.com/"
- HEADER_2FA_STRICT =
"X-DNSimple-2FA-Strict"
- HEADER_API_TOKEN =
"X-DNSimple-Token"
- HEADER_DOMAIN_API_TOKEN =
"X-DNSimple-Domain-Token"
- HEADER_OTP_TOKEN =
"X-DNSimple-OTP"
- HEADER_EXCHANGE_TOKEN =
"X-DNSimple-OTP-Token"
Class Attribute Summary collapse
-
.api_token ⇒ Object
Returns the value of attribute api_token.
-
.debug ⇒ Boolean
If the debug mode is enabled.
-
.domain_api_token ⇒ Object
Returns the value of attribute domain_api_token.
-
.exchange_token ⇒ Object
Returns the value of attribute exchange_token.
-
.password ⇒ Object
Returns the value of attribute password.
-
.username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
- .base_options ⇒ Object
-
.base_uri ⇒ String
Gets the qualified API base uri.
-
.base_uri=(value) ⇒ Object
Sets the qualified API base uri.
- .config_path ⇒ Object
- .credentials_loaded? ⇒ Boolean
- .delete(path, options = {}) ⇒ Object
- .get(path, options = {}) ⇒ Object
- .http_proxy ⇒ Object
- .load_credentials(path = config_path) ⇒ Object
- .load_credentials_if_necessary ⇒ Object
- .post(path, options = {}) ⇒ Object
- .put(path, options = {}) ⇒ Object
- .request(method, path, options) ⇒ Object
Class Attribute Details
.api_token ⇒ Object
Returns the value of attribute api_token.
19 20 21 |
# File 'lib/dnsimple/client.rb', line 19 def api_token @api_token end |
.debug ⇒ Boolean
Returns if the debug mode is enabled. Defaults to false.
17 18 19 |
# File 'lib/dnsimple/client.rb', line 17 def debug @debug end |
.domain_api_token ⇒ Object
Returns the value of attribute domain_api_token.
19 20 21 |
# File 'lib/dnsimple/client.rb', line 19 def domain_api_token @domain_api_token end |
.exchange_token ⇒ Object
Returns the value of attribute exchange_token.
19 20 21 |
# File 'lib/dnsimple/client.rb', line 19 def exchange_token @exchange_token end |
.password ⇒ Object
Returns the value of attribute password.
19 20 21 |
# File 'lib/dnsimple/client.rb', line 19 def password @password end |
.username ⇒ Object
Returns the value of attribute username.
19 20 21 |
# File 'lib/dnsimple/client.rb', line 19 def username @username end |
Class Method Details
.base_options ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dnsimple/client.rb', line 71 def self. = { :format => :json, :headers => { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{DNSimple::VERSION}" }, } if http_proxy .merge!( :http_proxyaddr => http_proxy[:addr], :http_proxyport => http_proxy[:port] ) end if exchange_token [:basic_auth] = { :username => exchange_token, :password => "x-2fa-basic" } elsif password [:basic_auth] = { :username => username, :password => password } elsif domain_api_token [:headers][HEADER_DOMAIN_API_TOKEN] = domain_api_token elsif api_token [:headers][HEADER_API_TOKEN] = "#{username}:#{api_token}" else raise Error, 'A password or API token is required for all API requests.' end end |
.base_uri ⇒ String
Gets the qualified API base uri.
25 26 27 |
# File 'lib/dnsimple/client.rb', line 25 def self.base_uri @base_uri ||= DEFAULT_BASE_URI.chomp("/") end |
.base_uri=(value) ⇒ Object
Sets the qualified API base uri.
32 33 34 |
# File 'lib/dnsimple/client.rb', line 32 def self.base_uri=(value) @base_uri = value.to_s.chomp("/") end |
.config_path ⇒ Object
44 45 46 |
# File 'lib/dnsimple/client.rb', line 44 def self.config_path ENV['DNSIMPLE_CONFIG'] || '~/.dnsimple' end |
.credentials_loaded? ⇒ Boolean
67 68 69 |
# File 'lib/dnsimple/client.rb', line 67 def self.credentials_loaded? (@credentials_loaded ||= false) or domain_api_token or (username and (password or api_token)) end |
.delete(path, options = {}) ⇒ Object
111 112 113 |
# File 'lib/dnsimple/client.rb', line 111 def self.delete(path, = {}) request :delete, path, end |
.get(path, options = {}) ⇒ Object
99 100 101 |
# File 'lib/dnsimple/client.rb', line 99 def self.get(path, = {}) request :get, path, end |
.http_proxy ⇒ Object
36 37 38 |
# File 'lib/dnsimple/client.rb', line 36 def self.http_proxy @http_proxy end |
.load_credentials(path = config_path) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dnsimple/client.rb', line 48 def self.load_credentials(path = config_path) begin credentials = YAML.load_file(File.(path)) self.username = credentials['username'] self.password = credentials['password'] self.exchange_token = credentials['exchange_token'] self.api_token = credentials['api_token'] self.domain_api_token = credentials['domain_api_token'] self.base_uri = credentials['site'] if credentials['site'] self.base_uri = credentials['base_uri'] if credentials['base_uri'] @http_proxy = { :addr => credentials['proxy_addr'], :port => credentials['proxy_port'] } if credentials['proxy_addr'] || credentials['proxy_port'] @credentials_loaded = true puts "Credentials loaded from #{path}" rescue => error puts "Error loading your credentials: #{error.}" exit 1 end end |
.load_credentials_if_necessary ⇒ Object
40 41 42 |
# File 'lib/dnsimple/client.rb', line 40 def self.load_credentials_if_necessary load_credentials unless credentials_loaded? end |
.post(path, options = {}) ⇒ Object
103 104 105 |
# File 'lib/dnsimple/client.rb', line 103 def self.post(path, = {}) request :post, path, end |
.put(path, options = {}) ⇒ Object
107 108 109 |
# File 'lib/dnsimple/client.rb', line 107 def self.put(path, = {}) request :put, path, end |
.request(method, path, options) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dnsimple/client.rb', line 115 def self.request(method, path, ) response = HTTParty.send(method, "#{base_uri}#{path}", .merge()) if response.code == 401 && response.headers[HEADER_OTP_TOKEN] == "required" raise TwoFactorAuthenticationRequired, response["message"] elsif response.code == 401 raise AuthenticationFailed, response["message"] end response end |