Class: DNSimple::Client

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.api_tokenObject



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_optionsObject



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.base_options
  options = {
    :format => :json,
    :headers => { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{DNSimple::VERSION}" },
  }

  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

.base_uriString

Gets the qualified API base uri.

Returns:

  • (String)

    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.

Parameters:

  • value (String)

    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_pathObject



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

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

.credentials_loaded?Boolean

Returns:

  • (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

Returns:

  • (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, options = {})
  request :delete, path, options
end

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



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

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

.http_proxyObject



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.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



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

def self.load_credentials_if_necessary
  load_credentials unless credentials_loaded?
end

.passwordObject



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, options = {})
  request :post, path, options
end

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



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

def self.put(path, options = {})
  request :put, path, options
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, options)
  response = HTTParty.send(method, "#{base_uri}#{path}", base_options.merge(options))

  if response.code == 401
    raise AuthenticationFailed
  end

  response
end

.usernameObject



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