Class: SocialAvatarProxy::ResponseBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @service = options.fetch(:service)
  @identifier = options.fetch(:identifier)
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



15
16
17
# File 'lib/social_avatar_proxy/response.rb', line 15

def identifier
  @identifier
end

#serviceObject (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

#fileObject



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

#responseObject



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