Class: HyperNavigator::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/hyper_navigator/node.rb

Constant Summary collapse

IGNORE_REFS =
["self", "up", "next", "prev"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rel, href, headers = {}, depth = nil) ⇒ Node

Returns a new instance of Node.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hyper_navigator/node.rb', line 96

def initialize(rel, href, headers={}, depth=nil)
  @rel = rel
  @href = href
  @headers = headers
  @descendants = []
  @depth = depth
  if href
    @response = HyperNavigator.get(href, headers)
    raise RuntimeError, @response unless @response.code =~ /^2..$/
  end
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



94
95
96
# File 'lib/hyper_navigator/node.rb', line 94

def depth
  @depth
end

#descendantsObject

Returns the value of attribute descendants.



94
95
96
# File 'lib/hyper_navigator/node.rb', line 94

def descendants
  @descendants
end

#hrefObject (readonly)

Returns the value of attribute href.



93
94
95
# File 'lib/hyper_navigator/node.rb', line 93

def href
  @href
end

#relObject (readonly)

Returns the value of attribute rel.



93
94
95
# File 'lib/hyper_navigator/node.rb', line 93

def rel
  @rel
end

#responseObject (readonly)

Returns the value of attribute response.



93
94
95
# File 'lib/hyper_navigator/node.rb', line 93

def response
  @response
end

Instance Method Details

#flatten_branchObject



118
119
120
# File 'lib/hyper_navigator/node.rb', line 118

def flatten_branch
  descendants + descendants.flat_map { | d| d.flatten_branch }
end


108
109
110
111
112
113
114
115
116
# File 'lib/hyper_navigator/node.rb', line 108

def links
  @cached_links ||= begin
    json = JSON.parse(@response.body) rescue nil
    return [] unless json
    json["links"].reject do |i|
      IGNORE_REFS.any? { |r| r == i["rel"] }
    end
  end
end