Class: SocialAvatarProxy::ResponseBuilder
- Inherits:
-
Object
- Object
- SocialAvatarProxy::ResponseBuilder
- Defined in:
- lib/social_avatar_proxy/response.rb
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #file ⇒ Object
-
#initialize(options) ⇒ ResponseBuilder
constructor
A new instance of ResponseBuilder.
- #response ⇒ Object
- #set_file_headers(response) ⇒ Object
- #write_file(response) ⇒ Object
Constructor Details
#initialize(options) ⇒ ResponseBuilder
Returns a new instance of ResponseBuilder.
17 18 19 20 |
# File 'lib/social_avatar_proxy/response.rb', line 17 def initialize() @service = .fetch(:service) @identifier = .fetch(:identifier) end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
15 16 17 |
# File 'lib/social_avatar_proxy/response.rb', line 15 def identifier @identifier end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
15 16 17 |
# File 'lib/social_avatar_proxy/response.rb', line 15 def service @service end |
Instance Method Details
#file ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/social_avatar_proxy/response.rb', line 67 def file @file ||= SocialAvatarProxy::Config.caches.fetch({ service: service, identifier: identifier }) do remote_avatar_file || default_image end end |
#response ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/social_avatar_proxy/response.rb', line 22 def response return @response if defined?(@response) if file @response = Response.new do |response| write_file(response) set_file_headers(response) end else @response = Rack::Response.new("Not Found", 404) end end |
#set_file_headers(response) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/social_avatar_proxy/response.rb', line 34 def set_file_headers(response) # set the modified time if file.respond_to?(:mtime) && file.mtime response["Last-Modified"] = file.mtime.httpdate end # set the content type if file.respond_to?(:content_type) && file.content_type response["Content-Type"] = file.content_type end end |
#write_file(response) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/social_avatar_proxy/response.rb', line 45 def write_file(response) # configure the file delivery case Config.delivery_method when :x_accel_redirect # set the NGINX header response["X-Accel-Redirect"] = file.path when :x_send_file # set the Apache header response["X-Sendfile"] = file.path else begin # read the file contents data = file.read # write it to the response response.write(data) ensure # close the file file.close end end end |