Class: RedditApi::Client

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

Constant Summary collapse

DEFAULT_SLEEP_TIME =
self.sleep_time
DEFAULT_MAX_FAILURES =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
# File 'lib/reddit_api/client.rb', line 20

def initialize(args = {})
  @client = args.fetch(:client, HTTParty)
  @requestor = args.fetch(:requestor, RedditApi::Requestor.new(client: client))
  @parser = args.fetch(:parser, RedditApi::ResponseParser)
  @null_response_factory = RedditApi:: NullResponse
  @failures = args.fetch(:failures, 0)
  @max_failures = args.fetch(:max_fail, DEFAULT_MAX_FAILURES)
  @sleep_time = args.fetch(:sleep_time, DEFAULT_SLEEP_TIME)
end

Instance Attribute Details

#failuresObject

Returns the value of attribute failures.



17
18
19
# File 'lib/reddit_api/client.rb', line 17

def failures
  @failures
end

#max_failuresObject (readonly)

Returns the value of attribute max_failures.



17
18
19
# File 'lib/reddit_api/client.rb', line 17

def max_failures
  @max_failures
end

#sleep_timeObject

Returns the value of attribute sleep_time.



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

def sleep_time
  @sleep_time
end

Class Method Details

.sleep_timeObject



6
7
8
9
10
11
12
# File 'lib/reddit_api/client.rb', line 6

def self.sleep_time
  if ENV["ENVIRONMENT"] == "TEST"
    0
  else
    2
  end
end

Instance Method Details

#get(query) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/reddit_api/client.rb', line 30

def get(query)
  while query.capture_count < query.count && failures < max_failures
    response = send_request(query)
    response = parser.parse_response(response, query.count)
    update_progress(query, response)
  end
  reset_failures
end