class DubDubDub::Client
Attributes
proxy_host[RW]
proxy_password[RW]
proxy_port[RW]
proxy_username[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/dubdubdub/client.rb, line 12 def initialize(options = {}) default_options = { proxy: false } options = default_options.merge(options) # If we want to use a proxy if options[:proxy] # If true, refer to global proxy config if options[:proxy] == true unless DubDubDub.configuration.ignore_proxy? proxy = DubDubDub.configuration.proxy raise ArgumentError, "No proxy has been configured or provided!" if proxy.nil? self.proxy = proxy end # Otherwise, it should be a proxy url else self.proxy = options[:proxy] end end end
Public Instance Methods
browse(url, *args)
click to toggle source
Helper method to browse by using a GET request via Mechanize
# File lib/dubdubdub/client.rb, line 113 def browse(url, *args) handle_mechanize_exceptions do mechanize.get(url, *args) end end
crawl(url, *args)
click to toggle source
Helper method to crawl by using a GET request via RestClient
# File lib/dubdubdub/client.rb, line 106 def crawl(url, *args) response = get(url, *args) Nokogiri::HTML(response.body) end
delete(url, *args)
click to toggle source
Perform a DELETE request
# File lib/dubdubdub/client.rb, line 99 def delete(url, *args) handle_rest_client_exceptions do rest_client_resource(url).delete(*args) end end
follow(url)
click to toggle source
Follow a URL to the end
# File lib/dubdubdub/client.rb, line 120 def follow(url) browse(url).uri.to_s end
get(url, *args)
click to toggle source
Perform a GET request
# File lib/dubdubdub/client.rb, line 85 def get(url, *args) handle_rest_client_exceptions do rest_client_resource(url).get(*args) end end
mechanize()
click to toggle source
Returns a Mechanize instance (agent)
# File lib/dubdubdub/client.rb, line 77 def mechanize agent = Mechanize.new agent.set_proxy(proxy_host, proxy_port) if proxy? agent end
post(url, *args)
click to toggle source
Perform a POST request
# File lib/dubdubdub/client.rb, line 92 def post(url, *args) handle_rest_client_exceptions do rest_client_resource(url).post(*args) end end
proxy()
click to toggle source
# File lib/dubdubdub/client.rb, line 50 def proxy if proxy_host and proxy_port address = "#{proxy_host}:#{proxy_port}" if proxy_username and proxy_password address.prepend("#{proxy_username}:#{proxy_password}@") end end address end
proxy=(url)
click to toggle source
# File lib/dubdubdub/client.rb, line 41 def proxy=(url) matches = url.match(/(?:(?<username>[^\:]+)\:(?<password>[^\@]+)\@)?(?<host>[\.0-9]+|localhost)(?:\:(?<port>[0-9]+))?/) self.proxy_host = matches[:host] self.proxy_port = matches[:port] || 80 self.proxy_username = matches[:username] self.proxy_password = matches[:password] end
proxy?()
click to toggle source
# File lib/dubdubdub/client.rb, line 62 def proxy? return false if DubDubDub.configuration.ignore_proxy? !!proxy end
proxy_port=(port)
click to toggle source
# File lib/dubdubdub/client.rb, line 37 def proxy_port=(port) @proxy_port = port.to_i end
rest_client_resource(url)
click to toggle source
Returns a RestClient::Resource
# File lib/dubdubdub/client.rb, line 69 def rest_client_resource(url) options = {} options[:proxy] = "http://#{proxy}" if proxy? RestClient::Resource.new(url, options) end
Private Instance Methods
handle_mechanize_exceptions() { || ... }
click to toggle source
# File lib/dubdubdub/client.rb, line 137 def handle_mechanize_exceptions(&block) begin handle_net_http_exceptions { yield } rescue Mechanize::RedirectLimitReachedError => e raise DubDubDub::RedirectLimitReachedError rescue Mechanize::ResponseCodeError => e raise DubDubDub::ResponseError.new(e, e.response_code) end end
handle_net_http_exceptions() { || ... }
click to toggle source
# File lib/dubdubdub/client.rb, line 125 def handle_net_http_exceptions(&block) begin yield rescue Timeout::Error, Errno::ETIMEDOUT, Errno::EHOSTUNREACH => e raise DubDubDub::ResponseError.new(e, 408) # Timeout rescue SocketError, EOFError => e raise DubDubDub::ResponseError.new(e, 404) # Not found rescue Errno::ECONNREFUSED => e raise DubDubDub::ResponseError.new(e, 502) # Connection refused end end
handle_rest_client_exceptions() { || ... }
click to toggle source
# File lib/dubdubdub/client.rb, line 147 def handle_rest_client_exceptions(&block) begin handle_net_http_exceptions { yield } rescue RestClient::MaxRedirectsReached => e raise DubDubDub::RedirectLimitReachedError rescue RestClient::RequestTimeout => e raise DubDubDub::ResponseError.new(e, 408) rescue RestClient::Exception => e code = e.response.code if e.response raise DubDubDub::ResponseError.new(e, code) end end