Class: AdnHashtagPhotos::PostsLoader

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

Overview

This class loads all posts for given hashtag

Instance Method Summary collapse

Constructor Details

#initialize(hashtag) ⇒ PostsLoader

Returns a new instance of PostsLoader.

Parameters:

  • hashtag (String)

    Hashtag to filter posts by



7
8
9
10
11
12
13
# 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

Instance Method Details

#load_posts(before_id = nil, loaded_posts = []) ⇒ Array

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

Parameters:

  • before_id (Nil, Integer) (defaults to: nil)

    Id of oldest post we have so far

  • loaded_posts (Array) (defaults to: [])

Returns:

  • (Array)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

#postsArray

Returns:

  • (Array)


16
17
18
19
20
21
22
# File 'lib/adn_hashtag_photos/posts_loader.rb', line 16

def posts
  if @posts_data.empty?
    @posts_data = load_posts
  end

  @posts_data
end