Class: Dyn::Traffic::Client
- Inherits:
-
Object
- Object
- Dyn::Traffic::Client
- Defined in:
- lib/dyn/traffic.rb
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#customer_name ⇒ Object
Returns the value of attribute customer_name.
-
#password ⇒ Object
Returns the value of attribute password.
-
#rest ⇒ Object
Returns the value of attribute rest.
-
#user_name ⇒ Object
Returns the value of attribute user_name.
Class Method Summary collapse
-
.underscore(string) ⇒ Object
Convert a CamelCasedString to an under_scored_string.
Instance Method Summary collapse
-
#api_request(&block) ⇒ Object
Handles making Dynect API requests and formatting the responses properly.
-
#delete(path_part, additional_headers = {}, &block) ⇒ Object
Raw DELETE request, formatted for Dyn.
-
#discard_change_set ⇒ Object
for convenience…
-
#get(path_part, additional_headers = {}, &block) ⇒ Object
Raw GET request, formatted for Dyn.
-
#gslb(options = {}) ⇒ Object
GSLB API.
-
#http_redirect(options = {}) ⇒ Object
HTTPRedirect API.
-
#initialize(customer_name, user_name, password, zone = nil, connect = true, verbose = false, max_redirects = 10) ⇒ Client
constructor
Creates a new base object for interacting with Dyn’s REST API.
-
#login ⇒ Hash
Login to Dyn - must be done before any other methods called.
-
#logout ⇒ Hash
Logout of Dyn - generally the last operation performed.
- #parse_response(response) ⇒ Object
-
#post(path_part, payload, additional_headers = {}, &block) ⇒ Object
Raw POST request, formatted for Dyn.
-
#publish ⇒ Object
for convenience…
-
#put(path_part, payload, additional_headers = {}, &block) ⇒ Object
Raw PUT request, formatted for Dyn.
-
#record_type ⇒ Object
Resource Record API.
-
#session ⇒ Object
Session API.
-
#zone ⇒ Object
Zone API.
-
#zone=(zone) ⇒ Object
Zone attribute setter.
Constructor Details
#initialize(customer_name, user_name, password, zone = nil, connect = true, verbose = false, max_redirects = 10) ⇒ Client
Creates a new base object for interacting with Dyn’s REST API
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dyn/traffic.rb', line 50 def initialize(customer_name, user_name, password, zone=nil, connect=true, verbose=false, max_redirects=10) @customer_name = customer_name @user_name = user_name @password = password @rest = Dyn::HttpClient::DefaultClient.new("api2.dynect.net", "443", "https") @rest.default_headers = { 'User-Agent' => Dyn::VERSION, 'Content-Type' => 'application/json' } @zone = zone @verbose = verbose @session = Dyn::Traffic::Session.new(self) login if connect end |
Instance Attribute Details
#auth_token ⇒ Object
Returns the value of attribute auth_token.
40 41 42 |
# File 'lib/dyn/traffic.rb', line 40 def auth_token @auth_token end |
#customer_name ⇒ Object
Returns the value of attribute customer_name.
40 41 42 |
# File 'lib/dyn/traffic.rb', line 40 def customer_name @customer_name end |
#password ⇒ Object
Returns the value of attribute password.
40 41 42 |
# File 'lib/dyn/traffic.rb', line 40 def password @password end |
#rest ⇒ Object
Returns the value of attribute rest.
40 41 42 |
# File 'lib/dyn/traffic.rb', line 40 def rest @rest end |
#user_name ⇒ Object
Returns the value of attribute user_name.
40 41 42 |
# File 'lib/dyn/traffic.rb', line 40 def user_name @user_name end |
Class Method Details
.underscore(string) ⇒ Object
Convert a CamelCasedString to an under_scored_string.
135 136 137 138 139 140 141 142 143 |
# File 'lib/dyn/traffic.rb', line 135 def self.underscore(string) word = string.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |
Instance Method Details
#api_request(&block) ⇒ Object
Handles making Dynect API requests and formatting the responses properly.
201 202 203 204 205 206 207 208 |
# File 'lib/dyn/traffic.rb', line 201 def api_request(&block) response = block.call if response.status == 307 and response.body =~ /^\/REST\// response.body.sub!('/REST/','') response = get(response.body) end parse_response(response.body && response.body.length >= 2 ? JSON.parse(response.body) : {}) end |
#delete(path_part, additional_headers = {}, &block) ⇒ Object
Raw DELETE request, formatted for Dyn. See list of endpoints at:
170 171 172 |
# File 'lib/dyn/traffic.rb', line 170 def delete(path_part, additional_headers = {}, &block) api_request { @rest.delete('/REST/' + path_part, "", additional_headers, &block) } end |
#discard_change_set ⇒ Object
for convenience…
95 96 97 |
# File 'lib/dyn/traffic.rb', line 95 def discard_change_set zone.discard_change_set end |
#get(path_part, additional_headers = {}, &block) ⇒ Object
Raw GET request, formatted for Dyn. See list of endpoints at:
160 161 162 |
# File 'lib/dyn/traffic.rb', line 160 def get(path_part, additional_headers = {}, &block) api_request { @rest.get('/REST/' + path_part, nil, additional_headers, &block) } end |
#gslb(options = {}) ⇒ Object
GSLB API
123 124 125 |
# File 'lib/dyn/traffic.rb', line 123 def gslb( = {}) Dyn::Traffic::GSLB.new(self, @zone, ) end |
#http_redirect(options = {}) ⇒ Object
HTTPRedirect API
109 110 111 |
# File 'lib/dyn/traffic.rb', line 109 def http_redirect( = {}) Dyn::Traffic::HTTPRedirect.new(self, @zone, ) end |
#login ⇒ Hash
Login to Dyn - must be done before any other methods called.
See: manage.dynect.net/help/docs/api2/rest/resources/Session.html
70 71 72 73 74 75 |
# File 'lib/dyn/traffic.rb', line 70 def login response = @session.create @auth_token = response["token"] @rest.default_headers = { 'Content-Type' => 'application/json', 'Auth-Token' => @auth_token } response end |
#logout ⇒ Hash
Logout of Dyn - generally the last operation performed
See: manage.dynect.net/help/docs/api2/rest/resources/Session.html
82 83 84 85 86 87 |
# File 'lib/dyn/traffic.rb', line 82 def logout response = @session.delete @auth_token = nil @rest.default_headers = { 'Content-Type' => 'application/json' } response end |
#parse_response(response) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/dyn/traffic.rb', line 210 def parse_response(response) case response["status"] when "success" response["data"] when "incomplete" # we get 'incomplete' when the API is running slow and claims the session has a previous job running # raise an error and return the job ID in case we want to ask the API what the job's status is = [] .push( "This session may have a job _still_ running (slowly). Call /REST/Job/#{response["job_id"]} to get its status." ) if response["msgs"] response["msgs"].each do || << "#{["LVL"]} #{["ERR_CD"]} #{["SOURCE"]} - #{["INFO"]}" end end raise Dyn::Exceptions::IncompleteRequest.new( "#{.join("\n")}", response["job_id"] ) when "failure" = [] response["msgs"].each do || << "#{["LVL"]} #{["ERR_CD"]} #{["SOURCE"]} - #{["INFO"]}" end raise Dyn::Exceptions::RequestFailed, "Request failed: #{.join("\n")}" end end |
#post(path_part, payload, additional_headers = {}, &block) ⇒ Object
Raw POST request, formatted for Dyn. See list of endpoints at:
manage.dynect.net/help/docs/api2/rest/resources/
Read the API documentation, and submit the proper data structure from here.
183 184 185 |
# File 'lib/dyn/traffic.rb', line 183 def post(path_part, payload, additional_headers = {}, &block) api_request { @rest.post('/REST/' + path_part, payload.to_json, additional_headers, &block) } end |
#publish ⇒ Object
for convenience…
90 91 92 |
# File 'lib/dyn/traffic.rb', line 90 def publish zone.publish end |
#put(path_part, payload, additional_headers = {}, &block) ⇒ Object
Raw PUT request, formatted for Dyn. See list of endpoints at:
manage.dynect.net/help/docs/api2/rest/resources/
Read the API documentation, and submit the proper data structure from here.
196 197 198 |
# File 'lib/dyn/traffic.rb', line 196 def put(path_part, payload, additional_headers = {}, &block) api_request { @rest.put('/REST/' + path_part, payload.to_json, additional_headers, &block) } end |
#record_type ⇒ Object
Resource Record API
148 149 150 151 152 |
# File 'lib/dyn/traffic.rb', line 148 %w{AAAA A CNAME DNSKEY DS KEY LOC MX NS PTR RP SOA SRV TXT SSHFP}.each do |record_type| define_method underscore(record_type) do Dyn::Traffic::Resource.new(self, @zone, "#{record_type}") end end |
#session ⇒ Object
Session API
116 117 118 |
# File 'lib/dyn/traffic.rb', line 116 def session Dyn::Traffic::Session.new(self) end |
#zone ⇒ Object
Zone API
130 131 132 |
# File 'lib/dyn/traffic.rb', line 130 def zone Dyn::Traffic::Zone.new(self, @zone) end |
#zone=(zone) ⇒ Object
Zone attribute setter
102 103 104 |
# File 'lib/dyn/traffic.rb', line 102 def zone=(zone) @zone = zone end |