Class: Hooker::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hooker/client.rb,
lib/hooker/client/version.rb

Constant Summary collapse

NEW_JOBS_LIST =
"hooker:jobs"
FAILURES_LIST =
"hooker:failures"
VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Client

Returns a new instance of Client.



13
14
15
# File 'lib/hooker/client.rb', line 13

def initialize(redis)
  @redis = redis
end

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



12
13
14
# File 'lib/hooker/client.rb', line 12

def redis
  @redis
end

Instance Method Details

#enqueue(url, headers, subscription, payload) ⇒ Object

TODO: Use shared secret to calculate HMAC and add to headers hash



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hooker/client.rb', line 18

def enqueue(url, headers, subscription, payload)
  stringy_headers = {}.tap do |h|
    headers.each { |k, v| h[k.to_s] = v.to_s }
  end

  uuid = SecureRandom.uuid
  _, queue_length = redis.pipelined do
    redis.hmset(job_key(uuid), "url", url, "headers", stringy_headers.to_json, "subscription", subscription, "payload", payload)
    redis.lpush(NEW_JOBS_LIST, uuid)
  end
  queue_length
end

#get_next_failureObject



31
32
33
34
# File 'lib/hooker/client.rb', line 31

def get_next_failure
  # block indefinitely waiting for the next failure.
  redis.blpop(FAILURES_LIST, 0)
end