Sha256: 8981cfe972acc81ef9c34334957d01639847f5b824c2021cb4f89e0470c4bf4e

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

require "redd/oauth2_access"

module Redd
  module Client
    class OAuth2
      module Authorization
        def auth_url(scope = ["identity"], duration = "temporary", state = "x")
          path = "https://ssl.reddit.com/api/v1/authorize"
          query = {
            client_id: @client_id,
            redirect_uri: @redirect_uri,
            response_type: "code",
            state: state,
            scope: scope.join(","),
            duration: duration
          }
          string_query = query.map { |key, value| "#{key}=#{value}" }.join("&")

          "#{path}?#{string_query}"
        end

        def request_access_token(code, set_access = true)
          response = auth_connection.post "/api/v1/access_token",
            grant_type: "authorization_code", code: code,
            redirect_uri: @redirect_uri

          access = Redd::OAuth2Access.new(response.body)
          @access = access if set_access
          access
        end

        def refresh_access_token(access = nil, set_access = true)
          refresh_token = extract_attribute(access, :refresh_token)
          response = auth_connection.post "/api/v1/access_token",
            grant_type: "refresh_token", refresh_token: refresh_token

          case access
          when Redd::OAuth2Access
            access.refresh(response.body)
          when ::String
            new_access = Redd::OAuth2Access.new(response.body)
            @access = new_access if set_access
            new_access
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redd-0.4.3 lib/redd/client/oauth2/authorization.rb
redd-0.4.2 lib/redd/client/oauth2/authorization.rb
redd-0.4.1 lib/redd/client/oauth2/authorization.rb
redd-0.4.0 lib/redd/client/oauth2/authorization.rb