Class: Brutalismbot::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brutalismbot/stub.rb,
lib/brutalismbot/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(posts: nil, reddit: nil, slack: nil, twitter: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
# File 'lib/brutalismbot/client.rb', line 10

def initialize(posts:nil, reddit:nil, slack:nil, twitter:nil)
  @posts   = posts   ||   Posts::Client.new
  @reddit  = reddit  ||  Reddit::Client.new
  @slack   = slack   ||   Slack::Client.new
  @twitter = twitter || Twitter::Client.new
end

Instance Attribute Details

#postsObject (readonly)

Returns the value of attribute posts.



8
9
10
# File 'lib/brutalismbot/client.rb', line 8

def posts
  @posts
end

#redditObject (readonly)

Returns the value of attribute reddit.



8
9
10
# File 'lib/brutalismbot/client.rb', line 8

def reddit
  @reddit
end

#slackObject (readonly)

Returns the value of attribute slack.



8
9
10
# File 'lib/brutalismbot/client.rb', line 8

def slack
  @slack
end

#twitterObject (readonly)

Returns the value of attribute twitter.



8
9
10
# File 'lib/brutalismbot/client.rb', line 8

def twitter
  @twitter
end

Class Method Details

.stub(posts: nil, slack: nil) ⇒ Object



19
20
21
# File 'lib/brutalismbot/stub.rb', line 19

def stub(posts:nil, slack:nil)
  new.stub!(posts: posts, slack: slack)
end

Instance Method Details

#pull(limit: nil, min_time: nil, max_time: nil, min_age: nil, dryrun: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/brutalismbot/client.rb', line 17

def pull(limit:nil, min_time:nil, max_time:nil, min_age:nil, dryrun:nil)
  # Get time window for new posts
  min_age  ||= 9000
  min_time ||= @posts.max_time
  max_time ||= Time.now.utc.to_i - min_age.to_i

  # Get posts
  opts  = {q:"self:no AND nsfw:no", restrict_sr: true, sort: "new"}
  posts = @reddit.list(:search, **opts).all

  # Filter, sort, and limit
  posts = posts.select{|post| post.created_between?(min_time, max_time) }
  posts = posts.sort{|a,b| a.created_utc <=> b.created_utc }
  posts = posts.slice(0, limit) unless limit.nil?

  # Persist posts
  posts.map{|post| @posts.push(post, dryrun: dryrun) }
end

#push(post, dryrun: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/brutalismbot/client.rb', line 36

def push(post, dryrun:nil)
  # Push to Twitter
  @twitter.push(post, dryrun: dryrun)

  # Push to Slack
  @slack.push(post, dryrun: dryrun)

  nil
end

#stub!(posts: nil, slack: nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/brutalismbot/stub.rb', line 10

def stub!(posts:nil, slack:nil)
  @posts   = Posts::Client.stub(posts)
  @slack   = Slack::Client.stub(posts)
  @stubbed = true

  self
end