Class: NLHue::RequestQueue
- Inherits:
-
Object
- Object
- NLHue::RequestQueue
- Defined in:
- lib/nlhue/request_queue.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
Instance Method Summary collapse
-
#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.
-
#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.
-
#initialize(host, timeout = 5, content_type = 'application/json;charset=utf-8') ⇒ RequestQueue
constructor
Initializes a request queue with the given host, default request timeout, and default POST/PUT content type.
-
#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.
-
#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.
-
#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.
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.
15 16 17 18 19 20 |
# File 'lib/nlhue/request_queue.rb', line 15 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
#host ⇒ Object
Returns the value of attribute host.
11 12 13 |
# File 'lib/nlhue/request_queue.rb', line 11 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.
60 61 62 |
# File 'lib/nlhue/request_queue.rb', line 60 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.
32 33 34 35 36 |
# File 'lib/nlhue/request_queue.rb', line 32 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.
43 44 45 |
# File 'lib/nlhue/request_queue.rb', line 43 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.
52 53 54 |
# File 'lib/nlhue/request_queue.rb', line 52 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.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/nlhue/request_queue.rb', line 70 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 |