Class: HTTPAttack::Items::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/httpattack/items/base.rb

Direct Known Subclasses

Github, StatusNet, Twitter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#statsObject (readonly)

Holds the remote response as the Item stats.



9
10
11
# File 'lib/httpattack/items/base.rb', line 9

def stats
  @stats
end

Class Method Details

.descObject



51
52
53
# File 'lib/httpattack/items/base.rb', line 51

def desc
  'A basic item.'
end

.nameObject



47
48
49
# File 'lib/httpattack/items/base.rb', line 47

def name
  self.to_s.split('::').last
end

Instance Method Details

#reload_stats(uri = stats_url) ⇒ Object

Parses the JSON response from the uri (which can be a a path or URL) and returns it as a hash.

On an error (exception), returns a hash like:

{ 'error' => error.message }


29
30
31
32
33
34
35
# File 'lib/httpattack/items/base.rb', line 29

def reload_stats(uri = stats_url)
  begin
    JSON.parse(open(uri).read)
  rescue => error
    {'error' => error.message}
  end
end

#reload_stats!(*args) ⇒ Object

Loads @stats with the JSON response through #reload_stats.



38
39
40
# File 'lib/httpattack/items/base.rb', line 38

def reload_stats!(*args)
  @stats = self.reload_stats(*args)
end

#statObject

This is the strength of the Item and must be overridden in a subclass. It should return an Integer (or Float).

Raises:



14
15
16
# File 'lib/httpattack/items/base.rb', line 14

def stat
  raise MustOverrideMethod
end

#stats_urlObject

This is the URL where the JSON will come from. Must be overridden in a subclassed Item.

Raises:



20
21
22
# File 'lib/httpattack/items/base.rb', line 20

def stats_url
  raise MustOverrideMethod
end