Class: OandaAPI::Client::ResourceDescriptor

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

Overview

Metadata about a resource request.

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.



24
25
26
27
28
29
30
31
32
# File 'lib/oanda_api/client/resource_descriptor.rb', line 24

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

Instance Attribute Details

#collection_nameSymbol (readonly)



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
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15

class ResourceDescriptor
  attr_reader :collection_name, :path, :resource_klass

  # 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|
      resource_name, resource_id = [Utils.singularize(names[:resource_name]), names[:resource_id]]
      self.resource_klass = resource_name
      @is_collection      = method == :get && resource_id.empty?
      @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 = resource_name.capitalize.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)



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
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15

class ResourceDescriptor
  attr_reader :collection_name, :path, :resource_klass

  # 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|
      resource_name, resource_id = [Utils.singularize(names[:resource_name]), names[:resource_id]]
      self.resource_klass = resource_name
      @is_collection      = method == :get && resource_id.empty?
      @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 = resource_name.capitalize.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



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
# File 'lib/oanda_api/client/resource_descriptor.rb', line 15

class ResourceDescriptor
  attr_reader :collection_name, :path, :resource_klass

  # 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|
      resource_name, resource_id = [Utils.singularize(names[:resource_name]), names[:resource_id]]
      self.resource_klass = resource_name
      @is_collection      = method == :get && resource_id.empty?
      @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 = resource_name.capitalize.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.



36
37
38
# File 'lib/oanda_api/client/resource_descriptor.rb', line 36

def is_collection?
  @is_collection
end