class AdnHashtagPhotos::PostsLoader

This class loads all posts for given hashtag

Public Class Methods

new(hashtag) click to toggle source

@param [String] hashtag Hashtag to filter posts by

# File lib/adn_hashtag_photos/posts_loader.rb, line 7
def initialize hashtag
  @posts_data = []
  @posts_url = 'https://api.app.net/posts/tag/%s?include_annotations=1&count=20'%[
    hashtag
  ]

end

Public Instance Methods

load_posts(before_id = nil, loaded_posts = []) click to toggle source

This method recursively loads older posts as long as there are any

@param [Nil, Integer] before_id Id of oldest post we have so far @param [Array] loaded_posts

@return [Array]

# File lib/adn_hashtag_photos/posts_loader.rb, line 30
def load_posts before_id = nil, loaded_posts = []
  posts_url = @posts_url
  posts_url += '&before_id=' + before_id.to_s if before_id

  the_response = RestClient.get(posts_url)

  this_load = JSON.parse( the_response,
                          symbolize_names: true
  )

  loaded_posts.push(this_load[:data]).flatten!

  if this_load[:meta][:more]
    load_posts(
        this_load[:data].last[:id],
        loaded_posts
      )
  else
    loaded_posts
  end
end
posts() click to toggle source

@return [Array]

# File lib/adn_hashtag_photos/posts_loader.rb, line 16
def posts
  if @posts_data.empty?
    @posts_data = load_posts
  end

  @posts_data
end