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.
-
#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-rb 1.0.3', '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.
130 131 132 133 134 135 136 137 138 |
# File 'lib/dyn/traffic.rb', line 130 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.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/dyn/traffic.rb', line 196 def api_request(&block) response_body = begin response = block.call response.body rescue Exception => e if @verbose puts "I have #{e.inspect} with #{e.http_code}" end if e.http_code == 307 e.response.sub!('/REST/','') if e.response =~ /^\/REST\// get(e.response) end e.response end parse_response(JSON.parse(response_body || '{}')) end |
#delete(path_part, additional_headers = {}, &block) ⇒ Object
Raw DELETE request, formatted for Dyn. See list of endpoints at:
165 166 167 |
# File 'lib/dyn/traffic.rb', line 165 def delete(path_part, additional_headers = {}, &block) api_request { @rest.delete('/REST/' + path_part, "", additional_headers, &block) } end |
#get(path_part, additional_headers = {}, &block) ⇒ Object
Raw GET request, formatted for Dyn. See list of endpoints at:
155 156 157 |
# File 'lib/dyn/traffic.rb', line 155 def get(path_part, additional_headers = {}, &block) api_request { @rest.get('/REST/' + path_part, nil, additional_headers, &block) } end |
#gslb(options = {}) ⇒ Object
GSLB API
118 119 120 |
# File 'lib/dyn/traffic.rb', line 118 def gslb( = {}) Dyn::Traffic::GSLB.new(self, @zone, ) end |
#http_redirect(options = {}) ⇒ Object
HTTPRedirect API
104 105 106 |
# File 'lib/dyn/traffic.rb', line 104 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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/dyn/traffic.rb', line 215 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." ) response["msgs"].each do || << "#{["LVL"]} #{["ERR_CD"]} #{["SOURCE"]} - #{["INFO"]}" 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.
178 179 180 |
# File 'lib/dyn/traffic.rb', line 178 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.
191 192 193 |
# File 'lib/dyn/traffic.rb', line 191 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
143 144 145 146 147 |
# File 'lib/dyn/traffic.rb', line 143 %w{AAAA A CNAME DNSKEY DS KEY LOC MX NS PTR RP SOA SRV TXT}.each do |record_type| define_method underscore(record_type) do Dyn::Traffic::Resource.new(self, @zone, "#{record_type}") end end |
#session ⇒ Object
Session API
111 112 113 |
# File 'lib/dyn/traffic.rb', line 111 def session Dyn::Traffic::Session.new(self) end |
#zone ⇒ Object
Zone API
125 126 127 |
# File 'lib/dyn/traffic.rb', line 125 def zone Dyn::Traffic::Zone.new(self, @zone) end |
#zone=(zone) ⇒ Object
Zone attribute setter
97 98 99 |
# File 'lib/dyn/traffic.rb', line 97 def zone=(zone) @zone = zone end |