Skip to content

Commit

Permalink
Modify proxy handling to have a better test coverage (rake rcov)
Browse files Browse the repository at this point in the history
  • Loading branch information
nledez committed Sep 1, 2010
1 parent aece5c6 commit bf94077
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
69 changes: 31 additions & 38 deletions lib/cine_passion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,39 @@
require 'rexml/document'
include REXML

begin
require 'cine_passion_config'
rescue LoadError => load_error
# Define default variables
SITEURL="http://scraper-cine-passion-demo.ledez.net"
APIKEY="fake-7945cb-fake"

puts '*'*50
puts File.join(File.dirname(__FILE__), 'cine_passion_config.rb') + " is missing"
puts " Please see README to create it"
puts "currently I use theres values :"
puts "SITEURL: #{SITEURL}"
puts "APIKEY; #{APIKEY}"
puts '*'*50
end

if not (defined? SITEURL || (not SITEURL.nil?))
raise 'Need to define SITEURL'
end

if not (defined? APIKEY || (not APIKEY.nil?))
raise 'Need to define APIKEY'
end

class CinePassion
attr_reader :xml_data, :movies_info, :result_nb, :status, :quota
attr_reader :xml_data, :movies_info, :result_nb, :status, :quota, :apikey, :siteurl, :proxyinfo

VERSION = '0.7.0'

# This class does not require parameters
# First action is reset object
def initialize
def initialize(apikey=nil, proxy=nil)
if apikey.nil?
@apikey = "fake-7945cb-fake"
@siteurl="http://scraper-cine-passion-demo.ledez.net"
puts '*'*50
puts "I need a apikey to get real values"
puts " Please see README to create it"
puts "currently I use theres values :"
puts "@siteurl: #{@siteurl}"
puts "@apikey; #{@apikey}"
puts '*'*50
else
@apikey = apikey
@siteurl="http://passion-xbmc.org"
end

@proxy_host = @proxy_port = @proxy_user = @proxy_password = nil
if (ENV['http_proxy'] || proxy)
uri=URI.parse(ENV['http_proxy']) if proxy.nil?
uri=URI.parse(proxy) if ENV['http_proxy'].nil?
@proxy_host = uri.host
@proxy_port = uri.port
@proxy_user, @proxy_password = uri.userinfo.split(/:/) if uri.userinfo
end
@proxyinfo = [@proxy_host, @proxy_port, @proxy_user, @proxy_password]

self.DataReset()
end

Expand All @@ -60,19 +61,12 @@ def DataReset()
# Load XML data from online Cine Passion Scraper
# Put movie name in parameter
def DataLoadFromSite(search)
proxy_host = proxy_port = proxy_user = proxy_password = nil
if (ENV['http_proxy'])
uri=URI.parse(ENV['http_proxy'])
proxy_host = uri.host
proxy_port = uri.port
proxy_user,proxy_password = uri.userinfo.split(/:/) if uri.userinfo
end
conn = Net::HTTP::Proxy(proxy_host,proxy_port, proxy_user, proxy_password)
conn = Net::HTTP::Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_password)

query="Title" #|IMDB"
lang="fr" # / en"
format="XML"
api_url="#{SITEURL}/scraper/API/1/Movie.Search/#{query}/#{lang}/#{format}/#{APIKEY}/#{search}"
api_url="#{@siteurl}/scraper/API/1/Movie.Search/#{query}/#{lang}/#{format}/#{@apikey}/#{search}"

url = URI.parse(URI.escape(api_url))
res = conn.start(url.host, url.port) {|http|
Expand Down Expand Up @@ -124,7 +118,7 @@ def ScrapAnalyse()
@status = 2
end

quota = root.elements['quota']
quota = root.elements['quota']
if not quota.nil?
@quota['authorize'] = quota.attributes['authorize']
@quota['use'] = quota.attributes['use']
Expand Down Expand Up @@ -204,7 +198,7 @@ def ScrapAnalyseOneMovie(oneMovieXML)
movie_info['ratings']['imdb']['votes'] = ratings_imdb.attributes['votes']
movie_info['ratings']['imdb']['value'] = ratings_imdb.text

nfo_base = "#{SITEURL}/scraper/index.php?id=#{movie_info['id']}&Download=1"
nfo_base = "#{@siteurl}/scraper/index.php?id=#{movie_info['id']}&Download=1"
movie_info['nfo'] = {}
movie_info['nfo']['Babylon'] = {}
movie_info['nfo']['Camelot'] = {}
Expand All @@ -218,7 +212,6 @@ def ScrapAnalyseOneMovie(oneMovieXML)
return movie_info
end


# Scrap get a filename with garbage information & clean it
def Scrap(search)
short_name = search.gsub(/\./, ' ')
Expand Down
20 changes: 19 additions & 1 deletion test/test_cine_passion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ def test_create_object
assert_instance_of(CinePassion, @test)
end

# Test ability to define a apikey
def test_can_have_apikey
@test = CinePassion.new("test-api-key")
assert_equal(@test.apikey, "test-api-key")
end

# Test ability to define a proxy
def test_can_have_apikey
@test1 = CinePassion.new("test-api-key")
assert_equal(@test1.proxyinfo, [nil, nil, nil, nil])

@test2 = CinePassion.new("test-api-key", "http://127.0.0.1:3128/")
assert_equal(@test2.proxyinfo, ["127.0.0.1", 3128, nil, nil])

@test3 = CinePassion.new("test-api-key", "http://user:pass@127.0.0.1:3128/")
assert_equal(@test3.proxyinfo, ["127.0.0.1", 3128, "user", "pass"])
end

# Test ability to load data from xml & reset data
def test_data_load_and_reset
@test = CinePassion.new
Expand Down Expand Up @@ -95,7 +113,7 @@ def test_xml_load_data
assert_equal(@test.quota['authorize'], "300")
assert_equal(@test.quota['use'], "1")
assert_equal(@test.quota['reset_date'], "2010-08-04 12:45:26")

end

def test_xml_load_data_multiple_movies
Expand Down

0 comments on commit bf94077

Please sign in to comment.