Class: ContextIO::Lite::URLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/contextio/lite/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.



39
40
41
42
# File 'lib/contextio/lite/url_builder.rb', line 39

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

.uri_encode(param) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/contextio/lite/url_builder.rb', line 85

def self.uri_encode(param)
  if param.is_a? String
    URI.encode param
  else
    param
  end
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.



18
19
20
21
22
23
24
# File 'lib/contextio/lite/url_builder.rb', line 18

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