Class: SocialAvatarProxy::Avatar
- Inherits:
-
Object
- Object
- SocialAvatarProxy::Avatar
- Defined in:
- lib/social_avatar_proxy/avatar.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
Instance Method Summary collapse
-
#body ⇒ Object
downloads the avatar from the remote server.
-
#content_type ⇒ Object
returns the content type of the file.
-
#exist? ⇒ Boolean
(also: #exists?)
returns whether the avatar returns a successful response.
-
#file ⇒ Object
return a temporary file object.
-
#initialize(identifier) ⇒ Avatar
constructor
A new instance of Avatar.
-
#last_modified ⇒ Object
returns the last modified timestamp.
-
#remote_url ⇒ Object
in the base Avatar class, we’ll use the identifier as the URL.
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
#identifier ⇒ Object (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
#body ⇒ Object
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_type ⇒ Object
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
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 |
#file ⇒ Object
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_modified ⇒ Object
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_url ⇒ Object
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 |