Module: HypermediaAPI::Html

Included in:
Document, Entity
Defined in:
lib/api/html.rb

Instance Method Summary collapse

Instance Method Details

#authObject

Returns the HTTP basic auth of the HTML.



5
6
7
# File 'lib/api/html.rb', line 5

def auth
  @http_basic_auth
end

#entities(entity_class) ⇒ Object

Returns an array of every HypermediaAPI::Entity of the given class found in the HTML.



18
19
20
21
22
# File 'lib/api/html.rb', line 18

def entities (entity_class)
  @nhtml.css("article.#{entity_class.name.underscore}").map do |article_element|
    entity_class.new_from_article_element(article_element)
  end.compact
end

#entity(entity_class) ⇒ Object

Returns the first HypermediaAPI::Entity of the given class found in the HTML, or nil if no such entity exists.



11
12
13
14
# File 'lib/api/html.rb', line 11

def entity (entity_class)
  article_element = @nhtml.at_css("article.#{entity_class.name.underscore}")
  entity_class.new_from_article_element(article_element)
end

#form(css_selector) ⇒ Object

Returns a HypermediaAPI::Form representing the first form element that matches the given css_selector. If no such form is found, raises an error.



26
27
28
29
30
31
32
# File 'lib/api/html.rb', line 26

def form (css_selector)
  if form_element = @nhtml.css(css_selector).filter('form').first
    HypermediaAPI::Form.new(form_element['action'], form_element['method'], self.auth)
  else
    raise HypermediaAPI::MissingForm, "There is no form matching '#{css_selector}' in HTML:\n#{self.html}"
  end
end

#htmlObject

Returns the HTML as a string.



35
36
37
# File 'lib/api/html.rb', line 35

def html
  @nhtml.to_html
end

Returns a HypermediaAPI::Link representing the first anchor element that matches the given css_selector. If no such anchor is found, raises an error.



41
42
43
44
45
46
47
# File 'lib/api/html.rb', line 41

def link (css_selector)
  if a_element = @nhtml.css(css_selector).filter('a').first
    HypermediaAPI::Link.new(a_element['href'], self.auth)
  else
    raise HypermediaAPI::MissingLink, "There is no link matching '#{css_selector}' in HTML:\n#{self.html}"
  end
end