Class: Vinyldns::API

Inherits:
Object
  • Object
show all
Defined in:
lib/vinyldns/api.rb,
lib/vinyldns/api/zone/zone.rb,
lib/vinyldns/api/group/group.rb

Defined Under Namespace

Classes: Group, Zone

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, region = 'us-east-1', api_url = ENV['VINYLDNS_API_URL'], content_type = 'application/x-www-form-urlencoded') ⇒ API

Required arguments:

  • method

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vinyldns/api.rb', line 22

def initialize(method, region = 'us-east-1', api_url = ENV['VINYLDNS_API_URL'], content_type = 'application/x-www-form-urlencoded')
  @api_url = api_url
  @method = method.upcase
  raise(ArgumentError, 'Not a valid http request method') unless %w[GET HEAD POST PUT DELETE TRACE OPTIONS CONNECT PATCH].include?(@method)
  @region = region
  if @method == 'GET'
    @content_type = content_type
  elsif @method == 'POST' || @method == 'PUT'
    @content_type = 'application/json'
  end
  # Generate a signed header for our HTTP requests
  # http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Sigv4/Signer.html
  @signer = Aws::Sigv4::Signer.new(
      service: 'VinylDNS',
      region: 'us-east-1',
      access_key_id: ENV['VINYLDNS_ACCESS_KEY_ID'],
      secret_access_key: ENV['VINYLDNS_SECRET_ACCESS_KEY'],
      apply_checksum_header: false # Required for posting body in make_request : http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Sigv4/Signer.html : If the 'X-Amz-Content-Sha256' header is set, the :body is optional and will not be read.
  )
end

Instance Attribute Details

#api_urlObject

Make sure we can access parameters of the object



19
20
21
# File 'lib/vinyldns/api.rb', line 19

def api_url
  @api_url
end

#bodyObject

Make sure we can access parameters of the object



19
20
21
# File 'lib/vinyldns/api.rb', line 19

def body
  @body
end

#content_typeObject

Make sure we can access parameters of the object



19
20
21
# File 'lib/vinyldns/api.rb', line 19

def content_type
  @content_type
end

#methodObject

Make sure we can access parameters of the object



19
20
21
# File 'lib/vinyldns/api.rb', line 19

def method
  @method
end

#regionObject

Make sure we can access parameters of the object



19
20
21
# File 'lib/vinyldns/api.rb', line 19

def region
  @region
end

#signerObject

Make sure we can access parameters of the object



19
20
21
# File 'lib/vinyldns/api.rb', line 19

def signer
  @signer
end

Class Method Details

.make_request(signed_object, uri, body = '') ⇒ Object

Required arguments:

  • a signed object. ex: Vinyldns::API.new(‘get/POST/dElETe’)

  • a uri path. ex: ‘zones/92cc1c82-e2fc-424b-a178-f24b18e3b67a’ – This will pull ingest.yourdomain.net’s zone



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vinyldns/api.rb', line 46

def self.make_request(signed_object, uri, body = '')
  signed_headers = signed_object.signer.sign_request(
        http_method: signed_object.method,
        url: uri == '/' ? "#{signed_object.api_url}#{uri}" : "#{signed_object.api_url}/#{uri}",
        headers: { 'content-type' => signed_object.content_type },
        body: body == '' ? body : body.to_json
  )
  url = URI(signed_object.api_url)
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE # SSL not signed? Don't care!
  request = Net::HTTP::Post.new(uri == '/' ? uri : "/#{uri}") if signed_object.method == 'POST'
  request = Net::HTTP::Put.new(uri == '/' ? uri : "/#{uri}") if signed_object.method == 'PUT'
  request = Net::HTTP::Get.new(uri == '/' ? uri : "/#{uri}") if signed_object.method == 'GET'
  signed_headers.headers.each { |k, v| request[k] = v }
  request['content-type'] = signed_object.content_type
  request.body = body == '' ? body : body.to_json
  response = https.request(request)
  case response
  when Net::HTTPSuccess
    JSON.parse(response.body)
  else
    return response
    #"HTTP Error: #{response.code} #{response.message} : #{response.body}"
  end
end