Class: UniformResourceIdentifier::Relative

Inherits:
Object
  • Object
show all
Extended by:
Parsable
Defined in:
lib/uniform_resource_identifier/relative.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parsable

parse

Constructor Details

#initialize(relative = nil) ⇒ Relative

Returns a new instance of Relative.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/uniform_resource_identifier/relative.rb', line 12

def initialize(relative=nil)
  if relative.respond_to?(:to_str)
    parsed_uri = Parser.parse(relative)
    # TODO
  elsif relative.respond_to?(:to_hash)
    relative = relative.to_hash.symbolize_keys
    
    @path = Path.parse(relative[:path]) if relative.has_key?(:path)
    @query = Query.parse(relative[:query]) if relative.has_key?(:query)
    @anchor = relative[:anchor].to_s.gsub(/^#/, '') if relative.has_key?(:anchor)
  else
    raise(TypeError, "relative must either be a String or a Hash") unless relative.nil?
  end
end

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



66
67
68
# File 'lib/uniform_resource_identifier/relative.rb', line 66

def anchor
  @anchor
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/uniform_resource_identifier/relative.rb', line 42

def blank?
  @path.blank? && @query.blank? && @anchor.blank?
end

#directoryObject

#

Delegates = #

#


76
77
78
# File 'lib/uniform_resource_identifier/relative.rb', line 76

def directory
  self.path.directory
end

#directory=(directory) ⇒ Object



80
81
82
# File 'lib/uniform_resource_identifier/relative.rb', line 80

def directory=(directory)
  self.path.directory = directory
end

#fileObject



84
85
86
# File 'lib/uniform_resource_identifier/relative.rb', line 84

def file
  self.path.file
end

#file=(file) ⇒ Object



88
89
90
# File 'lib/uniform_resource_identifier/relative.rb', line 88

def file=(file)
  self.path.file = file
end

#pathObject

#

Attributes = #

#


50
51
52
# File 'lib/uniform_resource_identifier/relative.rb', line 50

def path
  @path ||= Path.new
end

#path=(path) ⇒ Object



54
55
56
# File 'lib/uniform_resource_identifier/relative.rb', line 54

def path=(path)
  @path = Path.parse(path)
end

#queryObject



58
59
60
# File 'lib/uniform_resource_identifier/relative.rb', line 58

def query
  @query ||= Query.new
end

#query=(query) ⇒ Object



62
63
64
# File 'lib/uniform_resource_identifier/relative.rb', line 62

def query=(query)
  @query = Query.parse(query)
end

#to_hObject



34
35
36
37
38
39
40
# File 'lib/uniform_resource_identifier/relative.rb', line 34

def to_h
  {
    :path   => path.nil? ? nil : @path.to_h,
    :query  => query.nil? ? nil : @query.to_h,
    :anchor => @anchor
  }
end

#to_sObject



27
28
29
30
31
32
# File 'lib/uniform_resource_identifier/relative.rb', line 27

def to_s
  query = "/#{@path}" if @path.blank? || !@path.to_s.starts_with?("/")
  query = "?#{@query}" unless @query.blank?
  anchor = "##{@anchor}" unless @anchor.blank?
  "#{path}#{query}#{anchor}"
end