Class: Redd::Client::Unauthenticated

Inherits:
Object
  • Object
show all
Includes:
Account, Captcha, LinksComments, Listing, Live, Moderation, Subreddits, Utilities, Wiki
Defined in:
lib/redd/client/unauthenticated.rb,
lib/redd/client/unauthenticated/live.rb,
lib/redd/client/unauthenticated/wiki.rb,
lib/redd/client/unauthenticated/account.rb,
lib/redd/client/unauthenticated/captcha.rb,
lib/redd/client/unauthenticated/listing.rb,
lib/redd/client/unauthenticated/utilities.rb,
lib/redd/client/unauthenticated/moderation.rb,
lib/redd/client/unauthenticated/subreddits.rb,
lib/redd/client/unauthenticated/links_comments.rb

Overview

The Client used to connect without needing login credentials.

Direct Known Subclasses

Authenticated

Defined Under Namespace

Modules: Account, Captcha, LinksComments, Listing, Live, Moderation, Subreddits, Utilities, Wiki

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wiki

#get_wikipages, #wikipage

Methods included from Utilities

#comment_stream, #comments_from_response, #extract_attribute, #extract_fullname, #extract_id, #object_from_body, #object_from_kind, #object_from_response, #objects_from_listing, #submission_stream

Methods included from Subreddits

#get_subreddits, #search_subreddits, #subreddit

Methods included from Moderation

#stylesheet, #stylesheet_url

Methods included from Listing

#by_id, #get_comments, #get_controversial, #get_hot, #get_listing, #get_new, #get_random, #get_top

Methods included from LinksComments

#get_info, #get_replies, #replace_morecomments, #submission_comments

Methods included from Captcha

#captcha_url, #needs_captcha?, #new_captcha

Methods included from Account

#login

Constructor Details

#initialize(options = {}) ⇒ Unauthenticated

Set up an unauthenticated connection to reddit.

Parameters:

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

    A hash of options to connect using.

Options Hash (options):

  • :rate_limit (#after_limit)

    The handler that takes care of rate limiting.

  • :user_agent (String)

    The User-Agent string to use in the header of every request.

  • :api_endpoint (String)

    The main domain to connect to, in this case, the URL for reddit.



52
53
54
55
56
# File 'lib/redd/client/unauthenticated.rb', line 52

def initialize(options = {})
  @rate_limit = options[:rate_limit] || Redd::RateLimit.new
  @user_agent = options[:user_agent] || "Redd/Ruby, v#{Redd::VERSION}"
  @api_endpoint = options[:api_endpoint] || "https://www.reddit.com/"
end

Instance Attribute Details

#api_endpointString

Returns The site to connect to.

Returns:

  • (String)

    The site to connect to.



33
34
35
# File 'lib/redd/client/unauthenticated.rb', line 33

def api_endpoint
  @api_endpoint
end

#rate_limit#after_limit

Returns The handler that takes care of rate limiting.

Returns:

  • (#after_limit)

    The handler that takes care of rate limiting.



41
42
43
# File 'lib/redd/client/unauthenticated.rb', line 41

def rate_limit
  @rate_limit
end

#user_agentString

Returns The user-agent used to communicate with reddit.

Returns:

  • (String)

    The user-agent used to communicate with reddit.



37
38
39
# File 'lib/redd/client/unauthenticated.rb', line 37

def user_agent
  @user_agent
end

Instance Method Details

#connectionFaraday (private)

Gets the Faraday connection or creates one if it doesn’t exist yet.

Returns:

  • (Faraday)

    A new Faraday connection.



68
69
70
71
72
73
74
75
76
77
# File 'lib/redd/client/unauthenticated.rb', line 68

def connection
  @connection ||= Faraday.new(url: api_endpoint) do |faraday|
    faraday.use Faraday::Request::UrlEncoded
    faraday.use Redd::Response::RaiseError
    faraday.use Redd::Response::ParseJson
    faraday.adapter Faraday.default_adapter

    faraday.headers = headers
  end
end

#delete(*args) ⇒ Object (private)

Performs a DELETE request via #request.

See Also:



111
112
113
# File 'lib/redd/client/unauthenticated.rb', line 111

def delete(*args)
  request(:delete, *args)
end

#get(*args) ⇒ Object (private)

Performs a GET request via #request.

See Also:



93
94
95
# File 'lib/redd/client/unauthenticated.rb', line 93

def get(*args)
  request(:get, *args)
end

#headersHash (private)

Returns The headers that are sent with every request.

Returns:

  • (Hash)

    The headers that are sent with every request.



61
62
63
# File 'lib/redd/client/unauthenticated.rb', line 61

def headers
  @headers ||= {"User-Agent" => @user_agent}
end

#post(*args) ⇒ Object (private)

Performs a POST request via #request.

See Also:



99
100
101
# File 'lib/redd/client/unauthenticated.rb', line 99

def post(*args)
  request(:post, *args)
end

#put(*args) ⇒ Object (private)

Performs a PUT request via #request.

See Also:



105
106
107
# File 'lib/redd/client/unauthenticated.rb', line 105

def put(*args)
  request(:put, *args)
end

#request(method, path, params = {}) ⇒ String (private)

Send a request to the given path.

Parameters:

  • method (#to_sym)

    The HTTP verb to use.

  • path (String)

    The path under the api endpoint to request from.

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

    The additional parameters to send (defualt: {}).

Returns:

  • (String)

    The response body.



85
86
87
88
89
# File 'lib/redd/client/unauthenticated.rb', line 85

def request(method, path, params = {})
  rate_limit.after_limit do
    connection.send(method.to_sym, path, params).body
  end
end