Class: DynectRest::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/dynect_rest/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dynect, record_type, zone, fqdn = nil, record_id = nil, ttl = nil, rdata = {}) ⇒ Resource

Returns a new instance of Resource.



24
25
26
27
28
29
30
31
32
# File 'lib/dynect_rest/resource.rb', line 24

def initialize(dynect, record_type, zone, fqdn=nil, record_id=nil, ttl=nil, rdata={})
  @dynect = dynect
  @record_type = record_type
  @fqdn = fqdn
  @record_id = record_id
  @ttl = ttl
  @zone = zone
  @rdata = rdata
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dynect_rest/resource.rb', line 134

def method_missing(method_symbol, *args, &block)
  method_string = method_symbol.to_s
  if (args.length > 0 && method_string !~ /=$/)
    @rdata[method_string] = args.length == 1 ? args[0] : args
    self
  elsif @rdata.has_key?(method_string)
    @rdata[method_string]
  else
    raise NoMethodError, "undefined method `#{method_symbol.to_s}' for #{self.class.to_s}"
  end
end

Instance Attribute Details

#dynectObject

Returns the value of attribute dynect.



22
23
24
# File 'lib/dynect_rest/resource.rb', line 22

def dynect
  @dynect
end

#fqdn(value = nil) ⇒ Object

Returns the value of attribute fqdn.



22
23
24
# File 'lib/dynect_rest/resource.rb', line 22

def fqdn
  @fqdn
end

#rdataObject

Returns the value of attribute rdata.



22
23
24
# File 'lib/dynect_rest/resource.rb', line 22

def rdata
  @rdata
end

#record_id(value = nil) ⇒ Object

Returns the value of attribute record_id.



22
23
24
# File 'lib/dynect_rest/resource.rb', line 22

def record_id
  @record_id
end

#record_typeObject

Returns the value of attribute record_type.



22
23
24
# File 'lib/dynect_rest/resource.rb', line 22

def record_type
  @record_type
end

#ttl(value = nil) ⇒ Object

Returns the value of attribute ttl.



22
23
24
# File 'lib/dynect_rest/resource.rb', line 22

def ttl
  @ttl
end

#zoneObject

Returns the value of attribute zone.



22
23
24
# File 'lib/dynect_rest/resource.rb', line 22

def zone
  @zone
end

Instance Method Details

#[](rdata_key) ⇒ Object



34
35
36
# File 'lib/dynect_rest/resource.rb', line 34

def [](rdata_key)
  @rdata[rdata_key]
end

#[]=(rdata_key, rdata_value) ⇒ Object



38
39
40
# File 'lib/dynect_rest/resource.rb', line 38

def []=(rdata_key, rdata_value)
  @rdata[rdata_key] = rdata_value
end

#deleteObject



118
119
120
121
122
123
124
125
# File 'lib/dynect_rest/resource.rb', line 118

def delete
  url = if record_id
          "#{resource_path}/#{fqdn}/#{record_id}"
        else
          "#{resource_path}/#{fqdn}"
        end
  @dynect.delete(url)
end

#find(fqdn, query_hash) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/dynect_rest/resource.rb', line 95

def find(fqdn, query_hash)
  results = []
  get(fqdn).each do |rr|
    query_hash.each do |key, value|
      results << rr if rr[key.to_s] == value
    end
  end
  results
end

#get(fqdn = nil, record_id = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dynect_rest/resource.rb', line 63

def get(fqdn = nil, record_id=nil)
  if record_id && fqdn
    raw_rr = @dynect.get("#{resource_path}/#{fqdn}/#{record_id}")
    DynectRest::Resource.new(dynect,
                             raw_rr["record_type"],
                             raw_rr["zone"],
                             raw_rr["fqdn"],
                             raw_rr["record_id"],
                             raw_rr["ttl"],
                             raw_rr["rdata"])
  elsif fqdn
    results = @dynect.get("#{resource_path}/#{fqdn}")
    raw_rr_list = results.map do |record|
      if (record =~ /^#{resource_path(:full)}\/#{Regexp.escape(fqdn)}\/(\d+)$/)
        self.get(fqdn, $1)
      else
        record
      end
    end
    case raw_rr_list.length
    when 0
      raise DynectRest::Exceptions::RequestFailed, "Cannot find #{record_type} record for #{fqdn}"
    when 1
      raw_rr_list[0]
    else
      raw_rr_list
    end
  else
    @dynect.get(resource_path)
  end
end

#resource_path(full = false) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/dynect_rest/resource.rb', line 54

def resource_path(full=false)
  @record_type << "Record" unless @record_type[-6,6] == "Record"
  if (full == true || full == :full)
    "/REST/#{@record_type}/#{@zone}"
  else
    "#{@record_type}/#{@zone}"
  end
end

#save(replace = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dynect_rest/resource.rb', line 105

def save(replace=false)
  if record_id
    @dynect.put("#{resource_path}/#{@fqdn}/#{record_id}", self)
  else
    if replace == true || replace == :replace
      @dynect.put("#{resource_path}/#{@fqdn}", self)
    else
      @dynect.post("#{resource_path}/#{@fqdn}", self)
    end
  end
  self
end

#to_jsonObject



127
128
129
130
131
132
# File 'lib/dynect_rest/resource.rb', line 127

def to_json
  {
    "rdata" => @rdata,
    "ttl"   => @ttl
  }.to_json
end