retort 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ *.swp
data/lib/retort.rb CHANGED
@@ -2,6 +2,7 @@ require 'retort/version'
2
2
  require 'retort/core_ext'
3
3
  require 'retort/xmlrpc_client'
4
4
  require 'retort/service'
5
+ require 'retort/conversion'
5
6
  require 'retort/torrent'
6
7
 
7
8
  module Retort
@@ -0,0 +1,37 @@
1
+ module Retort::Conversion
2
+
3
+
4
+ class Call
5
+ TYPES = { time: 'to_time', date: 'to_date', size: 'to_xb' }
6
+ attr_accessor :attributes
7
+
8
+ def initialize(prefix)
9
+ @attributes = {}
10
+ @prefix = prefix
11
+ end
12
+
13
+ def method_missing(*args, &block)
14
+ property = args.shift
15
+ options = args.shift || {}
16
+
17
+ type = options[:type]
18
+ method_name = options[:name] || property
19
+ method_name = "#{@prefix}.#{method_name}" unless @prefix.empty?
20
+
21
+ if TYPES.member? type
22
+ #Syntax is 'to_*=$.....'. Example : 'to_date=$d.get_creation_date'.
23
+ @attributes[property] = "#{TYPES[type]}=$#{method_name}"
24
+ property = "#{property}_raw"
25
+ end
26
+ @attributes[property] = method_name
27
+ end
28
+ end
29
+
30
+ def build(prefix="")
31
+ c = Call.new prefix
32
+ yield c
33
+ c.attributes
34
+ end
35
+
36
+ end
37
+
@@ -1,29 +1,16 @@
1
1
  class Fixnum
2
- def bytes
3
- self
2
+ def truth
3
+ return [false,true][self] if (0..1).member? self
4
4
  end
5
-
6
- def kilobytes
7
- bytes / 1024.0
8
- end
9
-
10
- def kbs
11
- "#{sprintf('%.2f', kilobytes)}KB/s"
12
- end
13
-
14
- def megabytes
15
- kilobytes / 1024.0
16
- end
17
-
18
5
  end
19
6
 
20
7
  class Float
21
- def percent
8
+ def percent_raw
22
9
  (self * 100).to_i
23
- end
10
+ end
24
11
 
25
- def fmt
26
- "#{sprintf('%.2f', self)}"
12
+ def percent
13
+ "#{sprintf('%.2f', percent_raw)}"
27
14
  end
28
15
  end
29
16
 
@@ -1,11 +1,11 @@
1
- module Retort
1
+ module Retort
2
2
 
3
3
  class Config
4
4
  attr_accessor :url
5
5
  end
6
6
 
7
7
  class Service
8
-
8
+
9
9
  class << self
10
10
 
11
11
  def configure
@@ -1,38 +1,45 @@
1
- module Retort
1
+ module Retort
2
+
3
+ class Exception; end
2
4
 
3
5
  class Torrent
4
-
6
+ extend Retort::Conversion
7
+
5
8
  class << self
6
- def attr_mappings
7
- {
8
- info_hash: 'd.hash',
9
- name: 'd.name',
10
- connection_current: 'd.connection_current',
11
- size_bytes: 'd.size_bytes',
12
- completed_bytes: 'd.completed_bytes',
13
- creation_date: 'd.creation_date',
14
- bytes_done: 'd.bytes_done',
15
- up_rate: 'd.up.rate',
16
- down_rate: 'd.down.rate',
17
- seeders: 'd.peers_complete',
18
- leechers: 'd.peers_connected',
19
- is_completed: 'd.complete',
20
- is_active: 'd.is_active',
21
- is_hash_checked: 'd.is_hash_checked',
22
- is_hash_checking: 'd.is_hash_checking',
23
- is_multifile: 'd.is_multi_file',
24
- is_open: 'd.is_open'
25
- }
9
+ def attr_mappings
10
+
11
+ build(:d) do |t|
12
+ t.info_hash name: 'hash'
13
+ t.name
14
+ t.connection_current
15
+ t.size name: 'size_bytes', type: :size
16
+ t.completed name: 'completed_bytes', type: :size
17
+ t.creation_date name: 'creation_date', type: :date
18
+ t.downloaded name: 'bytes_done', type: :size
19
+ t.up_rate name: 'up.rate', type: :size
20
+ t.down_rate name: 'down.rate', type: :size
21
+ t.message name: 'get_message'
22
+ t.seeders name: 'peers_complete'
23
+ t.leechers name: 'peers_connected'
24
+ t.state
25
+ t.complete
26
+ t.is_active
27
+ t.is_hash_checked
28
+ t.is_hash_checking
29
+ t.is_multi_file
30
+ t.is_open
31
+ end
32
+
26
33
  end
27
34
 
28
35
  def all(view="main")
29
36
  methods = attr_mappings.map {|key,value| "#{value}="}
30
- Service.call("d.multicall", view, *methods).map do |r|
37
+ Service.call("d.multicall", view, *methods).map do |r|
31
38
  attributes = Hash[attr_mappings.keys.zip(r)]
32
39
  Torrent.new attributes
33
40
  end
34
41
  end
35
-
42
+
36
43
  def views
37
44
  Service.call "view.list"
38
45
  end
@@ -49,9 +56,37 @@ module Retort
49
56
  attr_accessor *(attr_mappings.keys)
50
57
 
51
58
  def initialize(attributes)
59
+
52
60
  attributes.each do |attrib, value|
53
- self.instance_variable_set "@#{attrib}".to_sym, value
61
+ variable = "@#{attrib}".to_sym
62
+ self.instance_variable_set variable, value
63
+ if attrib =~ /is_/
64
+ bool_value = value.truth
65
+ variable = "@#{attrib.to_s.gsub(/is_/, "")}".to_sym
66
+ self.instance_variable_set variable, bool_value
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ def status
73
+ return "complete" if @complete.truth
74
+ end
75
+
76
+ def actions
77
+ actions = []
78
+
79
+ actions << (@state.truth ? :stop : :start)
80
+ #actions << :check_hash unless @hash_checking
81
+ actions << (@open ? :close : :open)
82
+
83
+ unless @complete.truth
84
+ actions << (@active ? :pause : :resume)
54
85
  end
86
+
87
+ actions << :erase
88
+
89
+ actions
55
90
  end
56
91
  end
57
92
  end
@@ -1,3 +1,3 @@
1
1
  module Retort
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-02 00:00:00.000000000Z
12
+ date: 2011-09-04 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: An rTorrent xmlrpc wrapper written in ruby
15
15
  email:
@@ -24,6 +24,7 @@ files:
24
24
  - README.rdoc
25
25
  - Rakefile
26
26
  - lib/retort.rb
27
+ - lib/retort/conversion.rb
27
28
  - lib/retort/core_ext.rb
28
29
  - lib/retort/service.rb
29
30
  - lib/retort/torrent.rb