Class: ProxyRb::ProxyUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_rb/proxy_url.rb

Overview

A ProxyURL

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ ProxyUrl

Returns a new instance of ProxyUrl.



45
46
47
# File 'lib/proxy_rb/proxy_url.rb', line 45

def initialize(url)
  @url = url
end

Class Method Details

.build(hash) ⇒ ProxyUrl

Build a URL from hash

@param [Hash] hash
  An Hash representing the url parts

Returns:



15
16
17
# File 'lib/proxy_rb/proxy_url.rb', line 15

def self.build(hash)
  new(Addressable::URI.new(hash))
end

.parse(string) ⇒ ProxyUrl

Create URL from string

Parameters:

  • string (String)

    The url

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/proxy_rb/proxy_url.rb', line 26

def self.parse(string)
  string = string.to_s
  string = if string.empty?
             string
           elsif string.start_with?('http://')
             string
           else
             'http://' + string
           end

  new(Addressable::URI.heuristic_parse(string))
end

Instance Method Details

#empty?Boolean

Check if url is empty

Returns:

  • (Boolean)


64
65
66
# File 'lib/proxy_rb/proxy_url.rb', line 64

def empty?
  url.nil? || url.empty?
end

#to_hashHash

Return url as hash

Returns:

  • (Hash)

    the converted url



86
87
88
89
90
# File 'lib/proxy_rb/proxy_url.rb', line 86

def to_hash
  return {} if empty?

  url.to_hash
end

#to_sObject



57
58
59
60
61
# File 'lib/proxy_rb/proxy_url.rb', line 57

def to_s
  return '' if empty?

  url.to_s
end

#without_user_name_and_passwordProxyUrl

Return URL without user name and password

Returns:



72
73
74
75
76
77
78
79
80
# File 'lib/proxy_rb/proxy_url.rb', line 72

def without_user_name_and_password
  return self.class.new(nil) if empty?

  h = url.to_hash
  h.delete :user
  h.delete :password

  self.class.build(h)
end