Class: Fragmentary::RequestQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/fragmentary/request_queue.rb

Defined Under Namespace

Classes: Sender

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_type, host_root_url) ⇒ RequestQueue

Returns a new instance of RequestQueue.



27
28
29
30
31
32
33
34
35
# File 'lib/fragmentary/request_queue.rb', line 27

def initialize(user_type, host_root_url)
  @user_type = user_type
  # host_root_url represents where the queued *requests* are to be processed. For internal sessions it also represents where
  # the *queue* will be processed by delayed_job. For external requests, the queue will be processed by the host creating the
  # queue and the requests will be explicitly sent to the host_root_url.
  @host_root_url = host_root_url
  @requests = []
  self.class.all << self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs) ⇒ Object



68
69
70
# File 'lib/fragmentary/request_queue.rb', line 68

def method_missing(method, *args, **kwargs)
  sender.send(method, *args, **kwargs)
end

Instance Attribute Details

#host_root_urlObject (readonly)

Returns the value of attribute host_root_url.



25
26
27
# File 'lib/fragmentary/request_queue.rb', line 25

def host_root_url
  @host_root_url
end

#requestsObject (readonly)

Returns the value of attribute requests.



25
26
27
# File 'lib/fragmentary/request_queue.rb', line 25

def requests
  @requests
end

#user_typeObject (readonly)

Returns the value of attribute user_type.



25
26
27
# File 'lib/fragmentary/request_queue.rb', line 25

def user_type
  @user_type
end

Class Method Details

.allObject



5
6
7
# File 'lib/fragmentary/request_queue.rb', line 5

def self.all
  @@all ||= []
end

.send_all(delay: nil, between: nil, queue_suffix: '', priority: 0, user_group: 'default') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fragmentary/request_queue.rb', line 9

def self.send_all(delay: nil, between: nil, queue_suffix: '', priority: 0, user_group: 'default')
  unless delay or between
    all.each{|q| q.start(queue_suffix: queue_suffix, priority: priority, user_group: user_group)}
  else
    delay ||= 0.seconds
    between ||= 0.seconds
    unless delay.is_a? ActiveSupport::Duration
      raise TypeError, "Fragmentary::RequestQueue.send_all requires the keyword argument :delay to be of class ActiveSupport::Duration. The value provided is of class #{delay.class.name}."
    end
    unless between.is_a? ActiveSupport::Duration
      raise TypeError, "Fragmentary::RequestQueue.send_all requires the keyword argument :between to be of class ActiveSupport::Duration. The value provided is of class #{between.class.name}."
    end
    all.each{|q| q.start(delay: (delay += between), queue_suffix: queue_suffix, priority: priority, user_group: user_group)}
  end
end

Instance Method Details

#<<(request) ⇒ Object



37
38
39
40
41
42
# File 'lib/fragmentary/request_queue.rb', line 37

def <<(request)
  unless @requests.find{|r| r == request}
    @requests << request
  end
  self
end

#clearObject



52
53
54
# File 'lib/fragmentary/request_queue.rb', line 52

def clear
  @requests = []
end

#next_requestObject



48
49
50
# File 'lib/fragmentary/request_queue.rb', line 48

def next_request
  @requests.shift
end

#remove_path(path) ⇒ Object



56
57
58
# File 'lib/fragmentary/request_queue.rb', line 56

def remove_path(path)
  requests.delete_if{|r| r.path == path}
end

#send(**kwargs) ⇒ Object



64
65
66
# File 'lib/fragmentary/request_queue.rb', line 64

def send(**kwargs)
  sender.start(**kwargs)
end

#senderObject



60
61
62
# File 'lib/fragmentary/request_queue.rb', line 60

def sender
  @sender ||= Sender.new(self)
end

#sizeObject



44
45
46
# File 'lib/fragmentary/request_queue.rb', line 44

def size
  @requests.size
end