class MyLocalPutio::PutioCli

Constants

ROOT

Attributes

configuration[R]
endpoint[R]
http[R]
logger[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 6
def initialize(configuration)
  @configuration = configuration
  @logger = configuration.logger
  @endpoint = URI(ROOT)
  setup_connection
end

Public Instance Methods

delete_file(id) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 18
def delete_file(id)
  args = {file_ids: id}
  post("files/delete", args)
end
get_download_url(id) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 27
def get_download_url(id)
  get("files/#{id}/url")
end
get_files(parent_id=nil) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 13
def get_files(parent_id=nil)
  args = parent_id ? {parent_id: parent_id, sort: "NAME_DESC"} : {sort: "DATE_DESC"}
  get("files/list", args)
end
get_subtitles(id) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 23
def get_subtitles(id)
  get("files/#{id}/subtitles")
end

Protected Instance Methods

as_json(res) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 79
def as_json(res)
  raise "woot? #{res.inspect}" unless res.is_a?(Net::HTTPSuccess)
  YAML.load res.body
end
get(path, args={}) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 47
def get(path, args={})
  url = to_url(path)
  url.query = URI.encode_www_form to_args(args)
  req = Net::HTTP::Get.new(url.request_uri)
  logger.debug "GET #{url}"
  as_json http.request(req)
end
http_library() click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 33
def http_library
  if @configuration.socks_enabled?
    Net::HTTP::SOCKSProxy(@configuration.socks_host, @configuration.socks_port)
  else
    Net::HTTP
  end
end
post(path, args={}) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 55
def post(path, args={})
  url = to_url(path)
  args = to_args(args)
  logger.debug "POST #{url} -- #{args.inspect}"
  req = Net::HTTP::Post.new(url)
  req.set_form_data(args)
  as_json http.request(req)
end
setup_connection() click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 41
def setup_connection
  @http = http_library.new(@endpoint.host, @endpoint.port)
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
to_args(args={}) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 70
def to_args(args={})
  ret = {}
  args.each_pair do |k,v|
    ret[k.to_s] = v
  end
  args["oauth_token"] = configuration.token
  args
end
to_url(path) click to toggle source
# File lib/my-local-putio/putio_cli.rb, line 64
def to_url(path)
  url = endpoint.dup
  url.path += path
  url
end