Class: Snooby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/snooby/client.rb

Overview

Interface through which Snooby does all of its interacting with the API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent = "Snooby, #{rand}") ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/snooby/client.rb', line 9

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
end

Instance Attribute Details

#idObject (readonly)

Exposes username to raise a proper error in case of an attempt to delete another user’s content, modhash (uh) for sending along in the headers on API calls that change state, and id for (un)friending.



7
8
9
# File 'lib/snooby/client.rb', line 7

def id
  @id
end

#uhObject (readonly)

Exposes username to raise a proper error in case of an attempt to delete another user’s content, modhash (uh) for sending along in the headers on API calls that change state, and id for (un)friending.



7
8
9
# File 'lib/snooby/client.rb', line 7

def uh
  @uh
end

#usernameObject (readonly)

Exposes username to raise a proper error in case of an attempt to delete another user’s content, modhash (uh) for sending along in the headers on API calls that change state, and id for (un)friending.



7
8
9
# File 'lib/snooby/client.rb', line 7

def username
  @username
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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/snooby/client.rb', line 19

def authorize!(user, passwd, force_update = false)
  @username = user

  if Snooby.config['auth'][user] && !force_update
    # Authorization data exists, skip login and potential rate-limiting.
    @uh, @cookie, @id = Snooby.config['auth'][user]
  else
    data = {:user => user, :passwd => passwd, :api_type => 'json'}
    resp = Snooby.request(Paths[:login] % user, data)
    json = JSON.parse(resp)['json']

    # Will fire for incorrect login credentials and when rate-limited.
    raise RedditError, jj(json) unless json['errors'].empty?

    # Parse authorization data.
    @uh, @cookie = 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, @cookie, @id]
  File.open('.snooby', 'w') { |f| f << Snooby.config.to_json }

  # Allows Snooby's classes to access the currently authorized client.
  Snooby.active = self
end

#compose(to, subject, text) ⇒ Object Also known as: message



100
101
102
103
# File 'lib/snooby/client.rb', line 100

def compose(to, subject, text)
  data = {:to => to, :subject => subject, :text => text}
  Snooby.request(Paths[:compose], data)
end

#friend(name) ⇒ Object

Convenience methods.



82
83
84
# File 'lib/snooby/client.rb', line 82

def friend(name)
  User.new(name).friend
end

#meObject

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.



71
72
73
# File 'lib/snooby/client.rb', line 71

def me
  JSON.parse(Snooby.request(Paths[:me]))['data']
end

#saved(count = 25) ⇒ Object

Returns an array of structs containing the current client’s saved posts.



76
77
78
# File 'lib/snooby/client.rb', line 76

def saved(count = 25)
  Snooby.build(Post, :saved, nil, count)
end

#subreddit(name = nil) ⇒ Object Also known as: r

Returns a Subreddit object through which all relevant data is accessed.



63
64
65
# File 'lib/snooby/client.rb', line 63

def subreddit(name = nil)
  Subreddit.new(name)
end

#subscribe(name) ⇒ Object Also known as: sub



90
91
92
# File 'lib/snooby/client.rb', line 90

def subscribe(name)
  Subreddit.new(name).subscribe
end

#unfriend(name) ⇒ Object



86
87
88
# File 'lib/snooby/client.rb', line 86

def unfriend(name)
  User.new(name).unfriend
end

#unsubscribe(name) ⇒ Object Also known as: unsub



95
96
97
# File 'lib/snooby/client.rb', line 95

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.



57
58
59
# File 'lib/snooby/client.rb', line 57

def user(name)
  User.new(name)
end