Class: Fog::DNS::DNSimple::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/dnsimple/dns.rb,
lib/fog/dnsimple/requests/dns/get_record.rb,
lib/fog/dnsimple/requests/dns/get_domain.rb,
lib/fog/dnsimple/requests/dns/list_records.rb,
lib/fog/dnsimple/requests/dns/list_domains.rb,
lib/fog/dnsimple/requests/dns/create_domain.rb,
lib/fog/dnsimple/requests/dns/update_record.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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Mock) initialize(options = {})

Returns a new instance of Mock



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

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

Class Method Details

+ (Object) data



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

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

+ (Object) reset



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

def self.reset
  @data = nil
end

Instance Method Details

- (Object) create_domain(name)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/dnsimple/requests/dns/create_domain.rb', line 34

def create_domain(name)
  body = {
    "domain" =>  {
      "id"                 => Fog::Mock.random_numbers(1).to_i,
      "user_id"            => 1,
      "registrant_id"      => nil,
      "name"               => name,
      "unicode_name"       => name,
      "token"              => "4fIFYWYiJayvL2tkf_mkBkqC4L+4RtYqDA",
      "state"              => "registered",
      "language"           => nil,
      "lockable"           => true,
      "auto_renew"         => nil,
      "whois_protected"    => false,
      "record_count"       => 0,
      "service_count"      => 0,
      "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 = body
  response
end

- (Object) create_record(domain, name, type, content, options = {})



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/dnsimple/requests/dns/create_record.rb', line 44

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

  response = Excon::Response.new
  response.status = 201
  response.body = body
  response
end

- (Object) data



48
49
50
# File 'lib/fog/dnsimple/dns.rb', line 48

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

- (Object) delete_domain(name)



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

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

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

- (Object) delete_record(domain, record_id)



23
24
25
26
27
28
29
# File 'lib/fog/dnsimple/requests/dns/delete_record.rb', line 23

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

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

- (Object) get_domain(id)



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

def get_domain(id)
  domain = self.data[:domains].detect do |domain|
    domain["domain"]["id"] == id || domain["domain"]["name"] == id
  end

  response = Excon::Response.new
  response.status = 200
  response.body = domain
  response
end

- (Object) get_record(domain, record_id)



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

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

  if self.data[:records].has_key?(domain)
    response.status = 200
    response.body = self.data[:records][domain].detect { |record| record["record"]["id"] == record_id }

    if response.body.nil?
      response.status = 404
      response.body = {
        "error" => "Couldn't find Record with id = #{record_id}"
      }
    end
  else
    response.status = 404
    response.body = {
      "error" => "Couldn't find Domain with name = #{domain}"
    }
  end
  response
end

- (Object) list_domains



28
29
30
31
32
33
# File 'lib/fog/dnsimple/requests/dns/list_domains.rb', line 28

def list_domains
  response = Excon::Response.new
  response.status = 200
  response.body = self.data[:domains]
  response
end

- (Object) list_records(domain)



28
29
30
31
32
33
# File 'lib/fog/dnsimple/requests/dns/list_records.rb', line 28

def list_records(domain)
  response = Excon::Response.new
  response.status = 200
  response.body = self.data[:records][domain] || []
  response
end

- (Object) reset_data



52
53
54
# File 'lib/fog/dnsimple/dns.rb', line 52

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

- (Object) update_record(domain, record_id, options)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/dnsimple/requests/dns/update_record.rb', line 38

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

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

  response
end