Class: HyperMQ::Client
- Inherits:
-
Object
- Object
- HyperMQ::Client
- Defined in:
- lib/hypermq/client.rb
Instance Method Summary collapse
- #acknowledge(queue, message_id) ⇒ Object
- #domain ⇒ Object
- #fetch(queue, message_id = nil) ⇒ Object
-
#initialize(name, host, port = 80) ⇒ Client
constructor
A new instance of Client.
- #last_seen(queue) ⇒ Object
- #push(queue, message) ⇒ Object
Constructor Details
#initialize(name, host, port = 80) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 |
# File 'lib/hypermq/client.rb', line 8 def initialize(name, host, port=80) @name = name @host = host @port = port end |
Instance Method Details
#acknowledge(queue, message_id) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/hypermq/client.rb', line 32 def acknowledge(queue, ) url = [domain, 'ack', queue, name].join('/') status = Faraday.post(url) do |req| req.headers['Content-Type'] = 'application/json' req.body = %-{"id":"#{}"}- end.env[:status] status.to_i == 201 end |
#domain ⇒ Object
14 15 16 |
# File 'lib/hypermq/client.rb', line 14 def domain "http://#{host}:#{port}" end |
#fetch(queue, message_id = nil) ⇒ Object
26 27 28 29 30 |
# File 'lib/hypermq/client.rb', line 26 def fetch(queue, =nil) url = [domain, 'q', queue, ].compact.join('/') response = Faraday.get(url).env[:body] Yajl::Parser.parse(response) end |
#last_seen(queue) ⇒ Object
41 42 43 44 45 |
# File 'lib/hypermq/client.rb', line 41 def last_seen(queue) url = [domain, 'ack', queue, name].join('/') response = Faraday.get(url).env[:body] Yajl::Parser.parse(response).fetch('message') end |
#push(queue, message) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/hypermq/client.rb', line 18 def push(queue, ) url = [domain, 'q', queue].join('/') Faraday.post(url) do |req| req.headers['Content-Type'] = 'application/json' req.body = Yajl::Encoder.encode(producer: name, body: ) end.env[:status] == 201 end |