Class: MosEisley::HTTPClient
- Inherits:
-
Object
- Object
- MosEisley::HTTPClient
- Defined in:
- lib/http-client/client.rb
Class Method Summary collapse
- .post_form(url:, params:, &block) ⇒ Object
- .post_json(url:, params: nil, body: nil, &block) ⇒ Object
- .request(url:, head: nil, body:, stop: false, &block) ⇒ Object
Class Method Details
.post_form(url:, params:, &block) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/http-client/client.rb', line 9 def self.post_form(url:, params:, &block) if EM.reactor_running? MosEisley.logger.debug('POSTing form') MosEisley::HTTPClient.request(url: url, body: params, &block) else MosEisley.logger.debug('Starting reactor...') EM.run { MosEisley.logger.debug('POSTing form') MosEisley::HTTPClient.request(url: url, body: params, stop: true, &block) } end end |
.post_json(url:, params: nil, body: nil, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/http-client/client.rb', line 22 def self.post_json(url:, params: nil, body: nil, &block) head = {'Content-Type' => 'application/json'} body = S3PO.json_with_object(params) if params if EM.reactor_running? MosEisley.logger.debug('POSTing JSON') MosEisley::HTTPClient.request(url: url, body: body, head: head, &block) else MosEisley.logger.debug('Starting reactor...') EM.run { MosEisley.logger.debug('POSTing JSON') MosEisley::HTTPClient.request(url: url, body: body, head: head, stop: true, &block) } end end |
.request(url:, head: nil, body:, stop: false, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/http-client/client.rb', line 37 def self.request(url:, head: nil, body:, stop: false, &block) http = EM::HttpRequest.new(url).post(body: body) http.errback { MosEisley.logger.error('HTTP error') if stop EM.stop MosEisley.logger.debug('Stopped reactor.') end } http.callback { block.call(http) if block_given? if stop EM.stop MosEisley.logger.debug('Stopped reactor.') end } end |