Class: Heroics::Resource

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

Overview

A resource with methods mapped to API links.

Instance Method Summary collapse

Constructor Details

#initialize(links) ⇒ Resource

Instantiate a resource.

Parameters:

  • links (Hash<String,Link>)

    A hash that maps method names to links.



8
9
10
# File 'lib/heroics/resource.rb', line 8

def initialize(links)
  @links = links
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *parameters) ⇒ String, ... (private)

Find a link and invoke it.

Parameters:

  • name (String)

    The name of the method to invoke.

  • parameters (Array)

    The arguments to pass to the method. This should always be a ‘Hash` mapping parameter names to values.

Returns:

  • (String, Array, Hash)

    The response received from the server. JSON responses are automatically decoded into Ruby objects.

Raises:

  • (NoMethodError)

    Raised if the name doesn’t match a known link.



22
23
24
25
26
27
28
29
# File 'lib/heroics/resource.rb', line 22

def method_missing(name, *parameters)
  link = @links[name.to_s]
  if link.nil?
    address = "<#{self.class.name}:0x00#{(self.object_id << 1).to_s(16)}>"
    raise NoMethodError.new("undefined method `#{name}' for ##{address}")
  end
  link.run(*parameters)
end