Class: RequestRepeater::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/request_repeater/endpoint.rb

Constant Summary collapse

NoEndpointToCall =
Class.new(StandardError)
InvalidURL =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, sleepfor:) ⇒ Endpoint

Returns a new instance of Endpoint.



10
11
12
13
# File 'lib/request_repeater/endpoint.rb', line 10

def initialize(url:, sleepfor:)
  @sleepfor = sleepfor.to_i
  @url = url
end

Instance Attribute Details

#last_executionObject

Returns the value of attribute last_execution.



8
9
10
# File 'lib/request_repeater/endpoint.rb', line 8

def last_execution
  @last_execution
end

#sleepforObject (readonly)

Returns the value of attribute sleepfor.



6
7
8
# File 'lib/request_repeater/endpoint.rb', line 6

def sleepfor
  @sleepfor
end

#timerObject



36
37
38
# File 'lib/request_repeater/endpoint.rb', line 36

def timer
  @timer ||= Time
end

Instance Method Details

#==(other_endpoint) ⇒ Object



30
31
32
33
34
# File 'lib/request_repeater/endpoint.rb', line 30

def ==(other_endpoint)
  self.url == other_endpoint.url \
    && self.sleepfor == other_endpoint.sleepfor \
    && self.last_execution == other_endpoint.last_execution
end

#executable?Boolean

Returns:



22
23
24
# File 'lib/request_repeater/endpoint.rb', line 22

def executable?
  timer.now > last_execution + RequestRepeater.sleeptime(sleepfor)
end

#executeObject



15
16
17
18
19
20
# File 'lib/request_repeater/endpoint.rb', line 15

def execute
  if executable?
    yield
    executed
  end
end

#executedObject



26
27
28
# File 'lib/request_repeater/endpoint.rb', line 26

def executed
  @last_execution = timer.now
end

#inspectObject



53
54
55
# File 'lib/request_repeater/endpoint.rb', line 53

def inspect
  "#<#{self.class}:#{object_id.to_s(16)} url=#{url.to_s[0..16]}... sleepfor=#{sleepfor}>"
end

#uriObject



44
45
46
47
48
49
50
51
# File 'lib/request_repeater/endpoint.rb', line 44

def uri
  @uri ||= begin
    uri = URI.parse(url)
    raise InvalidURL, "#{url} is not valid url. Expecting format 'http://myapp/endpoint" unless uri.host
    uri.path = uri.path.to_s == '' ?  '/' : uri.path 
    uri
  end
end

#urlObject



40
41
42
# File 'lib/request_repeater/endpoint.rb', line 40

def url
   @url || raise(NoEndpointToCall)
end