Class: Snooby::Client
- Inherits:
-
Object
- Object
- Snooby::Client
- Defined in:
- lib/snooby/client.rb
Overview
Interface through which Snooby does all of its interacting with the API.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#uh ⇒ Object
readonly
Returns the value of attribute uh.
Instance Method Summary collapse
-
#authorize!(user, passwd, force_update = false) ⇒ Object
Causes the client to be recognized as the given user during API calls.
- #compose(to, subject, text) ⇒ Object (also: #message)
- #domain(name) ⇒ Object
- #friend(name) ⇒ Object
-
#initialize(user_agent = "Snooby, #{rand}") ⇒ Client
constructor
A new instance of Client.
-
#me ⇒ Object
Returns a hash containing the values given by me.json, used internally to obtain the client’s id, but also the most efficient way to check whether or not the client has mail.
-
#saved(count = 25) ⇒ Object
Returns an array of structs containing the current client’s saved posts.
- #submit(name, title, content) ⇒ Object
-
#subreddit(name = nil) ⇒ Object
(also: #r)
Returns a Subreddit object through which all relevant data is accessed.
- #subscribe(name) ⇒ Object (also: #sub)
- #unfriend(name) ⇒ Object
- #unsubscribe(name) ⇒ Object (also: #unsub)
-
#user(name) ⇒ Object
(also: #u)
Returns a User object through which all relevant data is accessed.
Constructor Details
#initialize(user_agent = "Snooby, #{rand}") ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/snooby/client.rb', line 7 def initialize(user_agent = "Snooby, #{rand}") # Net::HTTP's default User-Agent, "Ruby", is banned on reddit due to its # frequent improper use; cheers to Eric Hodel (drbrain) for implementing # this workaround for net-http-persistent. Conn.override_headers['User-Agent'] = user_agent # Allows Snooby's classes to access the currently active client, even if # it has not been authorized, allowing the user to log in mid-execution. Snooby.active = self end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/snooby/client.rb', line 5 def id @id end |
#uh ⇒ Object (readonly)
Returns the value of attribute uh.
5 6 7 |
# File 'lib/snooby/client.rb', line 5 def uh @uh end |
Instance Method Details
#authorize!(user, passwd, force_update = false) ⇒ Object
Causes the client to be recognized as the given user during API calls. GET operations do not need to be authorized, so if your intent is simply to gather data, feel free to disregard this method entirely.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/snooby/client.rb', line 21 def (user, passwd, force_update = false) if Snooby.config['auth'][user] && !force_update # Authorization data exists, skip login and potential rate-limiting. @uh, , @id = Snooby.config['auth'][user] else data = {:user => user, :passwd => passwd} json = Snooby.request(Paths[:login] % user, data)['json'] @uh, = json['data'].values end # Sets the reddit_session cookie required for API calls to be recognized # as coming from the intended user. Uses override_headers to allow for # switching the current user mid-execution, if so desired. Conn.override_headers['Cookie'] = "reddit_session=#{@cookie}" # A second call is made, if required, to grab the client's id, which is # necessary for (un)friending. @id ||= "t2_#{me['id']}" # Updates the config file to faciliate one-time authorization. This works # because the authorization data is immortal unless the password has been # changed; enable the force_update parameter if such is the case. Snooby.config['auth'][user] = [@uh, , @id] end |
#compose(to, subject, text) ⇒ Object Also known as: message
93 94 95 96 |
# File 'lib/snooby/client.rb', line 93 def compose(to, subject, text) data = {:to => to, :subject => subject, :text => text} Snooby.request Paths[:compose], data end |
#domain(name) ⇒ Object
57 58 59 |
# File 'lib/snooby/client.rb', line 57 def domain(name) Domain.new name end |
#friend(name) ⇒ Object
85 86 87 |
# File 'lib/snooby/client.rb', line 85 def friend(name) User.new(name).friend end |
#me ⇒ Object
Returns a hash containing the values given by me.json, used internally to obtain the client’s id, but also the most efficient way to check whether or not the client has mail.
64 65 66 |
# File 'lib/snooby/client.rb', line 64 def me Snooby.request(Paths[:me])['data'] end |
#saved(count = 25) ⇒ Object
Returns an array of structs containing the current client’s saved posts.
69 70 71 |
# File 'lib/snooby/client.rb', line 69 def saved(count = 25) Snooby.build Post, :saved, nil, count end |
#submit(name, title, content) ⇒ Object
73 74 75 |
# File 'lib/snooby/client.rb', line 73 def submit(name, title, content) Subreddit.new(name).submit title, content end |
#subreddit(name = nil) ⇒ Object Also known as: r
Returns a Subreddit object through which all relevant data is accessed. Supplying no name provides access to the client’s front page.
53 54 55 |
# File 'lib/snooby/client.rb', line 53 def subreddit(name = nil) Subreddit.new name end |
#subscribe(name) ⇒ Object Also known as: sub
77 78 79 |
# File 'lib/snooby/client.rb', line 77 def subscribe(name) Subreddit.new(name).subscribe end |
#unfriend(name) ⇒ Object
89 90 91 |
# File 'lib/snooby/client.rb', line 89 def unfriend(name) User.new(name).unfriend end |
#unsubscribe(name) ⇒ Object Also known as: unsub
81 82 83 |
# File 'lib/snooby/client.rb', line 81 def unsubscribe(name) Subreddit.new(name).unsubscribe end |
#user(name) ⇒ Object Also known as: u
Returns a User object through which all relevant data is accessed.
47 48 49 |
# File 'lib/snooby/client.rb', line 47 def user(name) User.new name end |