undertexter 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -10,6 +10,8 @@ PATH
10
10
  GEM
11
11
  remote: http://rubygems.org/
12
12
  specs:
13
+ addressable (2.2.4)
14
+ crack (0.1.8)
13
15
  diff-lcs (1.1.2)
14
16
  hintable_levenshtein (0.0.3)
15
17
  levenshteinish (0.0.1)
@@ -27,6 +29,9 @@ GEM
27
29
  rspec-expectations (2.4.0)
28
30
  diff-lcs (~> 1.1.2)
29
31
  rspec-mocks (2.4.0)
32
+ webmock (1.6.2)
33
+ addressable (>= 2.2.2)
34
+ crack (>= 0.1.7)
30
35
 
31
36
  PLATFORMS
32
37
  ruby
@@ -34,3 +39,4 @@ PLATFORMS
34
39
  DEPENDENCIES
35
40
  rspec (= 2.4.0)
36
41
  undertexter!
42
+ webmock
@@ -0,0 +1,20 @@
1
+ class SourceHasBeenChangedError < StandardError
2
+ def initialize(error, url)
3
+ super <<-END_RUBY
4
+ \nHi,
5
+
6
+ It looks like an error has occurred.
7
+
8
+ Please report it on the Github issue tracker (link below).
9
+ https://github.com/oleander/Undertexter/issues
10
+
11
+ Here is the error:
12
+
13
+ \t #{error.message}
14
+ \t #{error.backtrace.first}
15
+ \t #{url}
16
+
17
+ Thanks for using Undertexter!\n
18
+ END_RUBY
19
+ end
20
+ end
data/lib/undertexter.rb CHANGED
@@ -1,19 +1,20 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require 'rest-client'
4
- require 'subtitle'
5
- require 'nokogiri'
6
- require 'iconv'
7
- require 'undertexter/array'
3
+ require "rest-client"
4
+ require "subtitle"
5
+ require "nokogiri"
6
+ require "iconv"
7
+ require "undertexter/error"
8
+ require "undertexter/array"
8
9
 
9
10
  class Undertexter
10
- attr_accessor :raw_data, :base_details, :subtitles
11
+ attr_accessor :raw_data, :base_details, :subtitles, :search_string
11
12
 
12
13
  def initialize(options)
13
14
  @options = {
14
15
  :language => {
15
- :swedish => 'soek',
16
- :english => 'eng_search'
16
+ :swedish => "soek",
17
+ :english => "eng_search"
17
18
  },
18
19
  :preferred_language => options[:language]
19
20
  }
@@ -27,9 +28,10 @@ class Undertexter
27
28
 
28
29
  def self.find(search_string, options = {:language => :swedish})
29
30
  this = self.new(options)
31
+ this.search_string = search_string
30
32
 
31
33
  # Downloading the page
32
- this.get(search_string)
34
+ this.get!
33
35
 
34
36
  # If something went wrong, like a timeout, {raw_data} could be nil
35
37
  return [] if this.raw_data.nil?
@@ -49,9 +51,9 @@ class Undertexter
49
51
  @block = []
50
52
 
51
53
  tbody = doc.css("table").to_a.reject do |i|
52
- ! i.content.match(/Nedladdningar/i) or i.css('table').any?
54
+ ! i.content.match(/Nedladdningar/i) or i.css("table").any?
53
55
  end.sort_by do |inner|
54
- inner.css('table').count
56
+ inner.css("table").count
55
57
  end.first
56
58
 
57
59
  return if tbody.nil?
@@ -62,15 +64,17 @@ class Undertexter
62
64
  length = @block.length
63
65
  @block[length] = [] if @block[length].nil?
64
66
  line = last.content.split(/\n/).map(&:strip)
65
- value = first.at_css('a')
67
+ value = first.at_css("a")
66
68
 
67
69
  @block[length] << line[1] # (cd 1)
68
70
  @block[length] << line[3] # Nedladdningar: 11891
69
71
  @block[length] << line[4] # "Avatar (2009) PROPER DVDSCR XviD-MAXSPEED"
70
- @block[length] << value.attr('href') # http://www.undertexter.se/?p=undertext&id=19751
71
- @block[length] << value.attr('title') # Avatar
72
+ @block[length] << value.attr("href") # http://www.undertexter.se/?p=undertext&id=19751
73
+ @block[length] << value.attr("title") # Avatar
72
74
  @block[length].map!(&:strip)
73
75
  end
76
+ rescue StandardError => error
77
+ raise SourceHasBeenChangedError.new(error, url)
74
78
  end
75
79
 
76
80
  def build!
@@ -87,8 +91,12 @@ class Undertexter
87
91
  end
88
92
  end
89
93
 
90
- def get(search_string)
91
- @raw_data = RestClient.get(@base_details + CGI.escape(search_string), :timeout => 10) rescue nil
92
- @raw_data = Iconv.conv('utf-8','ISO-8859-1', @raw_data)
94
+ def get!
95
+ @raw_data = RestClient.get(url, :timeout => 10) rescue nil
96
+ @raw_data = Iconv.conv("utf-8","ISO-8859-1", @raw_data)
97
+ end
98
+
99
+ def url
100
+ @base_details + CGI.escape(search_string)
93
101
  end
94
102
  end