class RedditApi::Posts

Attributes

client[R]
post_factory[R]
query_factory[R]

Public Class Methods

new() click to toggle source
# File lib/reddit_api/posts.rb, line 5
def initialize
  @client = RedditApi::Client.new
  @post_factory = RedditApi::Post
  @query_factory =  RedditApi::Query
end

Public Instance Methods

top(subreddit, count) click to toggle source
# File lib/reddit_api/posts.rb, line 11
def top(subreddit, count)
  posts_data = top_data(subreddit, count)
  posts_data = map_to_params(posts_data)
  posts_data = filter_out(posts_data, :stickied_posts)
  build_all_posts(posts_data)
end

Private Instance Methods

build_all_posts(posts_data) click to toggle source
# File lib/reddit_api/posts.rb, line 54
def build_all_posts(posts_data)
  posts_data.map! do |post_data|
    build_post(post_data)
  end
end
build_post(post_data) click to toggle source
# File lib/reddit_api/posts.rb, line 60
def build_post(post_data)
  post_factory.new(post_data)
end
build_query(subreddit, count) click to toggle source
# File lib/reddit_api/posts.rb, line 27
def build_query(subreddit, count)
  endpoint = URI.encode("r/#{subreddit.name}/hot.json")
  query_factory.new(count: count,
                    endpoint: endpoint,
                    resource: :post)
end
filter_out(posts_data, filter) click to toggle source
# File lib/reddit_api/posts.rb, line 34
def filter_out(posts_data, filter)
  send(filter, posts_data)
end
map_to_params(posts_data) click to toggle source
# File lib/reddit_api/posts.rb, line 38
def map_to_params(posts_data)
  post_params = []
  posts_data.each do |post_data|
    if params = post_data["data"]
      post_params.push(params)
    end
  end
  post_params
end
stickied_posts(posts_data) click to toggle source
# File lib/reddit_api/posts.rb, line 48
def stickied_posts(posts_data)
  posts_data.select do |post_data|
    !post_data["stickied"]
  end
end
top_data(subreddit, count) click to toggle source
# File lib/reddit_api/posts.rb, line 21
def top_data(subreddit, count)
  query = build_query(subreddit, count)
  client.get(query)
  query.captured_records
end