Class: OandaAPI::Client::ResourceDescriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/oanda_api/client/resource_descriptor.rb

Overview

Metadata about a resource request.

Constant Summary collapse

RESOURCES_MAPPER =

Mapper for not "typical" resources. Key is a resource from the API path. Value is a hash that can contain "resource_name" from the code and/or "is_collection" (if true: will force treating response as a collection of resources, if false: will force treating response as a single resource).

{
    alltransactions: { resource_name: 'transaction_history', is_collection: false }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, method) ⇒ ResourceDescriptor

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

Parameters:


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oanda_api/client/resource_descriptor.rb', line 33

def initialize(path, method)
  @path = path
  path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names|
    mapped_resource = RESOURCES_MAPPER[names[:resource_name].to_sym]
    resource_name   = (mapped_resource||{})[:resource_name] || Utils.singularize(names[:resource_name])
    resource_id     = names[:resource_id]
    self.resource_klass = resource_name
    @is_collection      = (mapped_resource||{})[:is_collection]
    @is_collection      = method == :get && resource_id.empty?  if @is_collection.nil?
    @collection_name    = Utils.pluralize(resource_name).to_sym if is_collection?
  end
end

Instance Attribute Details

#collection_nameSymbol (readonly)

Returns method name that returns a collection of the resource from the API response.

Returns:

  • (Symbol)

    method name that returns a collection of the resource from the API response.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15

class ResourceDescriptor
  attr_reader :collection_name, :path, :resource_klass

  # Mapper for not "typical" resources.
  #   Key is a resource from the API path.
  #   Value is a hash that can contain "resource_name" from the code and/or
  #   "is_collection" (if true: will force treating response as a collection of resources,
  #   if false: will force treating response as a single resource).
  RESOURCES_MAPPER = {
      alltransactions: { resource_name: 'transaction_history', is_collection: false }
  }

  # 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}).
  def initialize(path, method)
    @path = path
    path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names|
      mapped_resource = RESOURCES_MAPPER[names[:resource_name].to_sym]
      resource_name   = (mapped_resource||{})[:resource_name] || Utils.singularize(names[:resource_name])
      resource_id     = names[:resource_id]
      self.resource_klass = resource_name
      @is_collection      = (mapped_resource||{})[:is_collection]
      @is_collection      = method == :get && resource_id.empty?  if @is_collection.nil?
      @collection_name    = Utils.pluralize(resource_name).to_sym if is_collection?
    end
  end

  # True if the request returns a collection.
  # @return [Boolean]
  def is_collection?
    @is_collection
  end

  private

  # The resource type
  # @param [String] resource_name
  # @return [void]
  def resource_klass=(resource_name)
    klass_symbol = OandaAPI::Utils.classify(resource_name).to_sym
    fail ArgumentError, "Invalid resource" unless OandaAPI::Resource.constants.include?(klass_symbol)
    @resource_klass = OandaAPI::Resource.const_get klass_symbol
  end
end

#pathString (readonly)

Returns path of the resource URI.

Returns:

  • (String)

    path of the resource URI.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15

class ResourceDescriptor
  attr_reader :collection_name, :path, :resource_klass

  # Mapper for not "typical" resources.
  #   Key is a resource from the API path.
  #   Value is a hash that can contain "resource_name" from the code and/or
  #   "is_collection" (if true: will force treating response as a collection of resources,
  #   if false: will force treating response as a single resource).
  RESOURCES_MAPPER = {
      alltransactions: { resource_name: 'transaction_history', is_collection: false }
  }

  # 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}).
  def initialize(path, method)
    @path = path
    path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names|
      mapped_resource = RESOURCES_MAPPER[names[:resource_name].to_sym]
      resource_name   = (mapped_resource||{})[:resource_name] || Utils.singularize(names[:resource_name])
      resource_id     = names[:resource_id]
      self.resource_klass = resource_name
      @is_collection      = (mapped_resource||{})[:is_collection]
      @is_collection      = method == :get && resource_id.empty?  if @is_collection.nil?
      @collection_name    = Utils.pluralize(resource_name).to_sym if is_collection?
    end
  end

  # True if the request returns a collection.
  # @return [Boolean]
  def is_collection?
    @is_collection
  end

  private

  # The resource type
  # @param [String] resource_name
  # @return [void]
  def resource_klass=(resource_name)
    klass_symbol = OandaAPI::Utils.classify(resource_name).to_sym
    fail ArgumentError, "Invalid resource" unless OandaAPI::Resource.constants.include?(klass_symbol)
    @resource_klass = OandaAPI::Resource.const_get klass_symbol
  end
end

#resource_klassSymbol

Returns class of the resource.

Returns:

  • (Symbol)

    class of the resource.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15

class ResourceDescriptor
  attr_reader :collection_name, :path, :resource_klass

  # Mapper for not "typical" resources.
  #   Key is a resource from the API path.
  #   Value is a hash that can contain "resource_name" from the code and/or
  #   "is_collection" (if true: will force treating response as a collection of resources,
  #   if false: will force treating response as a single resource).
  RESOURCES_MAPPER = {
      alltransactions: { resource_name: 'transaction_history', is_collection: false }
  }

  # 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}).
  def initialize(path, method)
    @path = path
    path.match(/\/(?<resource_name>[a-z]*)\/?(?<resource_id>\w*?)$/) do |names|
      mapped_resource = RESOURCES_MAPPER[names[:resource_name].to_sym]
      resource_name   = (mapped_resource||{})[:resource_name] || Utils.singularize(names[:resource_name])
      resource_id     = names[:resource_id]
      self.resource_klass = resource_name
      @is_collection      = (mapped_resource||{})[:is_collection]
      @is_collection      = method == :get && resource_id.empty?  if @is_collection.nil?
      @collection_name    = Utils.pluralize(resource_name).to_sym if is_collection?
    end
  end

  # True if the request returns a collection.
  # @return [Boolean]
  def is_collection?
    @is_collection
  end

  private

  # The resource type
  # @param [String] resource_name
  # @return [void]
  def resource_klass=(resource_name)
    klass_symbol = OandaAPI::Utils.classify(resource_name).to_sym
    fail ArgumentError, "Invalid resource" unless OandaAPI::Resource.constants.include?(klass_symbol)
    @resource_klass = OandaAPI::Resource.const_get klass_symbol
  end
end

Instance Method Details

#is_collection?Boolean

True if the request returns a collection.

Returns:

  • (Boolean)

48
49
50
# File 'lib/oanda_api/client/resource_descriptor.rb', line 48

def is_collection?
  @is_collection
end