Class: Kynigos

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Kynigos

Returns a new instance of Kynigos.



6
7
8
9
10
11
# File 'lib/kynigos.rb', line 6

def initialize(params)
  @username = params.fetch(:username)
  @password = params.fetch(:password)
  @client = Snoo::Client.new
  client. username, password
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/kynigos.rb', line 5

def client
  @client
end

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/kynigos.rb', line 5

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/kynigos.rb', line 5

def username
  @username
end

Instance Method Details

#clean_up_listings(sub) ⇒ Object

return title and url of listings in subreddit



38
39
40
41
42
43
44
45
# File 'lib/kynigos.rb', line 38

def clean_up_listings sub
  get_listings(sub)["data"]["children"].map do |listing|
    {
      title: listing["data"]["title"],
      url:   listing["data"]["permalink"]
    }
  end
end

#format_body(keywords, sub) ⇒ Object

return urls of listings after search



62
63
64
65
66
67
# File 'lib/kynigos.rb', line 62

def format_body keywords, sub
  words = [keywords.map(&:split)].flatten
  hunt_for(words, sub).map do |prize|
    prize[:url]
  end.join("\n")
end

#get_listings(sub) ⇒ Object

return the past 100 listings in the subreddit



28
29
30
31
32
33
34
# File 'lib/kynigos.rb', line 28

def get_listings sub
  client.get_listing({
    subreddit: sub,
    page:      'new',
    limit:     100
  })
end

#hunt_for(words, sub) ⇒ Object

search through listings in subreddit for keywords return listings with keywords



51
52
53
54
55
56
57
# File 'lib/kynigos.rb', line 51

def hunt_for words, sub
  clean_up_listings(sub).select do |title|
    title if words.map do |word|
      title[:title].include?(word)
    end.include? true
  end
end

#send_prize(to, keywords, sub) ⇒ Object

send a message to specified reddit user with urls of specific listings that match the specified keywords



18
19
20
# File 'lib/kynigos.rb', line 18

def send_prize to, keywords, sub
  client.send_pm to, title, format_body(keywords, sub)
end

#titleObject



22
23
24
# File 'lib/kynigos.rb', line 22

def title
  "Hunt from #{Time.now.strftime("%b. %e, %Y %I:%M%p %Z")}"
end