class Rack::Identicon::Response

Constants

DEFAULT_BG
DEFAULT_SIZE
ONE_YEAR

Public Class Methods

new(env) click to toggle source
# File lib/rack/identicon/response.rb, line 11
def initialize env
  @request = Rack::Request.new env
  @size = @request.params["s"] || DEFAULT_SIZE
  @bg = parse_color @request.params.fetch "b", DEFAULT_BG
end

Public Instance Methods

body() click to toggle source
# File lib/rack/identicon/response.rb, line 17
def body
  cache { ::Identicon.blob_for hashed_payload, @size.to_i, @bg }
end
headers() click to toggle source
# File lib/rack/identicon/response.rb, line 21
def headers
  {
    "Content-Type" => valid_request? ? "image/png" : "text/plain",
    "Cache-Control" => "public, max-age=#{ONE_YEAR}"
  }
end
triplet() click to toggle source
# File lib/rack/identicon/response.rb, line 28
def triplet
  if valid_request?
    [ 200, headers, [ body ] ]
  else
    [ 404, headers, [ "Not Found" ] ]
  end
end

Protected Instance Methods

cache(&blk) click to toggle source
# File lib/rack/identicon/response.rb, line 54
def cache &blk
  Rack::Identicon.cache cache_key, &blk
end
cache_key() click to toggle source
# File lib/rack/identicon/response.rb, line 58
def cache_key
  hashed_payload + "?s=#{@size}&b=#{@bg}"
end
hashed_payload() click to toggle source
# File lib/rack/identicon/response.rb, line 42
def hashed_payload
  @hashed_payload ||= Digest::SHA2.hexdigest(payload)
end
parse_color(hex_str) click to toggle source
# File lib/rack/identicon/response.rb, line 50
def parse_color hex_str
  hex_str.scan(/.{2}/).map { |c| c.to_i 16 }
end
payload() click to toggle source
# File lib/rack/identicon/response.rb, line 46
def payload
  @payload ||= @request.path_info[1..-1].to_s.strip
end
valid_request?() click to toggle source
# File lib/rack/identicon/response.rb, line 38
def valid_request?
  payload.length > 0
end