torckapi 0.0.10 → 0.0.11

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: 3a0baba0c9a96c4a5d53910a0aa79c8aae7b7700
4
- data.tar.gz: ab9bbfc9b647d4d86051b53f2e4f34cc81ece987
3
+ metadata.gz: af443d831abf10bd6fbedf125e88dc362488fa49
4
+ data.tar.gz: de96aca388dc814092a9d8a7a20e75c7569f1d06
5
5
  SHA512:
6
- metadata.gz: 42281cee6c47ef4a9b9bba6dd237cb21610fd2f79049b85e1d2c9fc36a8a9c320f6ebd43b05f76c884b927eae2a32774e64992586aa2d18847854db7c91ce2b5
7
- data.tar.gz: 529731b856d62142d9fdddcf5c50ec48a600c3a6bb8bc3ab37bfbc428eadbc336f669e30878de2fbc2594c8624feaef68f1ef9b70a254b12f6d8db8c5ac67984
6
+ metadata.gz: 2f3202afa722b9ee81318bd7bb6f4bca4b3d15e6421ceacd2265b2ea542cfd634a4c5d1ec34e4689bd5dbd21ef82277aea6f16fcbab389e584467e5e38c2fe98
7
+ data.tar.gz: 908c4d75b6966cec35789dbd45f0db581dcc92d72ff23bec8fdaf58651089542a63abed32eca39f60d9a672b1c8eccd379fab637f48d98e80fe8abb09bebf881
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Torckapi — torrent tracker querying API
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/torckapi.png)](http://badge.fury.io/rb/torckapi)
4
+ [![Code Climate](https://codeclimate.com/github/krupenik/torckapi.png)](https://codeclimate.com/github/krupenik/torckapi)
5
+ [![Dependency Status](https://gemnasium.com/krupenik/torckapi.png)](https://gemnasium.com/krupenik/torckapi)
6
+
3
7
  ## Description
4
8
 
5
9
  Torckapi is a querying interface to torrent trackers.
@@ -7,10 +7,8 @@ module Torckapi
7
7
  class InvalidSchemeError < Error; end
8
8
  class ConnectionFailedError < Error; end
9
9
  class CommunicationError < Error; end
10
- class CommunicationTimeoutError < CommunicationError; end
11
10
  class CommunicationFailedError < CommunicationError; end
12
- class AnnounceFailedError < CommunicationFailedError; end
13
- class ScrapeFailedError < CommunicationFailedError; end
11
+ class CommunicationTimeoutError < CommunicationError; end
14
12
  end
15
13
 
16
14
  module Response
@@ -19,8 +19,6 @@ 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?
24
22
  new info_hash, *data[4..11].unpack('L>2'), peers_from_compact(data[12..-1] || '')
25
23
  end
26
24
 
@@ -30,8 +28,6 @@ module Torckapi
30
28
  # @param compact [true, false] is peer data in compact format?
31
29
  # @return [Torckapi::Response::Announce] response
32
30
  def self.from_http info_hash, data, compact=true
33
- raise ArgumentError, "info_hash cannot be nil" if info_hash.nil?
34
- raise ArgumentError, "data cannot be nil" if data.nil?
35
31
  bdecoded_data = BEncode.load(data)
36
32
  new info_hash, *bdecoded_data.values_at("incomplete", "complete"), peers_from_compact(bdecoded_data["peers"])
37
33
  end
@@ -14,8 +14,6 @@ module Torckapi
14
14
  # @param data [String] UDP response data (omit action and transaction_id)
15
15
  # @return [Torckapi::Response::Error] response
16
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
17
  new info_hashes, data
20
18
  end
21
19
 
@@ -11,9 +11,7 @@ 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?
16
- raise ArgumentError, "data size invalid" if data.length != info_hashes.count * 12
14
+ raise ArgumentError, "data does not match info_hashes" if data.length != info_hashes.count * 12
17
15
  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)] })]
18
16
  end
19
17
 
@@ -10,6 +10,15 @@ module Torckapi
10
10
  class UDP < Base
11
11
  CONNECTION_TIMEOUT = 60
12
12
 
13
+ # index = action (connect, announce, scrape, error)
14
+ # [response class, minimum response length]
15
+ RESPONSES = [
16
+ [nil, 16],
17
+ [Torckapi::Response::Announce, 20],
18
+ [Torckapi::Response::Scrape, 8],
19
+ [Torckapi::Response::Error, 8]
20
+ ].freeze
21
+
13
22
  # (see Base#announce)
14
23
  def announce info_hash
15
24
  super info_hash
@@ -29,19 +38,11 @@ module Torckapi
29
38
  response = communicate action, data
30
39
 
31
40
  raise CommunicationFailedError if response.nil?
41
+ action = response[0][0..3].unpack('L>')[0]
42
+ raise CommunicationFailedError if RESPONSES[action][1] > response[0].length
32
43
 
33
44
  begin
34
- case response[0][0..3].unpack('L>')[0] # action
35
- when 1
36
- raise AnnounceFailedError if 20 > response[0].length
37
- Torckapi::Response::Announce.from_udp(*args, response[0][8..-1])
38
- when 2
39
- raise ScrapeFailedError if 8 > response[0].length
40
- Torckapi::Response::Scrape.from_udp(*args, response[0][8..-1])
41
- when 3
42
- raise CommunicationFailedError if 8 > response[0].length
43
- Torckapi::Response::Error.from_udp(*args, response[0][8..-1])
44
- end
45
+ RESPONSES[action][0].from_udp(*args, response[0][8..-1])
45
46
  rescue Torckapi::Response::ArgumentError => e
46
47
  $stderr.puts "Error: #{e.inspect}"
47
48
  $stderr.puts "Response: #{response.inspect}"
@@ -1,3 +1,3 @@
1
1
  module Torckapi
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
data/lib/torckapi.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require 'uri'
2
2
  require 'torckapi/version'
3
3
  require 'torckapi/errors'
4
- require 'torckapi/tracker/http'
5
- require 'torckapi/tracker/udp'
6
4
  require 'torckapi/response/announce'
7
5
  require 'torckapi/response/error'
8
6
  require 'torckapi/response/scrape'
7
+ require 'torckapi/tracker/http'
8
+ require 'torckapi/tracker/udp'
9
9
 
10
10
  module Torckapi
11
11
  # Creates a tracker interface instance
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.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Krupenik