dubdubdub 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies to develop your gem here.
4
+ # Include everything needed to run rake, tests, features, etc.
5
+ group :development do
6
+ gem "rspec", "~> 2.8.0"
7
+ gem 'vcr', '~> 2.3.0'
8
+ gem 'fakeweb', '~>1.3.0' # required for vcr
9
+ gem "bundler"
10
+ gem "jeweler", "~> 1.8.4"
11
+ gem 'pry'
12
+ end
@@ -0,0 +1,42 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ coderay (1.0.8)
5
+ diff-lcs (1.1.3)
6
+ fakeweb (1.3.0)
7
+ git (1.2.5)
8
+ jeweler (1.8.4)
9
+ bundler (~> 1.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ rdoc
13
+ json (1.7.5)
14
+ method_source (0.8.1)
15
+ pry (0.9.10)
16
+ coderay (~> 1.0.5)
17
+ method_source (~> 0.8)
18
+ slop (~> 3.3.1)
19
+ rake (10.0.2)
20
+ rdoc (3.12)
21
+ json (~> 1.4)
22
+ rspec (2.8.0)
23
+ rspec-core (~> 2.8.0)
24
+ rspec-expectations (~> 2.8.0)
25
+ rspec-mocks (~> 2.8.0)
26
+ rspec-core (2.8.0)
27
+ rspec-expectations (2.8.0)
28
+ diff-lcs (~> 1.1.2)
29
+ rspec-mocks (2.8.0)
30
+ slop (3.3.3)
31
+ vcr (2.3.0)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler
38
+ fakeweb (~> 1.3.0)
39
+ jeweler (~> 1.8.4)
40
+ pry
41
+ rspec (~> 2.8.0)
42
+ vcr (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 James Hu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ dubdubdub
2
+ =========
3
+
4
+ Networking dubstep.
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ require './lib/dubdubdub'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "dubdubdub"
19
+ gem.homepage = "http://github.com/axsuul/dubdubdub"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Networking dubstep.}
22
+ gem.description = %Q{A library that provides web utility methods with proxification.}
23
+ gem.email = "axsuul@gmail.com"
24
+ gem.authors = ["James Hu"]
25
+ gem.version = DubDubDub::VERSION
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ task :default => :spec
@@ -0,0 +1,77 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "dubdubdub"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Hu"]
12
+ s.date = "2012-12-03"
13
+ s.description = "A library that provides web utility methods with proxification."
14
+ s.email = "axsuul@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".rspec",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "dubdubdub.gemspec",
27
+ "lib/dubdubdub.rb",
28
+ "lib/dubdubdub/client.rb",
29
+ "lib/dubdubdub/exceptions.rb",
30
+ "spec/dubdubdub_spec.rb",
31
+ "spec/spec_helper.rb",
32
+ "spec/support/vcr.rb",
33
+ "spec/vcr/follow_url/alias_link.yml",
34
+ "spec/vcr/follow_url/base.yml",
35
+ "spec/vcr/follow_url/block_base_url.yml",
36
+ "spec/vcr/follow_url/eoferror.yml",
37
+ "spec/vcr/follow_url/https.yml",
38
+ "spec/vcr/follow_url/pass_block.yml",
39
+ "spec/vcr/follow_url/pass_block_iteration.yml",
40
+ "spec/vcr/follow_url/proxied.yml",
41
+ "spec/vcr/follow_url/proxy.yml",
42
+ "spec/vcr/follow_url/proxy_forbidden.yml"
43
+ ]
44
+ s.homepage = "http://github.com/axsuul/dubdubdub"
45
+ s.licenses = ["MIT"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = "1.8.23"
48
+ s.summary = "Networking dubstep."
49
+
50
+ if s.respond_to? :specification_version then
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
55
+ s.add_development_dependency(%q<vcr>, ["~> 2.3.0"])
56
+ s.add_development_dependency(%q<fakeweb>, ["~> 1.3.0"])
57
+ s.add_development_dependency(%q<bundler>, [">= 0"])
58
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
59
+ s.add_development_dependency(%q<pry>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
62
+ s.add_dependency(%q<vcr>, ["~> 2.3.0"])
63
+ s.add_dependency(%q<fakeweb>, ["~> 1.3.0"])
64
+ s.add_dependency(%q<bundler>, [">= 0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
66
+ s.add_dependency(%q<pry>, [">= 0"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
70
+ s.add_dependency(%q<vcr>, ["~> 2.3.0"])
71
+ s.add_dependency(%q<fakeweb>, ["~> 1.3.0"])
72
+ s.add_dependency(%q<bundler>, [">= 0"])
73
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
74
+ s.add_dependency(%q<pry>, [">= 0"])
75
+ end
76
+ end
77
+
@@ -0,0 +1,22 @@
1
+ class DubDubDub
2
+ # Version
3
+ VERSION = "0.0.1"
4
+
5
+ attr_accessor :client
6
+
7
+ def initialize(options = {})
8
+ @client = DubDubDub::Client.new(options)
9
+ end
10
+
11
+ # Redirect methods to client
12
+ def method_missing(method, *args, &block)
13
+ if @client.respond_to?(method)
14
+ @client.send(method, *args, &block)
15
+ else
16
+ send(method, *args, &block)
17
+ end
18
+ end
19
+ end
20
+
21
+ require File.expand_path(File.dirname(__FILE__) + '/dubdubdub/exceptions')
22
+ require File.expand_path(File.dirname(__FILE__) + '/dubdubdub/client')
@@ -0,0 +1,121 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+
4
+ class DubDubDub::Client
5
+ attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_password
6
+
7
+ def initialize(options = {})
8
+ self.proxy = options[:proxy] if options[:proxy]
9
+ end
10
+
11
+ def proxy_port=(port)
12
+ @proxy_port = port.to_i
13
+ end
14
+
15
+ def proxy=(url)
16
+ host, port = url.split(":")
17
+
18
+ port = 80 unless port
19
+ self.proxy_host = host
20
+ self.proxy_port = port
21
+ end
22
+
23
+ def proxy
24
+ "#{proxy_host}:#{proxy_port}"
25
+ end
26
+
27
+ def proxy?
28
+ !!proxy
29
+ end
30
+
31
+ # Returns a Net::HTTP object
32
+ def net_http(uri)
33
+ raise ArgumentError unless uri.is_a? URI::HTTP
34
+
35
+ net_http_class = if proxy?
36
+ Net::HTTP.Proxy(proxy_host, proxy_port, proxy_user, proxy_password)
37
+ else
38
+ Net::HTTP
39
+ end
40
+
41
+ http = net_http_class.new(uri.host, uri.port)
42
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE # ssl certificate doesn't need to be verified, otherwise a OpenSSL::SSL::SSLError might get thrown
43
+ http.use_ssl = true if uri.scheme == "https"
44
+
45
+ http
46
+ end
47
+
48
+ # Follow a url to the end until it can no longer go any further
49
+ # Even if it times out, it will return the url that it times out on!
50
+ def follow_url(url, options = {}, &block)
51
+ default_options = { limit: 20, attempts: 5, timeout: 5 }
52
+ options = default_options.merge(options)
53
+
54
+ at_base = false
55
+ response = nil
56
+ user_agents = [
57
+ 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11',
58
+ ''
59
+ ]
60
+ urls = [] # the url history
61
+
62
+ raise ArgumentError if options[:until] and !options[:until].is_a?(Proc)
63
+
64
+ # before we begin, let's yield the initial url if a block was given
65
+ yield(url) if block_given?
66
+
67
+ options[:limit].downto(1).each do |i|
68
+ begin
69
+ at_base = true if options[:until] and options[:until].call(url)
70
+
71
+ uri = URI.parse(url)
72
+ net_http = net_http(uri)
73
+ at_base = true unless uri.respond_to?(:request_uri) # make sure its a proper url
74
+
75
+ unless at_base
76
+ request = Net::HTTP::Get.new(uri.request_uri)
77
+ request_attempts = 0
78
+
79
+ # we make a certain amount of attempts in case we timeout
80
+ while request_attempts < options[:attempts]
81
+ begin
82
+ request_attempts += 1
83
+
84
+ # Don't let the request take too long
85
+ response = Timeout::timeout(options[:timeout]) do
86
+ net_http.request(request)
87
+ end
88
+
89
+ break # if it reaches this, that means the request was successful do break out!
90
+ # If any of these exceptions are thrown, it has timed out, so keep trying depending on how many attempts we have
91
+ rescue Timeout::Error, Errno::ETIMEDOUT, Errno::EHOSTUNREACH
92
+ # do another attempt if we are allowed one, or stop
93
+ at_base = true and break if request_attempts == options[:attempts]
94
+ rescue SocketError # doesn't exist
95
+ at_base = true and break
96
+ end
97
+ end
98
+
99
+ case response
100
+ when Net::HTTPSuccess then at_base = true
101
+ when Net::HTTPRedirection then url = response['location']
102
+ when Net::HTTPForbidden then raise DubDubDub::Forbidden
103
+ # Couldn't resolve, just return url
104
+ else at_base = true
105
+ end if response
106
+ end
107
+
108
+ # If any of these exceptions get thrown, return the current url
109
+ rescue SocketError, URI::InvalidURIError, EOFError
110
+ at_base = true
111
+ end
112
+
113
+ urls << url
114
+
115
+ break if at_base
116
+ yield(url) if block_given?
117
+ end
118
+
119
+ url
120
+ end
121
+ end
@@ -0,0 +1,2 @@
1
+ class DubDubDub::Exception < RuntimeError; end
2
+ class DubDubDub::Forbidden < DubDubDub::Exception; end
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ describe DubDubDub do
6
+ let(:www) { DubDubDub.new }
7
+
8
+ it "gives the version" do
9
+ DubDubDub::VERSION.should be_a String
10
+ end
11
+
12
+ describe '::new' do
13
+ it "instantiates a new instance with a new client" do
14
+ www = DubDubDub.new
15
+ www.should be_a DubDubDub
16
+ end
17
+
18
+ it "can specify a proxy" do
19
+ www = DubDubDub.new(proxy: "203.131.212.166:3128")
20
+ www.proxy_host.should == "203.131.212.166"
21
+ www.proxy_port.should == 3128
22
+ www.should be_proxy
23
+ end
24
+
25
+ it "uses port 80 by default for proxy" do
26
+ www = DubDubDub.new(proxy: "203.131.212.166")
27
+ www.proxy_host.should == "203.131.212.166"
28
+ www.proxy_port.should == 80
29
+ end
30
+ end
31
+
32
+ describe '#client' do
33
+ it "returns the client" do
34
+ www.client.should be_a DubDubDub::Client
35
+ end
36
+ end
37
+
38
+ describe '#follow_url' do
39
+ it "follows url to the end", vcr: { cassette_name: "follow_url/base", record: :once } do
40
+ www.follow_url("http://say.ly/TCc1CEp").should == "http://www.whosay.com/TomHanks/photos/148406"
41
+ www.follow_url("http://t.co//qbJx26r").should == "http://twitter.com/twitter/status/76360760606986241/photo/1"
42
+ www.follow_url("http://mypict.me/mMgLU").should == "http://mypict.me/mobile.php?id=336583610"
43
+ end
44
+
45
+ it "returns the base url if it meets a passed in block", vcr: { cassette_name: "follow_url/block_base_url", record: :once } do
46
+ www.follow_url("http://ow.ly/9Rp7p", until: lambda { |url| url =~ /ow\.ly/ }).should == "http://ow.ly/9Rp7p"
47
+ www.follow_url("http://ow.ly/9Rp7p", until: lambda { |url| url =~ /bit\.ly/ }).should == "http://bit.ly/GMx5lu"
48
+ www.follow_url("http://ow.ly/9Rp7p", until: lambda { |url| url =~ /bit\.lyyy/ }).should == "http://instagram.com/p/IbhSB6EKRQ/"
49
+ end
50
+
51
+ it "can pass in a block to get the url every step of the way", vcr: { cassette_name: "follow_url/pass_block_iteration", record: :once } do
52
+ urls = []
53
+
54
+ www.follow_url("http://ow.ly/9Rp7p") do |url|
55
+ urls << url
56
+ end
57
+
58
+ urls.first.should == "http://ow.ly/9Rp7p" # first url should be the initial one
59
+ urls.count.should == 4
60
+ end
61
+
62
+ it "can pass in a block with the last url being the base url", vcr: { cassette_name: "follow_url/pass_block", record: :once } do
63
+ urls = []
64
+
65
+ www.follow_url("http://twitpic.com/92a2p5") do |url|
66
+ urls << url
67
+ end
68
+
69
+ urls.count.should == 1
70
+ urls.last.should == "http://twitpic.com/92a2p5"
71
+ end
72
+
73
+ it "handles invalid uris", vcr: { cassette_name: "follow_url/invalid_uris", record: :once } do
74
+ lambda { www.follow_url("http://rank.1new.biz/sharp-紙パック式クリーナー-床用吸い込み口タイプ-オ/") }.should_not raise_error(URI::InvalidURIError)
75
+ www.follow_url("http://rank.1new.biz/sharp-紙パック式クリーナー-床用吸い込み口タイプ-オ/").should == "http://rank.1new.biz/sharp-紙パック式クリーナー-床用吸い込み口タイプ-オ/"
76
+ end
77
+
78
+ it "handles https", vcr: { cassette_name: "follow_url/https", record: :once } do
79
+ lambda { www.follow_url("https://www.youtube.com/watch?v=DM58Zdk7el0&feature=youtube_gdata_player") }.should_not raise_error(EOFError)
80
+ end
81
+
82
+ it "returns the same url if the name or service doesn't exist", vcr: { cassette_name: "follow_url/doesnt_exist", record: :once } do
83
+ www.follow_url("http://cnnsadasdasdasdasdasd.com/asd").should == "http://cnnsadasdasdasdasdasd.com/asd"
84
+ end
85
+
86
+ it "returns actual asset link for an alias link", vcr: { cassette_name: "follow_url/alias_link", record: :once } do
87
+ www.follow_url("http://yfrog.us/evlb0z:medium").should == "http://img535.imageshack.us/img535/9845/lb0.mp4"
88
+ end
89
+
90
+ it "does not raise a EOFError", vcr: { cassette_name: "follow_url/eoferror", record: :once } do
91
+ lambda { www.follow_url("http://www.soulpancake.com/post/1607/whats-your-beautiful-mess.html") }.should_not raise_error
92
+ end
93
+
94
+ it 'works with a proxy', vcr: { cassette_name: "follow_url/proxy", record: :once } do
95
+ www.proxy = "198.154.114.100:8080"
96
+ www.follow_url("http://yfrog.us/evlb0z:medium").should == "http://img535.imageshack.us/img535/9845/lb0.mp4"
97
+ end
98
+
99
+ it "raises forbidden properly on a bad proxy", vcr: { cassette_name: "follow_url/proxy_forbidden", record: :once } do
100
+ www.proxy = "190.202.116.101:3128"
101
+ lambda { www.follow_url("http://yfrog.us/evlb0z:medium").should }.should raise_error(DubDubDub::Forbidden)
102
+ end
103
+ end
104
+ end