torckapi 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8068649a0efc6fd9f1d714dd29d3b3046444c316
4
- data.tar.gz: 8956d195256c27b0cd1af9459c87f739cb616895
3
+ metadata.gz: 2c9a7381eb1ab2a6327c590e2f536e6c37e57b08
4
+ data.tar.gz: 8b3eb6f625736f2189f9c1a62f0185d04bcfeb9b
5
5
  SHA512:
6
- metadata.gz: d354588908b2023258217d798a614a1db970e49a95b56389f362e980e694a29b5ddf147aacebdf81f5b9aa31de0ab50b7e1fc884beefce72bedf96c6207790e7
7
- data.tar.gz: ecb437764df589846cf50f2153c13404b753609da9ea587437263b052ca772037b1d944923694219d0922227a8c710975a0b016f103b593a5874c6c97775b56b
6
+ metadata.gz: d2b7de86de6fdd7b2041616b9b781f5a2cf74505cf6a9a1690844d409b6bc377afe8dab0053948ded9cbe73e4caaedfb4f9ff323b6b9f1b7e2b66d1e75abbfe9
7
+ data.tar.gz: 92faf338aa7741f54996764877c35da4f8f6910b5904cae16155e503c2a69e50b56fc8baf3365f6c520ef411f231fbf5843170aaf6d8ba0d732b27fd87eaf835
@@ -9,4 +9,9 @@ module Torckapi
9
9
  class CommunicationFailedError < CommunicationError; end
10
10
  class CommunicationTimeoutError < CommunicationError; end
11
11
  end
12
+
13
+ module Response
14
+ class Error < Torckapi::Error; end
15
+ class ArgumentError < Error; end
16
+ end
12
17
  end
@@ -19,6 +19,8 @@ module Torckapi
19
19
  # @param data [String] UDP response data (omit action and transaction_id)
20
20
  # @return [Torckapi::Response::Announce] response
21
21
  def self.from_udp info_hash, data
22
+ raise ArgumentError, "info_hash cannot be nil" if info_hash.nil?
23
+ raise ArgumentError, "data cannot be nil" if data.nil?
22
24
  new info_hash, *data[4..11].unpack('L>2'), peers_from_compact(data[12..-1])
23
25
  end
24
26
 
@@ -3,22 +3,30 @@ module Torckapi
3
3
  class Error
4
4
  # @!attribute [r] info_hash
5
5
  # @return [String] 40-char hexadecimal string
6
+ # @!attribute [r] info_hashes
7
+ # @return [Array<String>] an array of 40-char hexadecimal strings
6
8
  # @!attribute [r] message
7
9
  # @return [String] error description
8
- attr_reader :info_hash, :message
10
+ attr_reader :info_hash, :info_hashes, :message
9
11
 
10
12
  # Construct response object from udp response data
11
13
  # @param info_hash [String] 40-char hexadecimal string
12
14
  # @param data [String] UDP response data (omit action and transaction_id)
13
15
  # @return [Torckapi::Response::Error] response
14
- def self.from_udp info_hash, data
15
- new info_hash, data
16
+ def self.from_udp info_hashes, data
17
+ raise ArgumentError, "info_hashes cannot be nil" if info_hashes.nil?
18
+ raise ArgumentError, "data cannot be nil" if data.nil?
19
+ new info_hashes, data
20
+ end
21
+
22
+ def info_hash
23
+ info_hashes[0]
16
24
  end
17
25
 
18
26
  private
19
27
 
20
- def initialize info_hash, message
21
- @info_hash = info_hash
28
+ def initialize info_hashes, message
29
+ @info_hashes = [*info_hashes]
22
30
  @message = message
23
31
  end
24
32
  end
@@ -11,6 +11,8 @@ module Torckapi
11
11
  # @param data [String] UDP response data (omit action and transaction_id)
12
12
  # @return [Torckapi::Response::Scrape] response
13
13
  def self.from_udp info_hashes, data
14
+ raise ArgumentError, "info_hashes cannot be nil" if info_hashes.nil?
15
+ raise ArgumentError, "data cannot be nil" if data.nil?
14
16
  new Hash[info_hashes.zip(data.unpack('a12' * (info_hashes.count)).map { |i| Hash[[:seeders, :completed, :leechers].zip i.unpack('L>3').map(&:to_i)] })]
15
17
  end
16
18
 
@@ -28,13 +28,19 @@ module Torckapi
28
28
  connect
29
29
  response = communicate action, data
30
30
 
31
- case response[0][0..3].unpack('L>')[0] # action
32
- when 1
33
- Torckapi::Response::Announce.from_udp(*args, response[0][8..-1])
34
- when 2
35
- Torckapi::Response::Scrape.from_udp(*args, response[0][8..-1])
36
- when 3
37
- Torckapi::Response::Error.from_udp(*args, response[0][8..-1])
31
+ begin
32
+ case response[0][0..3].unpack('L>')[0] # action
33
+ when 1
34
+ Torckapi::Response::Announce.from_udp(*args, response[0][8..-1])
35
+ when 2
36
+ Torckapi::Response::Scrape.from_udp(*args, response[0][8..-1])
37
+ when 3
38
+ Torckapi::Response::Error.from_udp(*args, response[0][8..-1])
39
+ end
40
+ rescue Torckapi::Response::ArgumentError => e
41
+ puts "Error: #{e.inspect}"
42
+ puts "Response: #{response.inspect}"
43
+ raise CommunicationFailedError
38
44
  end
39
45
  end
40
46
 
@@ -1,3 +1,3 @@
1
1
  module Torckapi
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torckapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Krupenik