Class: DNSimple::Client
- Inherits:
-
Object
- Object
- DNSimple::Client
- Defined in:
- lib/dnsimple/client.rb
Constant Summary collapse
- DEFAULT_BASE_URI =
"https://api.dnsimple.com/"
Class Method Summary collapse
- .api_token ⇒ Object
- .api_token=(api_token) ⇒ Object
- .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
- .debug=(debug) ⇒ Object
- .debug? ⇒ Boolean
- .delete(path, options = {}) ⇒ Object
- .get(path, options = {}) ⇒ Object
- .http_proxy ⇒ Object
- .http_proxy=(http_proxy) ⇒ Object
- .load_credentials(path = config_path) ⇒ Object
- .load_credentials_if_necessary ⇒ Object
- .password ⇒ Object
- .password=(password) ⇒ Object
- .post(path, options = {}) ⇒ Object
- .put(path, options = {}) ⇒ Object
- .request(method, path, options) ⇒ Object
- .username ⇒ Object
- .username=(username) ⇒ Object
Class Method Details
.api_token ⇒ Object
34 35 36 |
# File 'lib/dnsimple/client.rb', line 34 def self.api_token @api_token end |
.api_token=(api_token) ⇒ Object
38 39 40 |
# File 'lib/dnsimple/client.rb', line 38 def self.api_token=(api_token) @api_token = api_token end |
.base_options ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/dnsimple/client.rb', line 93 def self. = { :format => :json, :headers => { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{DNSimple::VERSION}" }, } if http_proxy .merge!( :http_proxyaddr => self.http_proxy[:addr], :http_proxyport => self.http_proxy[:port] ) end if password [:basic_auth] = { :username => username, :password => password } elsif api_token [:headers]['X-DNSimple-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.
45 46 47 |
# File 'lib/dnsimple/client.rb', line 45 def self.base_uri @base_uri ||= DEFAULT_BASE_URI.chomp("/") end |
.base_uri=(value) ⇒ Object
Sets the qualified API base uri.
52 53 54 |
# File 'lib/dnsimple/client.rb', line 52 def self.base_uri=(value) @base_uri = value.to_s.chomp("/") end |
.config_path ⇒ Object
68 69 70 |
# File 'lib/dnsimple/client.rb', line 68 def self.config_path ENV['DNSIMPLE_CONFIG'] || '~/.dnsimple' end |
.credentials_loaded? ⇒ Boolean
89 90 91 |
# File 'lib/dnsimple/client.rb', line 89 def self.credentials_loaded? (@credentials_loaded ||= false) or (username and (password or api_token)) end |
.debug=(debug) ⇒ Object
14 15 16 |
# File 'lib/dnsimple/client.rb', line 14 def self.debug=(debug) @debug = debug end |
.debug? ⇒ Boolean
10 11 12 |
# File 'lib/dnsimple/client.rb', line 10 def self.debug? @debug end |
.delete(path, options = {}) ⇒ Object
129 130 131 |
# File 'lib/dnsimple/client.rb', line 129 def self.delete(path, = {}) request :delete, path, end |
.get(path, options = {}) ⇒ Object
117 118 119 |
# File 'lib/dnsimple/client.rb', line 117 def self.get(path, = {}) request :get, path, end |
.http_proxy ⇒ Object
56 57 58 |
# File 'lib/dnsimple/client.rb', line 56 def self.http_proxy @http_proxy end |
.http_proxy=(http_proxy) ⇒ Object
60 61 62 |
# File 'lib/dnsimple/client.rb', line 60 def self.http_proxy=(http_proxy) @http_proxy = http_proxy end |
.load_credentials(path = config_path) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dnsimple/client.rb', line 72 def self.load_credentials(path = config_path) begin credentials = YAML.load_file(File.(path)) self.username ||= credentials['username'] self.password ||= credentials['password'] self.api_token ||= credentials['api_token'] self.base_uri = credentials['site'] if credentials['site'] self.base_uri = credentials['base_uri'] if credentials['base_uri'] self.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
64 65 66 |
# File 'lib/dnsimple/client.rb', line 64 def self.load_credentials_if_necessary load_credentials unless credentials_loaded? end |
.password ⇒ Object
26 27 28 |
# File 'lib/dnsimple/client.rb', line 26 def self.password @password end |
.password=(password) ⇒ Object
30 31 32 |
# File 'lib/dnsimple/client.rb', line 30 def self.password=(password) @password = password end |
.post(path, options = {}) ⇒ Object
121 122 123 |
# File 'lib/dnsimple/client.rb', line 121 def self.post(path, = {}) request :post, path, end |
.put(path, options = {}) ⇒ Object
125 126 127 |
# File 'lib/dnsimple/client.rb', line 125 def self.put(path, = {}) request :put, path, end |
.request(method, path, options) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/dnsimple/client.rb', line 133 def self.request(method, path, ) response = HTTParty.send(method, "#{base_uri}#{path}", .merge()) if response.code == 401 raise AuthenticationFailed end response end |
.username ⇒ Object
18 19 20 |
# File 'lib/dnsimple/client.rb', line 18 def self.username @username end |
.username=(username) ⇒ Object
22 23 24 |
# File 'lib/dnsimple/client.rb', line 22 def self.username=(username) @username = username end |