Class: DynDynDong::Generic

Inherits:
Service
  • Object
show all
Defined in:
lib/dyndyndong/services/generic.rb

Direct Known Subclasses

DynDNS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

each, #fetch, inherited, instances, #method_missing, services

Constructor Details

#initialize(*args) ⇒ Generic

Returns a new instance of Generic.



26
27
28
29
30
31
# File 'lib/dyndyndong/services/generic.rb', line 26

def initialize(*args)
  @user = nil
  @pass = nil
  @ip = getip
  super(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DynDynDong::Service

Class Method Details

.set(*args) ⇒ Object



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
94
95
96
97
98
# File 'lib/dyndyndong/services/generic.rb', line 67

def self.set(*args)
  sym = args.shift.to_s.downcase
  case sym
  when 'msgtable'
    raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" if args.size != 1
    raise ArgumentError, "wrong type" if !args.first.is_a?(Hash)
    self.class_eval %Q{
      def MSGTABLE(x)
        #{args.first.inspect}[x]
      end
    }
  when 'update_host'
    raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" if args.size != 1
    raise ArgumentError, "wrong type" if !args.first.is_a?(String)
    self.class_eval %Q{
      def update_host
        #{args.first.inspect}
      end
    }
  when 'offline'
    raise ArgumentError, "wrong number of arguments (#{args.size} for 2)" if args.size != 2
    raise ArgumentError, "wrong type" if !args[0].is_a?(String) or !args[1].is_a?(String)
    self.class_eval %Q{
      def offlines(what)
        {
          'YES' => #{args[0].inspect},
          'NO'  => #{args[1].inspect}
        }[what]
      end
    }
  end
end

Instance Method Details

#alias_host(h, offline = false) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/dyndyndong/services/generic.rb', line 57

def alias_host(h, offline = false)
  Net::HTTP.start(update_host) {|http|
    req = Net::HTTP::Get.new('/nic/update?hostname=%s&myip=%s&offline=%s' % [
        URI.escape(h), getip(h), offline?(offline)])
    req.basic_auth @user, @pass
    x = http.request(req).body.gsub(/#{Regexp.escape(@ip)}/, '').strip
    MSGTABLE(x)
  }
end

#host(h) ⇒ Object



44
45
46
# File 'lib/dyndyndong/services/generic.rb', line 44

def host(h)
  @hosts << [h.to_s]
end

#offline(h) ⇒ Object



48
49
50
# File 'lib/dyndyndong/services/generic.rb', line 48

def offline(h)
  @hosts << [h.to_s, true]
end

#password(p) ⇒ Object Also known as: password=



37
38
39
# File 'lib/dyndyndong/services/generic.rb', line 37

def password(p)
  @pass = p.to_s
end

#prefetchObject



52
53
54
55
# File 'lib/dyndyndong/services/generic.rb', line 52

def prefetch
  raise "username or password ungiven" if !(@user and @pass)
  @ip = getip
end

#username(u) ⇒ Object Also known as: username=



33
34
35
# File 'lib/dyndyndong/services/generic.rb', line 33

def username(u)
  @user = u.to_s
end