Class: SocialAvatarProxy::Avatar

Inherits:
Object
  • Object
show all
Defined in:
lib/social_avatar_proxy/avatar.rb

Direct Known Subclasses

FacebookAvatar, TwitterAvatar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ Avatar

Returns a new instance of Avatar.



8
9
10
# File 'lib/social_avatar_proxy/avatar.rb', line 8

def initialize(identifier)
  @identifier = identifier
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



6
7
8
# File 'lib/social_avatar_proxy/avatar.rb', line 6

def identifier
  @identifier
end

Instance Method Details

#bodyObject

downloads the avatar from the remote server



13
14
15
# File 'lib/social_avatar_proxy/avatar.rb', line 13

def body
  response.body
end

#content_typeObject

returns the content type of the file



36
37
38
39
40
41
# File 'lib/social_avatar_proxy/avatar.rb', line 36

def content_type
  @content_type ||= begin
    response["content-type"] ||
    "image/jpeg"
  end
end

#exist?Boolean Also known as: exists?

returns whether the avatar returns a successful response

Returns:

  • (Boolean)


18
19
20
21
22
23
# 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

#fileObject

return a temporary file object



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_modifiedObject

returns the last modified timestamp



27
28
29
30
31
32
33
# 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_urlObject

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



44
45
46
# File 'lib/social_avatar_proxy/avatar.rb', line 44

def remote_url
  identifier
end