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

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

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

- (Object) method_missing(method_symbol, *args, &block)



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

def method_missing(method_symbol, *args, &block)
  method_string = method_symbol.to_s
  if (args.length > 0 && method_string !~ /=$/)
    @rdata[method_string] = *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

- (Object) dynect

Returns the value of attribute dynect



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

def dynect
  @dynect
end

- (Object) fqdn(value = nil)

Returns the value of attribute fqdn



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

def fqdn(value=nil)
  value ? (@fqdn = value; self) : @fqdn
end

- (Object) rdata

Returns the value of attribute rdata



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

def rdata
  @rdata
end

- (Object) record_id(value = nil)

Returns the value of attribute record_id



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

def record_id(value=nil)
  value ? (@record_id = value; self) : @record_id
end

- (Object) record_type

Returns the value of attribute record_type



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

def record_type
  @record_type
end

- (Object) ttl(value = nil)

Returns the value of attribute ttl



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

def ttl(value=nil)
  value ? (@ttl = value; self) : @ttl
end

- (Object) zone

Returns the value of attribute zone



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

def zone
  @zone
end

Instance Method Details

- (Object) [](rdata_key)



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

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

- (Object) []=(rdata_key, rdata_value)



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

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

- (Object) delete



106
107
108
# File 'lib/dynect_rest/resource.rb', line 106

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

- (Object) find(fqdn, query_hash)



83
84
85
86
87
88
89
90
91
# File 'lib/dynect_rest/resource.rb', line 83

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

- (Object) get(fqdn, record_id = nil)



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dynect_rest/resource.rb', line 62

def get(fqdn, record_id=nil)
  if record_id
    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"])
  else
    raw_rr_list = []
    @dynect.get("#{resource_path}/#{fqdn}").each do |record|
      record =~ /^#{resource_path(:full)}\/#{fqdn}\/(\d+)$/
      raw_rr_list << self.get(fqdn, $1)
    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
  end
end

- (Object) resource_path(full = false)



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

def resource_path(full=false)
  if (full == true || full == :full) 
    "/REST/#{@record_type}Record/#{@zone}"
  else
    "#{@record_type}Record/#{@zone}"
  end
end

- (Object) save(replace = false)



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

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

- (Object) to_json



110
111
112
113
114
115
# File 'lib/dynect_rest/resource.rb', line 110

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