Class: Fog::DNS::Dnsimple::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/dnsimple/dns.rb,
lib/fog/dnsimple/requests/dns/get_domain.rb,
lib/fog/dnsimple/requests/dns/get_record.rb,
lib/fog/dnsimple/requests/dns/list_domains.rb,
lib/fog/dnsimple/requests/dns/list_records.rb,
lib/fog/dnsimple/requests/dns/create_domain.rb,
lib/fog/dnsimple/requests/dns/create_record.rb,
lib/fog/dnsimple/requests/dns/delete_domain.rb,
lib/fog/dnsimple/requests/dns/delete_record.rb,
lib/fog/dnsimple/requests/dns/update_record.rb,
lib/fog/dnsimple/requests/dns/list_all_domains.rb,
lib/fog/dnsimple/requests/dns/list_all_records.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



42
43
44
# File 'lib/fog/dnsimple/dns.rb', line 42

def initialize(options = {})
  @dnsimple_token = options[:dnsimple_token]
end

Class Method Details

.dataObject



29
30
31
32
33
34
35
36
# File 'lib/fog/dnsimple/dns.rb', line 29

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
        domains: [],
        records: {}
    }
  end
end

.resetObject



38
39
40
# File 'lib/fog/dnsimple/dns.rb', line 38

def self.reset
  @data = nil
end

Instance Method Details

#create_domain(zone_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/dnsimple/requests/dns/create_domain.rb', line 29

def create_domain(zone_name)
  body = {
    "id"                 => Fog::Mock.random_numbers(1).to_i,
    "account_id"         => @dnsimple_account,
    "registrant_id"      => nil,
    "name"               => zone_name,
    "unicode_name"       => zone_name,
    "token"              => "4fIFYWYiJayvL2tkf_mkBkqC4L+4RtYqDA",
    "state"              => "registered",
    "auto_renew"         => nil,
    "private_whois"     => false,
    "expires_on"         => Date.today + 365,
    "created_at"         => Time.now.iso8601,
    "updated_at"         => Time.now.iso8601,
  }
  self.data[:domains] << body

  response = Excon::Response.new
  response.status = 201
  response.body = { "data" => body }
  response
end

#create_record(zone_name, name, type, content, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/dnsimple/requests/dns/create_record.rb', line 38

def create_record(zone_name, name, type, content, options = {})
  body = {
    "id" => Fog::Mock.random_numbers(1).to_i,
    "domain_id" => 1,
    "name" => name,
    "content" => content,
    "ttl" => 3600,
    "priority" => 0,
    "type" => type,
    "system_record" => false,
    "created_at" => Time.now.iso8601,
    "updated_at" => Time.now.iso8601,
  }.merge(options)
  self.data[:records][zone_name] ||= []
  self.data[:records][zone_name] << body

  response = Excon::Response.new
  response.status = 201
  response.body = { "data" => body }
  response
end

#dataObject



46
47
48
# File 'lib/fog/dnsimple/dns.rb', line 46

def data
  self.class.data[@dnsimple_token]
end

#delete_domain(zone_name) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/fog/dnsimple/requests/dns/delete_domain.rb', line 25

def delete_domain(zone_name)
  self.data[:records].delete(zone_name)
  self.data[:domains].reject! { |domain| domain["id"] == zone_name || domain["name"] == zone_name }

  response = Excon::Response.new
  response.status = 204
  response
end

#delete_record(zone_name, record_id) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/fog/dnsimple/requests/dns/delete_record.rb', line 20

def delete_record(zone_name, record_id)
  self.data[:records][zone_name].reject! { |record| record["id"] == record_id }

  response = Excon::Response.new
  response.status = 204
  response
end

#get_domain(zone_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/dnsimple/requests/dns/get_domain.rb', line 26

def get_domain(zone_name)
  response = Excon::Response.new

  payload = self.data[:domains].find { |domain| domain["id"] == zone_name || domain["name"] == zone_name }
  if payload
    response.status = 200
    response.body = { "data" => payload }
  else
    # response.status = 404
    # response.body = { "message" => "Domain `#{zone_name}` not found" }
    raise Excon::Errors::NotFound, "Domain `#{zone_name}` not found"
  end

  response
end

#get_record(zone_name, record_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/dnsimple/requests/dns/get_record.rb', line 25

def get_record(zone_name, record_id)
  response = Excon::Response.new

  if self.data[:records].key?(zone_name)
    payload = self.data[:records][zone_name].find { |record| record["id"] == record_id }

    if payload
      response.status = 200
      response.body = { "data" => payload }
    else
      # response.status = 404
      # response.body = { "message" => "Record `#{record_id}` not found" }
      raise Excon::Errors::NotFound, "Record `#{record_id}` not found"
    end
  else
    # response.status = 404
    # response.body = { "message" => "Domain `#{zone_name}` not found" }
    raise Excon::Errors::NotFound, "Domain `#{zone_name}` not found"
  end

  response
end

#list_all_domains(_query: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/fog/dnsimple/requests/dns/list_all_domains.rb', line 26

def list_all_domains(_query: {})
  response = Excon::Response.new
  response.status = 200
  response.body = {
      "data" => self.data[:domains],
      "pagination" => { "current_page" => nil, "per_page" => nil, "total_entries" => 60, "total_pages" => 2 }
  }
  response
end

#list_all_records(zone_name, _query: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/fog/dnsimple/requests/dns/list_all_records.rb', line 27

def list_all_records(zone_name, _query: {})
  response = Excon::Response.new
  response.status = 200
  response.body = {
      "data" => Array(self.data[:records][zone_name]),
      "pagination" => { "current_page" => nil, "per_page" => nil, "total_entries" => 60, "total_pages" => 2 }
  }
  response
end

#list_domains(query: {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/dnsimple/requests/dns/list_domains.rb', line 23

def list_domains(query: {})
  page = query[:page] || 1
  per_page = query[:per_page] || 30

  response = Excon::Response.new
  response.status = 200
  response.body = {
      "data" => self.data[:domains],
      "pagination" => { "current_page" => page, "per_page" => per_page, "total_entries" => 60, "total_pages" => 2 }
  }
  response
end

#list_records(zone_name, query: {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/dnsimple/requests/dns/list_records.rb', line 24

def list_records(zone_name, query: {})
  page = query[:page] || 1
  per_page = query[:per_page] || 30

  response = Excon::Response.new
  response.status = 200
  response.body = {
      "data" => Array(self.data[:records][zone_name]),
      "pagination" => { "current_page" => page, "per_page" => per_page, "total_entries" => 60, "total_pages" => 2 }
  }
  response
end

#reset_dataObject



50
51
52
# File 'lib/fog/dnsimple/dns.rb', line 50

def reset_data
  self.class.data.delete(@dnsimple_token)
end

#update_record(zone_name, record_id, options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/dnsimple/requests/dns/update_record.rb', line 33

def update_record(zone_name, record_id, options)
  record = self.data[:records][zone_name].find { |record| record["id"] == record_id }
  response = Excon::Response.new

  if record.nil?
    response.status = 400
  else
    response.status = 200
    record.merge!(options)
    record["updated_at"] = Time.now.iso8601
    response.body = { "data" => record }
  end

  response
end