Class: PageRankr::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/page_rankr/site.rb

Constant Summary collapse

COMPONENTS =
[:scheme, :subdomain, :domain, :port, :path, :query, :fragment]

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Site

Returns a new instance of Site.



9
10
11
12
13
14
15
16
17
# File 'lib/page_rankr/site.rb', line 9

def initialize(site)
  site = "http://#{site}" unless site =~ /:\/\//
  @uri = Addressable::URI.parse(site)
  @domain = PublicSuffix.parse(@uri.host || "")

  @domain.valid? or raise DomainInvalid, "The domain provided is invalid.1"
rescue PublicSuffix::DomainInvalid, Addressable::URI::InvalidURIError
  raise DomainInvalid, "The domain provided is invalid."
end

Instance Method Details

#domainObject



23
24
25
# File 'lib/page_rankr/site.rb', line 23

def domain
  @domain.domain
end

#fragmentObject



43
44
45
# File 'lib/page_rankr/site.rb', line 43

def fragment
  @uri.fragment
end

#pathObject



35
36
37
# File 'lib/page_rankr/site.rb', line 35

def path
  @uri.path
end

#portObject



31
32
33
# File 'lib/page_rankr/site.rb', line 31

def port
  @uri.port
end

#queryObject



39
40
41
# File 'lib/page_rankr/site.rb', line 39

def query
  @uri.query
end

#schemeObject



19
20
21
# File 'lib/page_rankr/site.rb', line 19

def scheme
  @uri.scheme
end

#subdomainObject



27
28
29
# File 'lib/page_rankr/site.rb', line 27

def subdomain
  @domain.subdomain or domain
end

#url(supported_components = [:domain]) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/page_rankr/site.rb', line 47

def url(supported_components = [:domain])
  components = COMPONENTS & supported_components #get ordered list

  unless components.include?(:subdomain) ^ components.include?(:domain)
    raise SupportedComponentsInvalid, "Either subdomain or domain should be set as a supported component, not both."
  end

  components.inject("") do |url, component|
    url + case component
    when :scheme
      scheme and "#{scheme}://" or ""
    when :domain
      domain
    when :subdomain
      subdomain
    when :port
      port == @uri.default_port and "" or ":#{port}"
    when :path
      path or ""
    when :query
      query and "?#{query}" or ""
    when :fragment
      fragment and "##{fragment}" or ""
    end
  end
end