Class: OandaAPI::ResourceCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/oanda_api/resource_collection.rb

Overview

A collection of a specific resource. Returned by API requests that return collections. See the Oanda Development Guide for documentation about resource attributes expected for specific requests.

Examples:

Getting candle information

client  = OandaAPI::Client::TokenClient.new :practice, token
candles = client.candles( instrument: "EUR_USD",
                         granularity: "M1",
                               count: 1,
                       candle_format: "midpoint" ).get

candles              # => OandaAPI::ResourceCollection
candles.granularity  # => "M1"
candles.instrument   # => "EUR_USD"
candles.first        # => OandaAPI::Resource::Candle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, resource_descriptor, location: nil) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.

Parameters:

  • attributes (Hash)

    collection of resource attributes

  • resource_descriptor (OandaAPI::Client::ResourceDescriptor)

    metadata about the resource collection and its elements.

  • location (String) (defaults to: nil)

    optional may contain a URI to related resources



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

def initialize(attributes, resource_descriptor, location:nil)
  attributes = {} if attributes.nil? || attributes.respond_to?(:empty) && attributes.empty?
  if attributes.kind_of?(Array)
    h = {}
    h["#{resource_descriptor.collection_name}".to_sym] = attributes
    attributes = h
  end

  fail ArgumentError, "Expecting a Hash" unless attributes.respond_to? :each_pair
  @attributes = Utils.rubyize_keys attributes
  @collection = @attributes.delete(resource_descriptor.collection_name) || []
  @collection.map! { |resource| resource_descriptor.resource_klass.new resource }
  @location = location
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Responds to collection-scoped accessor methods that are specific to the type of resource collection. For example, a Candle collection includes the collection-scoped methods #granularity and #instrument.



60
61
62
63
64
65
66
67
68
69
# File 'lib/oanda_api/resource_collection.rb', line 60

def method_missing(sym, *args)
  case
  when @attributes.keys.include?(sym)
    @attributes[sym]
  when @collection.respond_to?(sym)
    @collection.send sym
  else
    super
  end
end

Instance Attribute Details

#locationString (readonly)

Returns:



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
# File 'lib/oanda_api/resource_collection.rb', line 20

class ResourceCollection
  include Enumerable

  attr_reader :location

  # @param [Hash] attributes collection of resource attributes
  #
  # @param [OandaAPI::Client::ResourceDescriptor] resource_descriptor metadata
  #   about the resource collection and its elements.
  #
  # @param [String] location optional may contain a URI to related resources
  def initialize(attributes, resource_descriptor, location:nil)
    attributes = {} if attributes.nil? || attributes.respond_to?(:empty) && attributes.empty?
    if attributes.kind_of?(Array)
      h = {}
      h["#{resource_descriptor.collection_name}".to_sym] = attributes
      attributes = h
    end

    fail ArgumentError, "Expecting a Hash" unless attributes.respond_to? :each_pair
    @attributes = Utils.rubyize_keys attributes
    @collection = @attributes.delete(resource_descriptor.collection_name) || []
    @collection.map! { |resource| resource_descriptor.resource_klass.new resource }
    @location = location
  end

  # @yield [OandaAPI::ResourceBase]
  # @return [Enumerator]
  def each
    if block_given?
      @collection.each { |el| yield el }
    else
      @collection.each
    end
  end

  # @private
  # Responds to collection-scoped accessor methods that are specific to the
  # type of resource collection. For example, a `Candle` collection includes
  # the collection-scoped methods `#granularity` and `#instrument`.
  def method_missing(sym, *args)
    case
    when @attributes.keys.include?(sym)
      @attributes[sym]
    when @collection.respond_to?(sym)
      @collection.send sym
    else
      super
    end
  end

  # Returns `true` for concrete, delegated and dynamic methods.
  # @return [Boolean]
  def respond_to?(sym)
    case
    when @attributes.keys.include?(sym)
      true
    when @collection.respond_to?(sym)
      true
    else
      super
    end
  end
end

Instance Method Details

#each {|OandaAPI::ResourceBase| ... } ⇒ Enumerator

Yields:

Returns:

  • (Enumerator)


48
49
50
51
52
53
54
# File 'lib/oanda_api/resource_collection.rb', line 48

def each
  if block_given?
    @collection.each { |el| yield el }
  else
    @collection.each
  end
end

#respond_to?(sym) ⇒ Boolean

Returns true for concrete, delegated and dynamic methods.

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
# File 'lib/oanda_api/resource_collection.rb', line 73

def respond_to?(sym)
  case
  when @attributes.keys.include?(sym)
    true
  when @collection.respond_to?(sym)
    true
  else
    super
  end
end