dubdubdub 0.2.0 → 0.2.2

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/dubdubdub.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "dubdubdub"
8
- s.version = "0.2.0"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["James Hu"]
12
- s.date = "2012-12-03"
12
+ s.date = "2012-12-04"
13
13
  s.description = "A library that provides web utility methods with proxification."
14
14
  s.email = "axsuul@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "dubdubdub.gemspec",
27
27
  "lib/dubdubdub.rb",
28
28
  "lib/dubdubdub/client.rb",
29
+ "lib/dubdubdub/configuration.rb",
29
30
  "lib/dubdubdub/exceptions.rb",
30
31
  "spec/dubdubdub_spec.rb",
31
32
  "spec/spec_helper.rb",
data/lib/dubdubdub.rb CHANGED
@@ -1,9 +1,27 @@
1
1
  class DubDubDub
2
2
  # Version
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.2"
4
4
 
5
5
  attr_accessor :client
6
6
 
7
+ class << self
8
+ attr_accessor :proxies
9
+ end
10
+
11
+ def self.configure
12
+ yield(configuration)
13
+ end
14
+
15
+ # Returns DubDubDub::Configuration or instantiates if doesn't exit
16
+ def self.configuration
17
+ @configuration ||= DubDubDub::Configuration.new
18
+ end
19
+
20
+ # Reset configuration back to defaults, useful for testing
21
+ def self.reset_configuration!
22
+ @configuration = nil
23
+ end
24
+
7
25
  def initialize(options = {})
8
26
  @client = DubDubDub::Client.new(options)
9
27
  end
@@ -19,4 +37,5 @@ class DubDubDub
19
37
  end
20
38
 
21
39
  require File.expand_path(File.dirname(__FILE__) + '/dubdubdub/exceptions')
40
+ require File.expand_path(File.dirname(__FILE__) + '/dubdubdub/configuration')
22
41
  require File.expand_path(File.dirname(__FILE__) + '/dubdubdub/client')
@@ -6,7 +6,27 @@ class DubDubDub::Client
6
6
  attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_password
7
7
 
8
8
  def initialize(options = {})
9
- self.proxy = options[:proxy] if options[:proxy]
9
+ default_options = {
10
+ proxy: false
11
+ }
12
+
13
+ options = default_options.merge(options)
14
+
15
+ # If we want to use a proxy
16
+ if options[:proxy]
17
+ # If true and we have a proxy list, use a random one from the list
18
+ # or ignore and don't use a proxy at all
19
+ if options[:proxy] == true
20
+ if DubDubDub.proxies and DubDubDub.proxies.is_a?(Array) and DubDubDub.proxies.any?
21
+ self.proxy = DubDubDub.proxies.sample
22
+ else
23
+ raise DubDubDub::Exception, "No proxies have been specified!"
24
+ end
25
+ # Otherwise, it should be a proxy url
26
+ else
27
+ self.proxy = options[:proxy]
28
+ end
29
+ end
10
30
  end
11
31
 
12
32
  def proxy_port=(port)
@@ -26,6 +46,8 @@ class DubDubDub::Client
26
46
  end
27
47
 
28
48
  def proxy?
49
+ return false if DubDubDub.configuration.ignore_proxies
50
+
29
51
  !!proxy
30
52
  end
31
53
 
@@ -0,0 +1,9 @@
1
+ class DubDubDub::Configuration
2
+ # Ignores all attempts to use proxies
3
+ attr_accessor :ignore_proxies
4
+
5
+ def initialize
6
+ # Default config values
7
+ self.ignore_proxies = false
8
+ end
9
+ end
@@ -31,12 +31,75 @@ describe DubDubDub do
31
31
  www.proxy_port.should == 80
32
32
  end
33
33
 
34
+ it "can specify to use a random proxy from list of proxies set" do
35
+ proxies = ["localhost:8000", "localhost:4000", "12.12.12.12:3000"]
36
+ DubDubDub.proxies = proxies
37
+
38
+ www = DubDubDub.new(proxy: true)
39
+ www.should be_proxy
40
+
41
+ proxies.include?(www.proxy).should be_true
42
+ end
43
+
44
+ it "raises an error if we have specified to use a proxy from the list but there are none" do
45
+ DubDubDub.proxies = []
46
+ lambda { DubDubDub.new(proxy: true) }.should raise_error(DubDubDub::Exception)
47
+
48
+ DubDubDub.proxies = nil
49
+ lambda { DubDubDub.new(proxy: true) }.should raise_error(DubDubDub::Exception)
50
+ end
51
+
34
52
  it "does not pass the method to client if that method doesn't exist within the client" do
35
53
  www = DubDubDub.new
36
54
  lambda { www.some_method_that_doesnt_exist }.should raise_error(NameError)
37
55
  end
38
56
  end
39
57
 
58
+ describe '::configuration' do
59
+ it "accesses config object" do
60
+ DubDubDub.configuration.should be_a DubDubDub::Configuration
61
+ end
62
+
63
+ it "has default config values" do
64
+ DubDubDub.configuration.ignore_proxies.should be_false
65
+ end
66
+ end
67
+
68
+ describe '::reset_configuration!' do
69
+ it "resets configuration by to defaults" do
70
+ DubDubDub.configure do |config|
71
+ config.ignore_proxies = true
72
+ end
73
+
74
+ DubDubDub.configuration.ignore_proxies.should be_true
75
+
76
+ DubDubDub.reset_configuration!
77
+
78
+ DubDubDub.configuration.ignore_proxies.should be_false
79
+ end
80
+ end
81
+
82
+ describe '::configure' do
83
+ it "sets config attributes" do
84
+ DubDubDub.configure do |config|
85
+ config.ignore_proxies = true
86
+ end
87
+
88
+ DubDubDub.configuration.ignore_proxies.should be_true
89
+ end
90
+ end
91
+
92
+ describe '::proxies' do
93
+ it "is nil by default" do
94
+ DubDubDub.proxies.should be_nil
95
+ end
96
+
97
+ it "can be set" do
98
+ DubDubDub.proxies = ["localhost:8000"]
99
+ DubDubDub.proxies.should be == ["localhost:8000"]
100
+ end
101
+ end
102
+
40
103
  describe '#client' do
41
104
  it "returns the client" do
42
105
  www.client.should be_a DubDubDub::Client
@@ -54,6 +117,15 @@ describe DubDubDub do
54
117
  agent.proxy_addr.should == "localhost"
55
118
  agent.proxy_port.should == 8000
56
119
  end
120
+
121
+ it "ignores proxy if configured" do
122
+ DubDubDub.configuration.ignore_proxies = true
123
+
124
+ www.proxy = "localhost:8000"
125
+ agent = www.mechanize
126
+ agent.proxy_addr.should be_nil
127
+ agent.proxy_port.should be_nil
128
+ end
57
129
  end
58
130
 
59
131
  describe '#get' do
data/spec/spec_helper.rb CHANGED
@@ -13,4 +13,8 @@ RSpec.configure do |config|
13
13
  config.treat_symbols_as_metadata_keys_with_true_values = true
14
14
  config.filter_run focus: true
15
15
  config.run_all_when_everything_filtered = true
16
+
17
+ config.after(:each) do
18
+ DubDubDub.reset_configuration!
19
+ end
16
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dubdubdub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-03 00:00:00.000000000 Z
12
+ date: 2012-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -172,6 +172,7 @@ files:
172
172
  - dubdubdub.gemspec
173
173
  - lib/dubdubdub.rb
174
174
  - lib/dubdubdub/client.rb
175
+ - lib/dubdubdub/configuration.rb
175
176
  - lib/dubdubdub/exceptions.rb
176
177
  - spec/dubdubdub_spec.rb
177
178
  - spec/spec_helper.rb
@@ -206,7 +207,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
206
207
  version: '0'
207
208
  segments:
208
209
  - 0
209
- hash: -1273812053448365973
210
+ hash: -2077159284295139736
210
211
  required_rubygems_version: !ruby/object:Gem::Requirement
211
212
  none: false
212
213
  requirements: