Sha256: a36e7f52a63fb751cd786a7a1242e396590e854896d19b48d300325e05307e23

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

require "social_avatar_proxy/config"
require "social_avatar_proxy/facebook_avatar"
require "social_avatar_proxy/twitter_avatar"
require "social_avatar_proxy/routes"
require "social_avatar_proxy/response"
require "social_avatar_proxy/timeout_error"
require "social_avatar_proxy/too_many_redirects_error"
require "rack"

module SocialAvatarProxy
  class App
    def self.call(env)
      new.call(env)
    end
    
    def self.routes
      new.routes
    end
    
    attr_reader :options, :request
    
    def call(env)
      @request = Rack::Request.new(env)
      begin
        response.finish
      rescue TimeoutError => e
        timeout
      rescue TooManyRedirectsError => e
        bad_gateway
      end
    end
    
    def path_prefix
      ((request && request.env["SCRIPT_NAME"]) || "").gsub(/\/$/, "")
    end
    
    def response
      # ensure this is a valid avatar request
      unless request.path =~ /^#{path_prefix}\/(facebook|twitter)\/([\w\-\.]+)(\.(jpe?g|png|gif|bmp))?$/i
        return not_found
      end
      # create our response
      SocialAvatarProxy::Response.build({
        service: $1,
        identifier: $2
      })
    end
    
    def routes
      Routes.new(self)
    end
    
    private
    def not_found
      Rack::Response.new("Not Found", 404)
    end
    
    def timeout
      Rack::Response.new("Gateway Timeout", 504)
    end
    
    def bad_gateway
      Rack::Response.new("Bad Gateway: Too many redirects", 502)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
social-avatar-proxy-2.0.1 lib/social_avatar_proxy/app.rb
social-avatar-proxy-2.0.0 lib/social_avatar_proxy/app.rb