class OandaAPI::Client::ResourceDescriptor

@private Metadata about a resource request.

@!attribute [r] collection_name

@return [Symbol] method name that returns a collection of the resource
  from the API response.

@!attribute [r] path

@return [String] path of the resource URI.

@!attribute [r] resource_klass

@return [Symbol] class of the resource.

Constants

RESOURCES_MAPPER

Mapper for not “typical” resources.

Key is a resource from the API path.
Value is a hash that can contain: 1) "resource_name" which is the OandaAPI ruby resource name and/or
2) "is_collection" (if true: response treated as a collection,
false: response treated as a singular resource) and/or
3) "api_resource_name" the actual API resource name

Attributes

collection_name[R]
path[R]
resource_klass[R]

Public Class Methods

new(path, method) click to toggle source

Analyzes the resource request and determines the type of resource expected from the API.

@param [String] path a path to a resource.

@param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}).

# File lib/oanda_api/client/resource_descriptor.rb, line 39
def initialize(path, method)
  @path = path
  path.match(/\/(?<resource_name>[a-z_]*)\/?(?<resource_id>\w*?)$/) do |names|
    initialize_from_resource_name(names[:resource_name], method, names[:resource_id])
  end
end

Public Instance Methods

is_collection?() click to toggle source

True if the request returns a collection. @return [Boolean]

# File lib/oanda_api/client/resource_descriptor.rb, line 48
def is_collection?
  @is_collection
end
labs?() click to toggle source

True if the resource represented by the path is one found in the “Labs”

resources in the API.
See {http://developer.oanda.com/rest-live/forex-labs/ Forex Labs} for
details on Labs resources.
# File lib/oanda_api/client/resource_descriptor.rb, line 56
def labs?
  OandaAPI::ResourceBase.labs_resource? resource_klass
end

Private Instance Methods

initialize_from_resource_name(resource_name, method, resource_id) click to toggle source

Will set instance attributes based on resource_name, method and resource_id.

@param [String] resource_name name of the resource (from the path). @param [Symbol] method an http verb (see {OandaAPI::Client.map_method_to_http_verb}). @param [Symbol] resource_id id of the resource. @return [void]

# File lib/oanda_api/client/resource_descriptor.rb, line 76
def initialize_from_resource_name(resource_name, method, resource_id)
  mapped_resource = RESOURCES_MAPPER.fetch(resource_name.to_sym,
                                           { resource_name: Utils.singularize(resource_name) })
  self.resource_klass = mapped_resource.fetch :resource_name
  @is_collection      = mapped_resource.fetch :is_collection, method == :get && resource_id.empty?
  @collection_name    = ResourceBase.pluralize(mapped_resource.fetch(:resource_name)).to_sym if is_collection?

  # If resource is using an alias name, replace it with its real API resource name.
  @path.sub!(/\w*$/, mapped_resource[:api_resource_name]) if mapped_resource[:api_resource_name]
end
resource_klass=(resource_name) click to toggle source

The resource type @param [String] resource_name @return [void]

# File lib/oanda_api/client/resource_descriptor.rb, line 65
def resource_klass=(resource_name)
  @resource_klass = OandaAPI::ResourceBase.class_from_symbol resource_name.to_sym
  fail ArgumentError, "Invalid resource: #{resource_name}" unless @resource_klass
end