class MShard::MShard

Public Class Methods

redis_cache(uri) click to toggle source
# File lib/mshard/mshard.rb, line 60
def self.redis_cache(uri)
  @cache ||= Hash.new do |hash, uri_|
    hash[uri_] = Redis::Namespace.new(:shard, redis: Redis.new(url: uri_))
  end
  @cache[uri]
end

Public Instance Methods

detour?() click to toggle source
# File lib/mshard/mshard.rb, line 44
def detour?
  ENV['http_proxy']
end
get(id) click to toggle source
# File lib/mshard/mshard.rb, line 15
def get(id)
  self.class.get("/v2/shards/#{id}").body
end
get_safe(*args) click to toggle source
# File lib/mshard/mshard.rb, line 36
def get_safe(*args)
  try { get(*args) }
end
load(id, redis_uri: ENV['MSHARD_REDIS_URI']) click to toggle source
# File lib/mshard/mshard.rb, line 67
def load(id, redis_uri: ENV['MSHARD_REDIS_URI'])
  return get(id) if detour? || redis_uri.nil?
  self.class.redis_cache(redis_uri).get(id)
end
load_safe(*args) click to toggle source
# File lib/mshard/mshard.rb, line 72
def load_safe(*args)
  try { load(*args) }
end
notify(**args) click to toggle source
# File lib/mshard/mshard.rb, line 48
def notify(**args)
  args[:slack] ||= true
  args[:webhook_url] ||= ENV['SLACK_WEBHOOK_URI']
  args[:channel] ||= ENV['SLACK_CHANNEL']
  return set(args) if detour?
  Slack::Notifier.new(args[:webhook_url], **args).ping(args[:text])
end
notify_safe(**args) click to toggle source
# File lib/mshard/mshard.rb, line 56
def notify_safe(**args)
  try { notify(**args) }
end
save(redis_uri: ENV['MSHARD_REDIS_URI'], **args) click to toggle source
# File lib/mshard/mshard.rb, line 76
def save(redis_uri: ENV['MSHARD_REDIS_URI'], **args)
  return set(**args) if detour? || redis_uri.nil?
  response =
    self.class.redis_cache(redis_uri).set(args[:id], args[:contents])
  raise response unless response == 'OK'
  args[:id]
end
save_safe(*args) click to toggle source
# File lib/mshard/mshard.rb, line 84
def save_safe(*args)
  try { save(*args) }
end
set(params) click to toggle source
# File lib/mshard/mshard.rb, line 19
def set(params)
  self.class.post('/v2/shards', body: params)['id']
end
set_safe(*args) click to toggle source
# File lib/mshard/mshard.rb, line 40
def set_safe(*args) # rubocop:disable Naming/AccessorMethodName
  try { set(*args) }
end
try(times: 5, delay: 2) { || ... } click to toggle source
# File lib/mshard/mshard.rb, line 23
def try(times: 5, delay: 2)
  error = nil
  times.times do
    begin
      return yield
    rescue StandardError => e
      error = e
      sleep delay
    end
  end
  raise error
end