Skip to content

Commit

Permalink
Refactored torrent class using rt conversion methods
Browse files Browse the repository at this point in the history
rTorrent provides a bunch of conversion methods which
removed the need to be converting file size, dates and
time. Attribute builder now uses these to do automatic
conversion and creating a _raw attribute for each of
those fields converted.
  • Loading branch information
marcelmorgan committed Sep 4, 2011
1 parent adb8d61 commit 8fd71bf
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 47 deletions.
1 change: 1 addition & 0 deletions lib/retort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'retort/core_ext'
require 'retort/xmlrpc_client'
require 'retort/service'
require 'retort/conversion'
require 'retort/torrent'

module Retort
Expand Down
37 changes: 37 additions & 0 deletions lib/retort/conversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Retort::Conversion


class Call
TYPES = { time: 'to_time', date: 'to_date', size: 'to_xb' }
attr_accessor :attributes

def initialize(prefix)
@attributes = {}
@prefix = prefix
end

def method_missing(*args, &block)
property = args.shift
options = args.shift || {}

type = options[:type]
method_name = options[:name] || property
method_name = "#{@prefix}.#{method_name}" unless @prefix.empty?

if TYPES.member? type
#Syntax is 'to_*=$.....'. Example : 'to_date=$d.get_creation_date'.
@attributes[property] = "#{TYPES[type]}=$#{method_name}"
property = "#{property}_raw"
end
@attributes[property] = method_name
end
end

def build(prefix="")
c = Call.new prefix
yield c
c.attributes
end

end

25 changes: 6 additions & 19 deletions lib/retort/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
class Fixnum
def bytes
self
def truth
return [false,true][self] if (0..1).member? self
end

def kilobytes
bytes / 1024.0
end

def kbs
"#{sprintf('%.2f', kilobytes)}KB/s"
end

def megabytes
kilobytes / 1024.0
end

end

class Float
def percent
def percent_raw
(self * 100).to_i
end
end

def fmt
"#{sprintf('%.2f', self)}"
def percent
"#{sprintf('%.2f', percent_raw)}"
end
end

4 changes: 2 additions & 2 deletions lib/retort/service.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Retort
module Retort

class Config
attr_accessor :url
end

class Service

class << self

def configure
Expand Down
85 changes: 60 additions & 25 deletions lib/retort/torrent.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
module Retort
module Retort

class Exception; end

class Torrent

extend Retort::Conversion

class << self
def attr_mappings
{
info_hash: 'd.hash',
name: 'd.name',
connection_current: 'd.connection_current',
size_bytes: 'd.size_bytes',
completed_bytes: 'd.completed_bytes',
creation_date: 'd.creation_date',
bytes_done: 'd.bytes_done',
up_rate: 'd.up.rate',
down_rate: 'd.down.rate',
seeders: 'd.peers_complete',
leechers: 'd.peers_connected',
is_completed: 'd.complete',
is_active: 'd.is_active',
is_hash_checked: 'd.is_hash_checked',
is_hash_checking: 'd.is_hash_checking',
is_multifile: 'd.is_multi_file',
is_open: 'd.is_open'
}
def attr_mappings

build(:d) do |t|
t.info_hash name: 'hash'
t.name
t.connection_current
t.size name: 'size_bytes', type: :size
t.completed name: 'completed_bytes', type: :size
t.creation_date name: 'creation_date', type: :date
t.downloaded name: 'bytes_done', type: :size
t.up_rate name: 'up.rate', type: :size
t.down_rate name: 'down.rate', type: :size
t.message name: 'get_message'
t.seeders name: 'peers_complete'
t.leechers name: 'peers_connected'
t.state
t.complete
t.is_active
t.is_hash_checked
t.is_hash_checking
t.is_multi_file
t.is_open
end

end

def all(view="main")
methods = attr_mappings.map {|key,value| "#{value}="}
Service.call("d.multicall", view, *methods).map do |r|
Service.call("d.multicall", view, *methods).map do |r|
attributes = Hash[attr_mappings.keys.zip(r)]
Torrent.new attributes
end
end

def views
Service.call "view.list"
end
Expand All @@ -49,9 +56,37 @@ def action(name, info_hash)
attr_accessor *(attr_mappings.keys)

def initialize(attributes)

attributes.each do |attrib, value|
self.instance_variable_set "@#{attrib}".to_sym, value
variable = "@#{attrib}".to_sym
self.instance_variable_set variable, value
if attrib =~ /is_/
bool_value = value.truth
variable = "@#{attrib.to_s.gsub(/is_/, "")}".to_sym
self.instance_variable_set variable, bool_value
end
end

end

def status
return "complete" if @complete.truth
end

def actions
actions = []

actions << (@state.truth ? :stop : :start)
#actions << :check_hash unless @hash_checking
actions << (@open ? :close : :open)

unless @complete.truth
actions << (@active ? :pause : :resume)
end

actions << :erase

actions
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/retort/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Retort
VERSION = "0.0.3"
VERSION = "0.0.4"
end

0 comments on commit 8fd71bf

Please sign in to comment.