<commit_before>require 'oauth'
require 'uri'

module Flickr
  class Login
    DEFAULTS = {
      site: "http://www.flickr.com/services",
      return_to: "/"
    }

    def initialize(api_key, shared_secret, options = {})
      @api_key, @shared_secret = api_key, shared_secret
      @options = DEFAULTS.merge(options)
    end

    def call(env)
      request = Request.new(env)

      unless request[:oauth_verifier]
        redirect_to_flickr(request)
      else
        handle_flickr_authorization(request)
      end
    end

    def login_handler(options = {})
      @options.update(options)
      self
    end

    module Helpers
      def flickr_user
        session[:flickr_user]
      end

      def flickr_access_token
        session[:flickr_access_token]
      end

      def flickr_clear
        [:flickr_user, :flickr_access_token].each do |key|
          session.delete(key)
        end
      end
    end

    class Request < ::Rack::Request
      def url_for(path)
        url = scheme + '://' + host
        if (scheme == 'https' and port != 443) or (scheme == 'http' and port != 80)
          url << ":#{port}"
        end
        url << path
      end
    end

    private

    def redirect_to_flickr(request)
      callback_url = URI.parse(request.url).tap { |url| url.query = nil }
      request_token = consumer.get_request_token(oauth_callback: callback_url.to_s)
      request.session[:flickr_request_token] = [request_token.token, request_token.secret]
      authorize_params = request[:perms] ? {perms: request[:perms]} : {}
      redirect request_token.authorize_url(authorize_params)
    end

    def handle_flickr_authorization(request)
      request_token = renew_request_token(request)
      access_token = request_token.get_access_token(oauth_verifier: request[:oauth_verifier])
      request.session[:flickr_access_token] = [access_token.token, access_token.secret]
      request.session[:flickr_user] = access_token.params
      redirect request.url_for(@options[:return_to])
    end

    def renew_request_token(request)
      rtoken, rsecret = request.session.delete(:flickr_request_token)
      OAuth::RequestToken.new(consumer, rtoken, rsecret)
    end

    def consumer
      @consumer ||= OAuth::Consumer.new @api_key, @shared_secret, site: @options[:site]
    end

    def redirect(url)
      ['302', {'Location' => url, 'Content-type' => 'text/plain'}, []]
    end
  end
end
<commit_msg>Make it work with flickr-objects<commit_after>require 'oauth'
require 'uri'

class Flickr
  class Login
    DEFAULTS = {
      site: "http://www.flickr.com/services",
      return_to: "/"
    }

    def initialize(api_key, shared_secret, options = {})
      @api_key, @shared_secret = api_key, shared_secret
      @options = DEFAULTS.merge(options)
    end

    def call(env)
      request = Request.new(env)

      unless request[:oauth_verifier]
        redirect_to_flickr(request)
      else
        handle_flickr_authorization(request)
      end
    end

    def login_handler(options = {})
      @options.update(options)
      self
    end

    module Helpers
      def flickr_user
        session[:flickr_user]
      end

      def flickr_access_token
        session[:flickr_access_token]
      end

      def flickr_clear
        [:flickr_user, :flickr_access_token].each do |key|
          session.delete(key)
        end
      end
    end

    class Request < ::Rack::Request
      def url_for(path)
        url = scheme + '://' + host
        if (scheme == 'https' and port != 443) or (scheme == 'http' and port != 80)
          url << ":#{port}"
        end
        url << path
      end
    end

    private

    def redirect_to_flickr(request)
      callback_url = URI.parse(request.url).tap { |url| url.query = nil }
      request_token = consumer.get_request_token(oauth_callback: callback_url.to_s)
      request.session[:flickr_request_token] = [request_token.token, request_token.secret]
      authorize_params = request[:perms] ? {perms: request[:perms]} : {}
      redirect request_token.authorize_url(authorize_params)
    end

    def handle_flickr_authorization(request)
      request_token = renew_request_token(request)
      access_token = request_token.get_access_token(oauth_verifier: request[:oauth_verifier])
      request.session[:flickr_access_token] = [access_token.token, access_token.secret]
      request.session[:flickr_user] = access_token.params
      redirect request.url_for(@options[:return_to])
    end

    def renew_request_token(request)
      rtoken, rsecret = request.session.delete(:flickr_request_token)
      OAuth::RequestToken.new(consumer, rtoken, rsecret)
    end

    def consumer
      @consumer ||= OAuth::Consumer.new @api_key, @shared_secret, site: @options[:site]
    end

    def redirect(url)
      ['302', {'Location' => url, 'Content-type' => 'text/plain'}, []]
    end
  end
end

{'fraction_non_alphanumeric': 0.10730406237176857, 'fraction_numerical': 0.003282724661469019, 'mean_word_length': 2.8507109004739335, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6231375', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1519, 'n_words': 304}