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: 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

{
    alltransactions: { resource_name: "transaction_history", is_collection: false},
    calendar:        { resource_name: "calendar_event",      is_collection: true},
    calendar_events: { resource_name: "calendar_event",      is_collection: true,  api_resource_name: "calendar"},
    spreads:         { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"},
    spread_historys: { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"}
}

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:



39
40
41
42
43
44
# 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

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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: 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
  RESOURCES_MAPPER = {
      alltransactions: { resource_name: "transaction_history", is_collection: false},
      calendar:        { resource_name: "calendar_event",      is_collection: true},
      calendar_events: { resource_name: "calendar_event",      is_collection: true,  api_resource_name: "calendar"},
      spreads:         { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"},
      spread_historys: { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"}
  }

  # 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|
      initialize_from_resource_name(names[:resource_name], method, names[:resource_id])
    end
  end

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

  # 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.
  def labs?
    OandaAPI::ResourceBase.labs_resource? resource_klass
  end

  private

  # The resource type
  # @param [String] resource_name
  # @return [void]
  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

  # 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]
  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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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: 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
  RESOURCES_MAPPER = {
      alltransactions: { resource_name: "transaction_history", is_collection: false},
      calendar:        { resource_name: "calendar_event",      is_collection: true},
      calendar_events: { resource_name: "calendar_event",      is_collection: true,  api_resource_name: "calendar"},
      spreads:         { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"},
      spread_historys: { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"}
  }

  # 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|
      initialize_from_resource_name(names[:resource_name], method, names[:resource_id])
    end
  end

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

  # 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.
  def labs?
    OandaAPI::ResourceBase.labs_resource? resource_klass
  end

  private

  # The resource type
  # @param [String] resource_name
  # @return [void]
  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

  # 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]
  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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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: 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
  RESOURCES_MAPPER = {
      alltransactions: { resource_name: "transaction_history", is_collection: false},
      calendar:        { resource_name: "calendar_event",      is_collection: true},
      calendar_events: { resource_name: "calendar_event",      is_collection: true,  api_resource_name: "calendar"},
      spreads:         { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"},
      spread_historys: { resource_name: "spread_history",      is_collection: false, api_resource_name: "spreads"}
  }

  # 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|
      initialize_from_resource_name(names[:resource_name], method, names[:resource_id])
    end
  end

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

  # 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.
  def labs?
    OandaAPI::ResourceBase.labs_resource? resource_klass
  end

  private

  # The resource type
  # @param [String] resource_name
  # @return [void]
  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

  # 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]
  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
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

#labs?Boolean

True if the resource represented by the path is one found in the "Labs" resources in the API. See Forex Labs for details on Labs resources.

Returns:

  • (Boolean)


56
57
58
# File 'lib/oanda_api/client/resource_descriptor.rb', line 56

def labs?
  OandaAPI::ResourceBase.labs_resource? resource_klass
end