Class: TVDB::Service::Threading::ThreadedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/tvdb_client/service/threading/threaded_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ThreadedRequest

Returns a new instance of ThreadedRequest.



9
10
11
12
13
# File 'lib/tvdb_client/service/threading/threaded_request.rb', line 9

def initialize( options )
  @connection = options.fetch( :connection )
  @pool_size  = options.fetch( :pool_size )  { 10 }
  @threads    = Array.new
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/tvdb_client/service/threading/threaded_request.rb', line 6

def connection
  @connection
end

#pool_sizeObject (readonly)

Returns the value of attribute pool_size.



6
7
8
# File 'lib/tvdb_client/service/threading/threaded_request.rb', line 6

def pool_size
  @pool_size
end

#threadsObject

Returns the value of attribute threads.



7
8
9
# File 'lib/tvdb_client/service/threading/threaded_request.rb', line 7

def threads
  @threads
end

Instance Method Details

#make_request(route) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tvdb_client/service/threading/threaded_request.rb', line 15

def make_request( route )
  response   = connection.get( route )

  return response if response.code != 200

  links      = connection.get( route ).body["links"]
  first_page = links["first"]
  last_page  = links["last"]
  @results   = Array.new

  for page in first_page..last_page
    manage_thread_pool

    threads << Thread.new( page ) do |page_num|
      params   = { :params => { page: page_num } }
      @results << connection.get( route, params ).body["data"]
    end
  end

  threads.each { |thread| thread.join }

  return @results.flatten
end