Class: ContextIO::API::URLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/contextio/api/url_builder.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.register_url(resource_class, &block) ⇒ Object

Register a block that calculates the URL for a given resource.

Examples:

For Accounts

register_url ContextIO::Account do ||
  "accounts/#{.id}"
end

Parameters:

  • resource_class (Class)

    The class of the resource you are registering.

  • block (Block)

    The code that will compute the url for the resource. This is actually a path. Start after the version number of the API in the URL. When a URL is being calculated for a specific resource, the resource instance will be yielded to the block.



47
48
49
50
# File 'lib/contextio/api/url_builder.rb', line 47

def self.register_url(resource_class, &block)
  @registered_urls ||= {}
  @registered_urls[resource_class] = block
end

.url_for(resource) ⇒ String

Tells you the right URL for a resource to fetch attributes from.

Parameters:

  • resource (ContextIO::Resource, ContextIO::ResourceCollection)

    The resource or resource collection.

Returns:

  • (String)

    The path for that resource in the API.



26
27
28
29
30
31
32
# File 'lib/contextio/api/url_builder.rb', line 26

def self.url_for(resource)
  if (builder = @registered_urls[resource.class])
    builder.call(resource)
  else
    raise Error, "URL could not be built for unregistered Class: #{resource.class}."
  end
end