Class: Radd::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/radd/update.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Update

Returns a new instance of Update.



9
10
11
# File 'lib/radd/update.rb', line 9

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/radd/update.rb', line 3

def env
  @env
end

Class Method Details

.call(env) ⇒ Object



5
6
7
# File 'lib/radd/update.rb', line 5

def self.call(env)
  new(env).call
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/radd/update.rb', line 22

def call
  raise Forbidden unless record
  raise InvalidRequest.new('Invalid IP address') unless ip
  record.ip = ip
  record.save
  [200, {'Content-Type' => 'text/plain'}, ["OK #{ip}"]]
rescue RaddError => boom
  status = case boom
  when InvalidRequest, Sequel::ValidationFailed then 422
  when Forbidden then 403
  else
    500
  end
  respond status, "ERROR #{boom.message}"
rescue Exception => e
  respond 500, "ERROR"
end

#ipObject



17
18
19
20
# File 'lib/radd/update.rb', line 17

def ip
  addr = env['REMOTE_ADDR']
  addr && Radd.valid_ip?(addr) && addr
end

#recordObject



13
14
15
# File 'lib/radd/update.rb', line 13

def record
  @record ||= Record.where(name: name).first
end