Class: Leecher::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, aria2_client, gheed_client) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
# File 'lib/leecher/client.rb', line 12

def initialize(config, aria2_client, gheed_client)
  @config = config
  @aria2_client = aria2_client
  @gheed_client = gheed_client
end

Instance Attribute Details

#aria2_clientObject (readonly)

Returns the value of attribute aria2_client.



10
11
12
# File 'lib/leecher/client.rb', line 10

def aria2_client
  @aria2_client
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/leecher/client.rb', line 10

def config
  @config
end

#gheed_clientObject (readonly)

Returns the value of attribute gheed_client.



10
11
12
# File 'lib/leecher/client.rb', line 10

def gheed_client
  @gheed_client
end

Instance Method Details

#filename(relative) ⇒ Object



23
24
25
# File 'lib/leecher/client.rb', line 23

def filename(relative)
  File.join(@config.work_dir, relative)
end

#notify_gheed(uri, state) ⇒ Object



51
52
53
54
# File 'lib/leecher/client.rb', line 51

def notify_gheed(uri, state)
  metalink, deliveries = gheed_client.state_change(uri, state)
  queue.set_store_entry(metalink["metalink_url"], :state => :complete)
end


27
28
29
30
31
32
33
# File 'lib/leecher/client.rb', line 27

def process_message_on_metalink_queue(msg)
  # URIs will only ever be one (contract with gheed)
  uris, options = MultiJson.decode(msg)
  queue.add_uris(uris)
  gid = aria2_client.add_uri(uris, options)
  queue.set_store_entry(uris.first, :gid => gid.to_i, :state => :added_to_aria2)
end

#queueObject



19
20
21
# File 'lib/leecher/client.rb', line 19

def queue
  @queue ||= MetalinkQueue.new(filename("queue.yml"))
end

#state_change(gid) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/leecher/client.rb', line 35

def state_change(gid)
  response = aria2_client.tell_status(gid)
  uri = response["files"].first["uris"].first["uri"]
  state = response["status"].to_sym
  queue.set_store_entry(uri, :gid => gid, :state => state)

  # Don't tell Gheed about completing metalink downloads - we only
  # need to say when the file the metalink speaks about changes state
  unless File.extname(uri) == ".metalink"
    # .. and to a state we care about
    if [:complete, :error].include?(state)
      notify_gheed(uri, state)
    end
  end
end

#status_updateObject



56
57
58
59
60
61
62
63
64
# File 'lib/leecher/client.rb', line 56

def status_update
  status = Status.new(aria2_client)
  if status.changed?(@status)
    @status = status
    gheed_client.status_update(@status.to_params)
  else
    false
  end
end