Module: Redd

Defined in:
lib/redd.rb,
lib/redd/error.rb,
lib/redd/client.rb,
lib/redd/version.rb,
lib/redd/api_client.rb,
lib/redd/middleware.rb,
lib/redd/models/user.rb,
lib/redd/models/access.rb,
lib/redd/models/comment.rb,
lib/redd/models/listing.rb,
lib/redd/models/session.rb,
lib/redd/models/gildable.rb,
lib/redd/models/mod_mail.rb,
lib/redd/models/postable.rb,
lib/redd/models/inboxable.rb,
lib/redd/models/replyable.rb,
lib/redd/models/subreddit.rb,
lib/redd/models/wiki_page.rb,
lib/redd/utilities/stream.rb,
lib/redd/models/front_page.rb,
lib/redd/models/lazy_model.rb,
lib/redd/models/searchable.rb,
lib/redd/models/submission.rb,
lib/redd/models/basic_model.rb,
lib/redd/models/live_thread.rb,
lib/redd/models/messageable.rb,
lib/redd/models/moderatable.rb,
lib/redd/models/multireddit.rb,
lib/redd/auth_strategies/web.rb,
lib/redd/models/more_comments.rb,
lib/redd/auth_strategies/script.rb,
lib/redd/models/private_message.rb,
lib/redd/utilities/rate_limiter.rb,
lib/redd/utilities/unmarshaller.rb,
lib/redd/utilities/error_handler.rb,
lib/redd/auth_strategies/userless.rb,
lib/redd/auth_strategies/auth_strategy.rb

Overview

Redd is a simple and intuitive API wrapper.

Defined Under Namespace

Modules: AuthStrategies, Models, Utilities Classes: APIClient, APIError, AuthenticationError, BadRequest, Client, Forbidden, InsufficientScope, InvalidAccess, Middleware, NotFound, ResponseError, ServerError, TokenRetrievalError, TooManyRequests

Constant Summary collapse

VERSION =
'0.8.8'

Class Method Summary collapse

Class Method Details

.it(opts = {}) ⇒ Models::Session

Based on the arguments you provide it, it guesses the appropriate authentication strategy. You can do this manually with:

script   = Redd::AuthStrategies::Script.new(**arguments)
web      = Redd::AuthStrategies::Web.new(**arguments)
userless = Redd::AuthStrategies::Userless.new(**arguments)

It then creates an APIClient with the auth strategy provided and calls authenticate on it:

client = Redd::APIClient.new(script); client.authenticate(code)
client = Redd::APIClient.new(web); client.authenticate
client = Redd::APIClient.new(userless); client.authenticate

Finally, it creates the Redd::Models::Session model, which is essentially a starting point for the user. But you can basically create any model with the client.

session = Redd::Models::Session.new(client)

user = Redd::Models::User.new(client, name: 'Mustermind')
puts user.comment_karma

If `auto_refresh` is `false` or if the access doesn't have an associated `expires_in`, you can manually refresh the token by calling:

session.client.refresh

Also, you can swap out the client's access any time.

new_access = { access_token: '', refresh_token: '', expires_in: 1234 }

session.client.access = Redd::Models::Access.new(script, new_access)
session.client.access = Redd::Models::Access.new(web, new_access)
session.client.access = Redd::Models::Access.new(userless, new_access)

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create the object with

Options Hash (opts):

  • :user_agent (String)

    your app's unique and descriptive user agent

  • :client_id (String)

    the client id of your app

  • :secret (String)

    the app secret (for confidential types, i.e. not installed)

  • :username (String)

    the username of your bot (only for script)

  • :password (String)

    the plaintext password of your bot (only for script)

  • :redirect_uri (String)

    the provided redirect URI (only for web and installed)

  • :code (String)

    the code given by reddit (required for web and installed)

Returns:

See Also:


61
62
63
64
65
# File 'lib/redd.rb', line 61

def it(opts = {})
  api_client = script(opts) || web(opts) || userless(opts)
  raise "couldn't guess app type" unless api_client
  Models::Session.new(api_client)
end

.url(client_id:, redirect_uri:, response_type: 'code', state: '', scope: ['identity'], duration: 'temporary') ⇒ String

Create a url to send to users for authorization.

Parameters:

  • response_type ('code', 'token') (defaults to: 'code')

    the type of response from reddit

  • state (String) (defaults to: '')

    a randomly generated token to avoid CSRF attacks.

  • client_id (String)

    the client id of the app

  • redirect_uri (String)

    the URI for reddit to redirect to after authorization

  • scope (Array<String>) (defaults to: ['identity'])

    an array of scopes to request

  • duration ('temporary', 'permanent') (defaults to: 'temporary')

    the duration to request the code for (only applies when response_type is 'code')

Returns:

  • (String)

    the generated url


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/redd.rb', line 76

def url(client_id:, redirect_uri:, response_type: 'code', state: '', scope: ['identity'],
        duration: 'temporary')
  'https://www.reddit.com/api/v1/authorize?' + URI.encode_www_form(
    client_id: client_id,
    redirect_uri: redirect_uri,
    state: state,
    scope: scope.join(','),
    response_type: response_type,
    duration: duration
  )
end