Class: Subdb::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/subdb/video.rb

Constant Summary collapse

API =
"http://api.thesubdb.com/"
SANDBOX =
"http://sandbox.thesubdb.com/"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Video

Returns a new instance of Video.



49
50
51
52
53
54
# File 'lib/subdb/video.rb', line 49

def initialize(path)
  fail "#{path} is not a file" unless File.exists?(path)

  @path = path
  @hash = build_hash
end

Class Attribute Details

.test_modeObject

Returns the value of attribute test_mode.



38
39
40
# File 'lib/subdb/video.rb', line 38

def test_mode
  @test_mode
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



47
48
49
# File 'lib/subdb/video.rb', line 47

def hash
  @hash
end

#pathObject (readonly)

Returns the value of attribute path.



47
48
49
# File 'lib/subdb/video.rb', line 47

def path
  @path
end

Class Method Details

.api_urlObject



40
41
42
# File 'lib/subdb/video.rb', line 40

def api_url
  test_mode ? SANDBOX : API
end

Instance Method Details

#download(languages = ["en"]) ⇒ Object



61
62
63
64
# File 'lib/subdb/video.rb', line 61

def download(languages = ["en"])
  res = request("download", :language => languages.join(","))
  check_get(res)
end

#pathbaseObject



96
97
98
# File 'lib/subdb/video.rb', line 96

def pathbase
  File.basename(path)
end

#searchObject



56
57
58
59
# File 'lib/subdb/video.rb', line 56

def search
  res = request("search")
  check_get(res)
end

#upload(path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/subdb/video.rb', line 66

def upload(path)
  fail "Invalid subtitle file #{path}" unless File.exists?(path)

  params = {:action => "upload", :hash => @hash}

  url = URI.parse(self.class.api_url)

  begin
    file = File.open(path, "rb")

    io = UploadIO.new(file, "application/octet-stream", File.basename(path))

    req               = Net::HTTP::Post::Multipart.new(url.path + stringify_params(params), {"file" => io, "hash" => @hash})
    req["User-Agent"] = user_agent

    res = Net::HTTP.start(url.host, url.port) do |http|
      http.request(req)
    end

    case res.code.to_s
    when "201" then true
    when "403" then false
    when "400" then fail "Malformed request"
    when "415" then fail "Invalid subtitle type"
    end
  ensure
    file.close
  end
end