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.



39
40
41
42
43
# File 'lib/api/document.rb', line 39

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

Instance Method Details

#entities(klass) ⇒ Object



15
16
17
18
# File 'lib/api/document.rb', line 15

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

#entity(klass) ⇒ Object



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

def entity (klass)
  article_element = @document.at_css("article.#{klass.name.underscore}")
  return nil unless article_element
  
  klass.new_from_fields(article_element.element_children.filter('code'))
end

#entity_with_attribute(klass, kv_pairs) ⇒ Object



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

def entity_with_attribute (klass, kv_pairs)
  article_element = @document.at_css("article.#{klass.name.underscore}")
end

#form(css_selector) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/api/document.rb', line 20

def form (css_selector)
  if form_element = @document.at_css(css_selector)
    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



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

def headers
  @headers
end

#htmlObject



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

def html
  @document.to_html
end


45
46
47
48
49
50
51
52
# File 'lib/api/document.rb', line 45

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


54
55
56
57
58
59
# File 'lib/api/document.rb', line 54

def links (css_selector)
  @document.css(css_selector).map do |a_element|
    href_uri = a_element['href']
    Link.new(action_uri, http_method)
  end
end

#statusObject



61
62
63
# File 'lib/api/document.rb', line 61

def status
  @status
end