Sha256: e2d61da17227271fb7bedcaffa6dcb1444cb11f81a08979a67d06a0ae54fa9ae

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

require "cgi"
require_relative "base"

module Redd
  module Clients
    # The client for installed apps that can't keep a secret.
    # It might even work with Rubymotion (fingers crossed).
    class Installed < Base
      # @!attribute [r] client_id
      attr_reader :client_id

      # @!attribute [r] redirect_uri
      attr_reader :redirect_uri

      # @param [Hash] options The options to create the client with.
      # @see Base#initialize
      # @see Redd.it
      def initialize(client_id, redirect_uri, **options)
        @client_id = client_id
        @redirect_uri = redirect_uri
        super(**options)
      end

      # @param [String] state A random string to double-check later.
      # @param [Array<String>] scope The scope to request access to.
      # @param [:temporary, :permanent] duration
      # @return [String] The url to redirect the user to.
      def auth_url(state, scope = ["identity"], duration = :temporary)
        query = {
          response_type: "token",
          client_id: @client_id,
          redirect_uri: @redirect_uri,
          state: state,
          scope: scope.join(","),
          duration: duration
        }

        url = URI.join(auth_endpoint, "/api/v1/authorize")
        url.query = URI.encode_www_form(query)
        url.to_s
      end

      # Authorize using the url fragment.
      # @param [String] fragment The part of the url after the "#".
      # @return [Access] The access given by reddit.
      def authorize!(fragment)
        parsed = CGI.parse(fragment)
        @access = Access.new(
          access_token: parsed[:access_token].first,
          expires_in: parsed[:expires_in].first,
          scope: parsed[:scope]
        )
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
redd-0.7.8 lib/redd/clients/installed.rb
redd-0.7.7 lib/redd/clients/installed.rb
redd-0.7.6 lib/redd/clients/installed.rb
redd-0.7.5 lib/redd/clients/installed.rb
redd-0.7.4 lib/redd/clients/installed.rb
redd-0.7.3 lib/redd/clients/installed.rb
redd-0.7.2 lib/redd/clients/installed.rb
redd-0.7.1 lib/redd/clients/installed.rb