class SocialAvatarProxy::Avatar

Attributes

identifier[R]

Public Class Methods

new(identifier) click to toggle source
# File lib/social_avatar_proxy/avatar.rb, line 8
def initialize(identifier)
  @identifier = identifier
end

Public Instance Methods

body() click to toggle source

downloads the avatar from the remote server

# File lib/social_avatar_proxy/avatar.rb, line 13
def body
  response.body
end
content_type() click to toggle source

returns the content type of the file

# File lib/social_avatar_proxy/avatar.rb, line 36
def content_type
  @content_type ||= begin
    response["content-type"] ||
    "image/jpeg"
  end
end
exist?() click to toggle source

returns whether the avatar returns a successful response

# File lib/social_avatar_proxy/avatar.rb, line 18
def exist?
  @exists ||= begin
    # ensure the response is success/redirect
    response.code.to_i.between?(200, 399)
  end
end
Also aliased as: exists?
exists?()
Alias for: exist?
file() click to toggle source

return a temporary file object

# File lib/social_avatar_proxy/avatar.rb, line 49
def file
  AvatarFile.new(File.basename(remote_url), encoding: body.encoding).tap do |file|
    begin
      file.write(body)
      file.content_type(content_type)
      file.mtime(last_modified)
      file.rewind
    rescue
      file.close
      raise
    end
  end if exist?
end
last_modified() click to toggle source

returns the last modified timestamp

# File lib/social_avatar_proxy/avatar.rb, line 27
def last_modified
  @last_modified ||= begin
    response["last-modified"] &&
    Time.parse(response["last-modified"]) or
    Time.now
  end
end
remote_url() click to toggle source

in the base Avatar class, we'll use the identifier as the URL

# File lib/social_avatar_proxy/avatar.rb, line 44
def remote_url
  identifier
end

Private Instance Methods

response() click to toggle source

request the remote file

# File lib/social_avatar_proxy/avatar.rb, line 65
def response
  @response ||= RemoteFileResolver.new(remote_url).resolve
end