Class: HyperMQ::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hypermq/client.rb

Instance Method Summary collapse

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, message_id)
  url = [domain, 'ack', queue, name].join('/')
  status = Faraday.post(url) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = %-{"id":"#{message_id}"}-
  end.env[:status]
  status.to_i == 201
end

#domainObject



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, message_id=nil)
  url = [domain, 'q', queue, message_id].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, message)
  url = [domain, 'q', queue].join('/')
  Faraday.post(url) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = Yajl::Encoder.encode(producer: name, body: message)
  end.env[:status] == 201
end