Class: Rrant::Remote

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/rrant/remote.rb

Overview

Public: Contains necessary logic to contanct remote API, fetch rants and images. Needs to be initialized with store object, since we add fetched rants to it.

Constant Summary collapse

BASE_URL =
'https://www.devrant.io/api/devrant/rants?app=3'
MAX_CYCLE =
10
SLEEP =
0.4

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#bill, #files_path, #image_blank?

Constructor Details

#initialize(store, skip_images = true) ⇒ Remote

Public: Constructor.

store - Instance of Rrant::Store. skip_images - Boolean, when set we won’t download images.



23
24
25
26
27
28
29
# File 'lib/rrant/remote.rb', line 23

def initialize(store, skip_images = true)
  raise Error::InvalidStore unless store.is_a?(Store)

  @rants       = []
  @store       = store
  @skip_images = skip_images
end

Instance Attribute Details

#rantsObject (readonly)

Returns the value of attribute rants.



13
14
15
# File 'lib/rrant/remote.rb', line 13

def rants
  @rants
end

Instance Method Details

#save(min_amount) ⇒ Object

Public: Calls rant fetching methods, adds fetched rants to store and puts basic info to STDOUT. Since rants are fetched in batches of 20, we dont want to discard some of them, thus we accept minimum amount and are ok with more rants fetched.

min_amount - Integer, fetch at least this amount of rants.

Returns an array of rants.



39
40
41
42
43
44
45
46
47
48
# File 'lib/rrant/remote.rb', line 39

def save(min_amount)
  @min_amount = min_amount
  log_start
  build_rants
  fetch_images
  @store.add(@rants)
  log_finish

  @rants
end