Class: HyperAPI::Base

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

Constant Summary collapse

KNOWN_ATTRIBUTE_TYPES =
%w(string integer float)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_string) ⇒ Base

Returns a new instance of Base.



33
34
35
36
# File 'lib/hyper_api.rb', line 33

def initialize(html_string)
  html = ::Nokogiri::HTML(html_string)
  load_attributes(html)
end

Class Method Details

.known_attributesObject



16
17
18
# File 'lib/hyper_api.rb', line 16

def known_attributes
  @known_attributes ||= {}
end

Instance Method Details

#known_attributesObject



55
56
57
# File 'lib/hyper_api.rb', line 55

def known_attributes
  self.class.known_attributes
end

#load_attributes(html) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hyper_api.rb', line 38

def load_attributes(html)
  known_attributes.each do |attr, options|
    type, path, block = options

    unless (value = html.css(path)).empty?
      value = block ? value.instance_eval(&block) : value.text
      value = value.send("to_#{type[0]}")
    end

    instance_variable_set("@#{attr}", value)

    self.class.send :define_method, attr do
      instance_variable_get("@#{attr}")
    end
  end
end