Class: DNSimple::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsimple/client.rb

Constant Summary collapse

API_BASE_URI =
"https://api.dnsimple.com/"

Class Method Summary collapse

Class Method Details

.api_tokenObject



33
34
35
# File 'lib/dnsimple/client.rb', line 33

def self.api_token
  @api_token
end

.api_token=(api_token) ⇒ Object



37
38
39
# File 'lib/dnsimple/client.rb', line 37

def self.api_token=(api_token)
  @api_token = api_token
end

.base_uriString

Gets the qualified API base uri.

Returns:

  • (String)

    The qualified API base uri.



44
45
46
# File 'lib/dnsimple/client.rb', line 44

def self.base_uri
  @base_uri ||= API_BASE_URI.chomp("/")
end

.base_uri=(value) ⇒ Object

 Sets the qualified API base uri.

Parameters:

  • value (String)

    The qualified API base uri.



51
52
53
# File 'lib/dnsimple/client.rb', line 51

def self.base_uri=(value)
  @base_uri = value.to_s.chomp("/")
end

.config_pathObject



67
68
69
# File 'lib/dnsimple/client.rb', line 67

def self.config_path
  ENV['DNSIMPLE_CONFIG'] || '~/.dnsimple'
end

.credentials_loaded?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/dnsimple/client.rb', line 88

def self.credentials_loaded?
  (@credentials_loaded ||= false) or (username and (password or api_token))
end

.debug=(debug) ⇒ Object



13
14
15
# File 'lib/dnsimple/client.rb', line 13

def self.debug=(debug)
  @debug = debug
end

.debug?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/dnsimple/client.rb', line 9

def self.debug?
  @debug
end

.delete(path, options = {}) ⇒ Object



128
129
130
# File 'lib/dnsimple/client.rb', line 128

def self.delete(path, options = {})
  request :delete, path, options
end

.get(path, options = {}) ⇒ Object



116
117
118
# File 'lib/dnsimple/client.rb', line 116

def self.get(path, options = {})
  request :get, path, options
end

.http_proxyObject



55
56
57
# File 'lib/dnsimple/client.rb', line 55

def self.http_proxy
  @http_proxy
end

.http_proxy=(http_proxy) ⇒ Object



59
60
61
# File 'lib/dnsimple/client.rb', line 59

def self.http_proxy=(http_proxy)
  @http_proxy = http_proxy
end

.load_credentials(path = config_path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dnsimple/client.rb', line 71

def self.load_credentials(path = config_path)
  begin
    credentials = YAML.load_file(File.expand_path(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.message}"
    exit 1
  end
end

.load_credentials_if_necessaryObject



63
64
65
# File 'lib/dnsimple/client.rb', line 63

def self.load_credentials_if_necessary
  load_credentials unless credentials_loaded?
end

.passwordObject



25
26
27
# File 'lib/dnsimple/client.rb', line 25

def self.password
  @password
end

.password=(password) ⇒ Object



29
30
31
# File 'lib/dnsimple/client.rb', line 29

def self.password=(password)
  @password = password
end

.post(path, options = {}) ⇒ Object



120
121
122
# File 'lib/dnsimple/client.rb', line 120

def self.post(path, options = {})
  request :post, path, options
end

.put(path, options = {}) ⇒ Object



124
125
126
# File 'lib/dnsimple/client.rb', line 124

def self.put(path, options = {})
  request :put, path, options
end

.request(method, path, options) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/dnsimple/client.rb', line 132

def self.request(method, path, options)
  response = HTTParty.send(method, "#{base_uri}#{path}",
    standard_options.merge(options))

  if response.code == 401
    raise AuthenticationFailed
  end

  response
end

.standard_optionsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dnsimple/client.rb', line 92

def self.standard_options
  options = {
              :format => :json,
              :headers => {'Accept' => 'application/json'},
            }

  if http_proxy
    options.merge!(
      :http_proxyaddr => self.http_proxy[:addr],
      :http_proxyport => self.http_proxy[:port]
    )
  end

  if password
    options[:basic_auth] = {:username => username, :password => password}
  elsif api_token
    options[:headers]['X-DNSimple-Token'] = "#{username}:#{api_token}"
  else
    raise Error, 'A password or API token is required for all API requests.'
  end

  options
end

.usernameObject



17
18
19
# File 'lib/dnsimple/client.rb', line 17

def self.username
  @username
end

.username=(username) ⇒ Object



21
22
23
# File 'lib/dnsimple/client.rb', line 21

def self.username=(username)
  @username = username
end