Class: HypermediaAPI::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/api/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(nokogiri_html_document, status, headers) ⇒ Document

Returns a new instance of Document.



35
36
37
38
39
# File 'lib/api/document.rb', line 35

def initialize (nokogiri_html_document, status, headers)
  @document = nokogiri_html_document
  @status = status.to_i
  @headers = headers
end

Instance Method Details

#entities(klass) ⇒ Object



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

def entities (klass)
  articles = @document.css("article.#{klass.name.underscore}")
  articles.map {|article| klass.new_from_fields(article.css('code')) }
end

#entity(klass) ⇒ Object



4
5
6
7
8
9
# File 'lib/api/document.rb', line 4

def entity (klass)
  article = @document.css("article.#{klass.name.underscore}").first
  return nil unless article
  
  klass.new_from_fields(article.css('code'))
end

#form(css_selector) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/api/document.rb', line 16

def form (css_selector)
  if form_element = @document.css(css_selector).first
    action_uri = form_element['action']
    http_method = form_element['method'].downcase
    
    HypermediaAPI::Form.new(action_uri, http_method)
  else
    raise HypermediaAPI::MissingForm, "The API does not have a form matching '#{css_selector}'."
  end
end

#headersObject



27
28
29
# File 'lib/api/document.rb', line 27

def headers
  @headers
end

#htmlObject



31
32
33
# File 'lib/api/document.rb', line 31

def html
  @document.to_html
end


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

def link (css_selector)
  if a_element = @document.css(css_selector).first
    href_uri = form_element['href']
    Link.new(action_uri, http_method)
  else
    raise MissingForm, "The API does not have a form matching '#{css_selector}'."
  end
end

#statusObject



50
51
52
# File 'lib/api/document.rb', line 50

def status
  @status
end