Class: NLHue::RequestQueue

Inherits:
Object
  • Object
show all
Extended by:
Log
Includes:
Log
Defined in:
lib/nlhue/request_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

bench, log, log_e, on_bench, on_log, on_log_e

Constructor Details

#initialize(host, timeout = 5, content_type = 'application/json;charset=utf-8') ⇒ RequestQueue

Initializes a request queue with the given host, default request timeout, and default POST/PUT content type.



18
19
20
21
22
23
# File 'lib/nlhue/request_queue.rb', line 18

def initialize host, timeout=5, content_type='application/json;charset=utf-8'
  @host = host
  @default_type = content_type
  @default_timeout = timeout
  @request_queue = {}
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



14
15
16
# File 'lib/nlhue/request_queue.rb', line 14

def host
  @host
end

Instance Method Details

#delete(path, category = nil, timeout = nil, &block) ⇒ Object

Makes a DELETE request to the given path, timing out after the given number of seconds, and calling the given block with a hash containing :content, :headers, and :status, or just false if there was an error.



63
64
65
# File 'lib/nlhue/request_queue.rb', line 63

def delete path, category=nil, timeout=nil, &block
  request 'DELETE', path, category, nil, nil, timeout, &block
end

#get(path, category = nil, timeout = nil, &block) ⇒ Object

Makes a GET request to the given path, timing out after the given number of seconds, and calling the given block with a hash containing :content, :headers, and :status, or just false if there was an error. The default category is the path.



35
36
37
38
39
# File 'lib/nlhue/request_queue.rb', line 35

def get path, category=nil, timeout=nil, &block
  # FIXME: Use em-http-request instead of HttpClient,
  # which returns an empty :content field for /description.xml
  request 'GET', path, category, nil, nil, timeout, &block
end

#post(path, data, category = nil, content_type = nil, timeout = nil, &block) ⇒ Object

Makes a POST request to the given path, with the given data and content type, timing out after the given number of seconds, and calling the given block with a hash containing :content, :headers, and :status, or just false if there was an error. The default category is the path.



46
47
48
# File 'lib/nlhue/request_queue.rb', line 46

def post path, data, category=nil, content_type=nil, timeout=nil, &block
  request 'POST', path, category, data, content_type, timeout, &block
end

#put(path, data, category = nil, content_type = nil, timeout = nil, &block) ⇒ Object

Makes a PUT request to the given path, with the given data and content type, timing out after the given number of seconds, and calling the given block with a hash containing :content, :headers, and :status, or just false if there was an error.



55
56
57
# File 'lib/nlhue/request_queue.rb', line 55

def put path, data, category=nil, content_type=nil, timeout=nil, &block
  request 'PUT', path, category, data, content_type, timeout, &block
end

#request(verb, path, category = nil, data = nil, content_type = nil, timeout = nil, &block) ⇒ Object

Queues a request of the given type to the given path, using the given data and content type for e.g. PUT and POST. The request will time out after timeout seconds. The given block will be called with a hash containing :content, :headers, and :status if a response was received, or just false on error. This should be called from the EventMachine reactor thread.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nlhue/request_queue.rb', line 73

def request verb, path, category=nil, data=nil, content_type=nil, timeout=nil, &block
  raise 'A block must be given.' unless block_given?
  raise 'Call from the EventMachine reactor thread.' unless EM.reactor_thread?

  category ||= path
  content_type ||= @default_type
  timeout ||= @default_timeout

  @request_queue[category] ||= []

  req = [verb, path, category, data, content_type, timeout, block]
  @request_queue[category] << req
  do_next_request category if @request_queue[category].size == 1
end