Class: Normalic::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/normalic/uri.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = {}) ⇒ URI

Returns a new instance of URI.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/normalic/uri.rb', line 9

def initialize(fields={})
  @scheme = fields[:scheme]
  @user = fields[:user]
  @subdomain = fields[:subdomain]
  @domain = fields[:domain]
  @tld = fields[:tld]
  @port = fields[:port]
  @path = fields[:path]
  @query_hash = fields[:query_hash]
  @fragment = fields[:fragment]
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def domain
  @domain
end

#fragmentObject

Returns the value of attribute fragment.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def fragment
  @fragment
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def path
  @path
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def port
  @port
end

#query_hashObject

Returns the value of attribute query_hash.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def query_hash
  @query_hash
end

#schemeObject

Returns the value of attribute scheme.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def scheme
  @scheme
end

#subdomainObject

Returns the value of attribute subdomain.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def subdomain
  @subdomain
end

#tldObject

Returns the value of attribute tld.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def tld
  @tld
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/normalic/uri.rb', line 5

def user
  @user
end

Class Method Details

.parse(raw) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/normalic/uri.rb', line 21

def self.parse(raw)
  url = raw.to_s.clone

  # parts before the authority, left-to-right
  scheme = url.cut!(/^\w+:\/\//) and scheme.cut!(/:\/\/$/)
  scheme ||= 'http'

  # parts after the authority, right-to-left
  fragment = url.cut!(/#.*$/) and fragment.cut!(/^#/)
  query = url.cut!(/\?.*$/) and query.cut!(/^\?/)
  query_hash = query ? self.parse_query(query) : nil
  path = self.normalize_path(url.cut!(/\/.*$/))

  # parse the authority
  user = url.cut!(/^.+@/) and user.cut!(/@$/)
  port = url.cut!(/:\d+$/) and port.cut!(/^:/)
  tld = url.cut!(/\.\w+$/) and tld.cut!(/^\./)
  domain = url.cut!(/(\.|\A)[^\.]+$/) and domain.cut!(/^\./)
  subdomain = url.empty? ? 'www' : url

  return nil unless tld && domain

  self.new(:scheme => scheme,
           :user => user,
           :subdomain => subdomain,
           :domain => domain,
           :tld => tld,
           :port => port,
           :path => path,
           :query_hash => query_hash,
           :fragment => fragment)
end

Instance Method Details

#==(other) ⇒ Object



98
99
100
# File 'lib/normalic/uri.rb', line 98

def ==(other)
  self.to_s == other.to_s ? true : false
end

#[](field_name) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/normalic/uri.rb', line 82

def [](field_name)
  begin
    self.send(field_name.to_s)
  rescue NoMethodError => e
    nil
  end
end

#[]=(field_name, value) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/normalic/uri.rb', line 90

def []=(field_name, value)
  begin
    self.send("#{field_name}=", value)
  rescue NoMethodError => e
    nil
  end
end

#match_essential?(other) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
# File 'lib/normalic/uri.rb', line 102

def match_essential?(other)
  return false unless tld == other.tld
  return false unless domain == other.domain
  return false unless subdomain == other.subdomain ||
                      (subdomain == 'www' && !other.subdomain) ||
                      (!subdomain && other.subdomain == 'www')
  true
end

#to_sObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/normalic/uri.rb', line 54

def to_s
  scheme_s = scheme ? scheme + '://' : nil
  user_s = user ? user + '@' : nil

  host_s = [subdomain, domain, tld].select do |e|
    e ? true : false
  end.join('.')
  host_s = nil if host_s == ''

  port_s = port ? ':' + port : nil
  path_s = path

  if query_hash
    query_s = '?' + query_hash.to_a.collect do |kv|
      kv[0].to_s + '=' + kv[1].to_s
    end.join('&')
  else
    query_s = nil
  end

  fragment_s = fragment ? '#' + fragment : nil

  [scheme_s, user_s, host_s, port_s,
   path_s, query_s, fragment_s].select do |e|
     e ? true : false
   end.join
end