Class: HypermediaAPI::Entity

Inherits:
Object
  • Object
show all
Includes:
Html
Defined in:
lib/api/entity.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Html

#auth, #entities, #entity, #form, #html, #link

Class Method Details

.authObject

Returns a Hash containing the HTTP Basic Auth :username and :password for this entity class.



14
15
16
# File 'lib/api/entity.rb', line 14

def self.auth
  @auth ||= { username: '', password: '' }
end

.authorize(username, password) ⇒ Object

Sets the HTTP Basic Auth :username and :password for this entity class.



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

def self.authorize (username, password)
  return unless username.present? && password.present?
  @auth = { username: username, password: password }
end

.fields(*field_names) ⇒ Object

Sets the fields for this entity class.



7
8
9
10
# File 'lib/api/entity.rb', line 7

def self.fields (*field_names)
  return (@field_names || []) if field_names.empty?
  @field_names = field_names.map(&:to_sym).each {|field_name| attr_reader field_name }
end

.inherited(subclass) ⇒ Object

Sets the authorization and API root url for subclasses of this entity class.



25
26
27
28
# File 'lib/api/entity.rb', line 25

def self.inherited (subclass)
  subclass.authorize(self.auth[:username], self.auth[:password])
  subclass.root_url(@root_url)
end

.new_from_article_element(article_element) ⇒ Object

Creates a new entity of this entity class using the HTML in article_element.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/api/entity.rb', line 31

def self.new_from_article_element (article_element)
  entity = self.new
  entity.instance_variable_set(:"@nhtml", article_element)
  entity.instance_variable_set(:"@http_basic_auth", self.auth)
  
  if bookmark_a_element = article_element.element_children.filter('a[rel=bookmark]').first
    link = HypermediaAPI::Link.new(bookmark_a_element['href'], self.auth)
    entity.instance_variable_set(:"@bookmark_link", link)
  end
  
  values = article_element.element_children.filter('code').map do |field_element|
    value_str = field_element.content
    name = field_element['data-name']
    
    value = case field_element['data-type']
      when 'date' then value_str.empty? ? nil : Date.parse(value_str)
      when 'integer' then value_str.empty? ? nil : value_str.to_i
      when 'float' then value_str.empty? ? nil : value_str.to_f
      when 'boolean' then value_str.empty? ? nil : value_str != 'false'
      when 'string' then value_str
    end
    
    [name, value]
  end
  
  values.each do |name, value|
    next unless entity.respond_to?(name)
    entity.instance_variable_set(:"@#{name}", value)
  end
  
  entity
end

.query(query_name, form_values) ⇒ Object

Submits a query using the named query form found in the API root document of this entity class, then returns a Hypermedia::Document representing the response.



67
68
69
70
# File 'lib/api/entity.rb', line 67

def self.query (query_name, form_values)
  query_form = self.root_doc.form("##{query_name.to_s.dasherize}")
  query_form.submit(form_values)
end

.root_docObject

Returns the API root document for this entity class.



73
74
75
# File 'lib/api/entity.rb', line 73

def self.root_doc
  @root_doc ||= HypermediaAPI.get(@root_url, auth: self.auth)
end

.root_url(url) ⇒ Object

Sets the API root url for this entity class.



78
79
80
# File 'lib/api/entity.rb', line 78

def self.root_url (url)
  @root_url = url
end

Instance Method Details

Returns a HypermediaAPI::Link pointing to the bookmark uri of the entity.



83
84
85
# File 'lib/api/entity.rb', line 83

def bookmark_link
  @bookmark_link
end

#reload!Object

Reloads the HypermediaAPI::Entity from the uri in the bookmark link.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/api/entity.rb', line 88

def reload!
  response = self.bookmark_link.click
  
  if response.status == 200
    entity = response.entity(self.class)
    
    self.instance_variable_set(:"@nhtml", entity.instance_variable_get(:"@nhtml"))
    self.instance_variable_set(:"@bookmark_link", entity.instance_variable_get(:"@bookmark_link"))
    
    self.class.fields.each do |field_name|
      self.instance_variable_set(:"@#{field_name}", entity.send(field_name))
    end
  end
  
  self
end