Class: Hyperclient::ResourceFactory

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

Overview

This class acts as an interface to build Resources. It has a simple identity map so a user can save HTTP calls when interacting with the same resource.

Examples:

ResourceFactory.resource('http://myapi.org/resource/1')
=> #<Hyperclient::Resource url: 'http://myapi.org/resource/1'>

Class Method Summary collapse

Class Method Details

.identity_mapObject (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a Hash that acts as identity map.



27
28
29
# File 'lib/hyperclient/resource_factory.rb', line 27

def self.identity_map
  @identity_map ||= {}
end

.resource(url, *args) ⇒ Object

A factory method to build Resources. It will try to find a Resource in the identity map or build a new one if does not exist.

Parameters:

  • url

    A String to identify the Resource

  • args

    An Array to pass other arguments to the Resource initialization.



17
18
19
20
21
22
23
# File 'lib/hyperclient/resource_factory.rb', line 17

def self.resource(url, *args)
  identity_map.fetch(url) do |url|
    resource = Resource.new(url, *args)
    identity_map.update(url => resource)
    resource
  end
end